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/