.net - 将具有 CSV 格式的 PowerShell 对象直接写入 FTP

标签 .net powershell csv ftp

PowerShell 中有没有办法将对象直接写入 FTP?

例如,假设我需要读取 XML 文件并将相关部分发送到某处。 我会这样开始:

$URL = 'https://example.org/portal/v1/report01.xml'
$Response = [xml]( Invoke-WebRequest $URL -Method Get )
$Response.REPORT01.row | Select-Object id, date, name, balance-due

我知道我可以链接它来创建 CSV 文件...

| Export-Csv -Path "c:\temp\report01.csv" -Encoding ASCII -NoTypeInformation

...然后通过 FTP 将文件传输到其最终目的地。

我想知道是否有办法避免写入磁盘。所以我想做的就是获取响应,将其格式化为 CSV,然后将其发送到需要发送的位置。

最佳答案

将内存数据上传到 FTP 服务器的最简单方法是使用 WebClient.UploadString :

$client = New-Object System.Net.WebClient
$client.Credentials = New-Object System.Net.NetworkCredential("username", "password")
$client.UploadString("ftp://ftp.example.com/remote/path/file.csv", $contents)

要在内存中以 CSV 形式获取对象数据,请使用 ConvertTo-Csv而不是 Export-Csv:

$contents =
    (($data | ConvertTo-Csv -NoTypeInformation) -join [Environment]::NewLine) +
    [Environment]::NewLine

关于.net - 将具有 CSV 格式的 PowerShell 对象直接写入 FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51717361/

相关文章:

c# - 合并两个 Observable 时保留排序

csv - 在 Hive 中上传封闭格式的 .csv 数据

java - SUPER CSV 将 bean 写入 CSV

file - Lua:如何将大型 CSV 文件加载到表中并访问它

c# - 将所有警告视为错误

.net - Commit 期间 MySQL 超时

c# - 获取共享计算机上的目录列表 .NET

powershell - 批量隐藏Invoke-WebRequest

powershell - 通过 Powershell 运行 AZCopy 并监控进度

json - Microsoft GRAPH 查询列出了使用端点/deviceManagement/deviceHealthScripts 检测脚本内容的不熟悉格式