【问题标题】:Reading values from CSV files and storing in local variable in PowerShell从 CSV 文件中读取值并存储在 PowerShell 中的局部变量中
【发布时间】:2017-10-12 19:04:20
【问题描述】:

我被困在某一点上,要求是我必须将用户名、密码、主机名和主机密钥存储在 CSV 文件中,并且我必须读取并将值存储在可用于建立 SFTP 连接的本地变量中。

我的 CSV 如下所示:

主机名、用户名、密码、SshHostKey指纹 abc.com,李尔,qwert,ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx...

我用来读取和存储不同列值的代码是:

Add-Type -Path "WinSCPnet.dll"

$csv = Import-Csv c:\test\output.csv
$csv | ForEach-Object
{
    $Hostname = $_.hostname
    $username = $_.username
    $Password = $_.Password
    "$Hostname - $username -$Password"
}

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = $Hostname
    UserName = $username
    Password = $Password
    SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
}

尝试显示值时没有显示任何内容。

【问题讨论】:

  • 出于一般安全原因,您不能只将纯文本密码通过管道传输到 WinSCP(或任何其他 Microsoft 应用程序)。您需要使用安全密码 (ConvertTo-SecureString -String "something" -AsPlainText -Force) 创建凭据 (New-Object Management.Automation.PSCredential),例如:stackoverflow.com/a/41548976/1701026

标签: powershell csv sftp winscp


【解决方案1】:

ForEach-Object cmdlet 之后的脚本块必须在同一行开始。否则 PowerShell 不会将它们连接在一起。

$csv | ForEach-Object {
    $HostName = $_.HostName
    $Username = $_.Username 
    $Password = $_.Password
    "$HostName - $Username - $Password"
}

您最终将面临的另一个问题是您的代码确实只处理 CSV 文件中的最后一行。

您很可能实际上想要在ForEach-Object 脚本块处理解析出的值。像这样:

$csv | ForEach-Object {
    $HostName = $_.HostName
    $Username = $_.Username 
    $Password = $_.Password

    "$HostName - $Username - $Password"

    # Set up session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = $HostName
        UserName = $Username
        Password = $Password
        SshHostKeyFingerprint = ...
    }

    # ...
}

【讨论】:

  • 当我们从本地机器导入 CSV 时出现以下错误:Import-Csv : Cannot bind parameter 'Delimiter'。无法转换值"Integration\Scripts\Power" to type "System.Char". Error: "String must be exactly one character long."
  • 这是实际路径:# Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll" $csv = Import-Csv C:\abc\Projects\Bank\Attendance Integration\Scripts\Power Shell Script\Configuration.csv $csv | foreach-object { $Hostname = $_.hostname $username =$_.username $Password = $_.Password "$Hostname - $username -$Password" } $session.Dispose()
  • 路径必须用空格括起来:$csv = Import-Csv "C:\abc\Projects\Bank\Attendance Integration\Scripts\Power Shell Script\Configuration.csv"
  • 嗨,代码中还有一个错误:$csv = Import-Csv "C:\abc\Projects\Bank\Attendance Integration\Scripts\Power Shell Script\Configuration.csv" $csv | foreach-object { $Hostname = $_.hostname $username =$_.username $Password = $_.Password $hostkey = $_.hostkey } $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = $Hostname UserName = $username Password = $Password SshHostKeyFingerprint = $hostkey }
  • 即将出现的错误是:New-Object : The value supplied is not valid, or the property is read-only. Change the value, and then try again. At C:\abc\Projects\Bank\Attendance Integration\Scripts\Power Shell Script\Download&MoveToArchive.ps1:16 char:19
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-10
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 2019-09-13
  • 1970-01-01
相关资源
最近更新 更多