【问题标题】:Write PowerShell object with CSV formatting directly to FTP将 CSV 格式的 PowerShell 对象直接写入 FTP
【发布时间】:2019-01-13 23:42:29
【问题描述】:

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,然后将其传送到需要去的地方。

【问题讨论】:

    标签: .net powershell csv ftp


    【解决方案1】:

    将内存数据上传到 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
    

    【讨论】:

    • 嗨,马丁,感谢您的回复。我刚刚找到了 cmdlet ConvertTo-CSV,所以我想我已经准备好了!
    猜你喜欢
    • 2012-07-22
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 2020-01-07
    • 2018-03-18
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    相关资源
    最近更新 更多