Select 基于用户输入的 CSV 文件中 WinSCP .NET 程序集的 SFTP 连接数据
Select SFTP connection data for WinSCP .NET assembly from CSV file based on user input
我有这个 PowerShell 代码。
效果很好。但是当我向 db.csv
添加更多行时,它不起作用并且 return 错误代码:
Exception calling "Open" with "1" argument(s): "Connection has been unexpectedly closed. Server sent command exit status 0.
Authentication log (see session log for details):
Using username "username".
Authentication failed."
At C:\Users\me\Desktop\testeScript\CollectLog.ps1:41 char:5
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException
例如,当我在 db.csv
中只有两行时,脚本运行良好
HostName,IP
name1,10.10.1.1
name2,10.10.1.2
我尝试在 CSV 文档中使用 19 主机名和 Ip 地址行并且工作正常,但是当我只添加 1 个停止工作时。
19 件作品
20+ 不工作..
有什么想法吗? (谢谢你,对不起我的英语)
Add-Type -Path "WinSCPnet.dll"
$Name = @()
$ip = @()
Import-Csv db.csv |`
ForEach-Object {
$Name += $_.HostName
$ip += $_.IP
}
$inputID = Read-Host -Prompt "Type ID"
if ($Name -contains $inputID)
{
$Where = [array]::IndexOf($Name, $inputID)
Write-Host "IP: " $ip[$Where]
}
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "$ip"
UserName = "username"
Password = "password"
GiveUpSecurityAndAcceptAnySshHostKey = "true"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Transfer files
$session.GetFiles("/C:/Program Files/Common Files/logs/Device.log", "E:\loguri\Log\Arhive\*").Check()
}
finally
{
$session.Dispose()
}
Compress-Archive -Path "E:\loguri\Log\Arhive\Device.log" -DestinationPath "E:\loguri\Log\Arhive$inputID.zip" -Update
Remove-Item -Path "E:\loguri\Log\Arhive\Device.log" -Force
正如 Theo 所说,您无需将 CSV 分成两个数组。像这样导入
$db = import-csv 'db.csv'
您可以 $db[0]
、$db[1]
访问每一行,CSV 中的每一列都将是 属性,例如$db[0].Hostname
和 $db[0].IP
读入输入后,您只需 select 数组 $db
中的条目。也许像这样。但是,您的代码和我的代码都没有涵盖不匹配的情况!
$entry = $db -match $inputID
那么你的session会这样定义
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Scp
HostName = $entry.ip
UserName = "username"
Password = "password"
GiveUpSecurityAndAcceptAnySshHostKey = "true"
}
综上所述,鉴于您收到的错误消息,username/password 和 ip 的组合似乎无效。
Add-Type -Path "WinSCPnet.dll"
$db = import-csv 'db.csv'
$inputID = Read-Host -Prompt "Type ID"
$entry = $db -match $inputID
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = $entry.IP
UserName = "username"
Password = "password"
GiveUpSecurityAndAcceptAnySshHostKey = "true"
}
$session = New-Object WinSCP.Session
try {
# Connect
$session.Open($sessionOptions)
# Transfer files
$session.GetFiles("/C:/Program Files/Common Files/logs/Device.log", "E:\loguri\Log\Arhive\*").Check()
}
finally {
$session.Dispose()
}
if (Test-Path "E:\loguri\Log\Arhive\Device.log") {
Compress-Archive -Path "E:\loguri\Log\Arhive\Device.log" -DestinationPath "E:\loguri\Log\Arhive$inputID.zip" -Update
Remove-Item -Path "E:\loguri\Log\Arhive\Device.log" -Force
}
我有这个 PowerShell 代码。
效果很好。但是当我向 db.csv
添加更多行时,它不起作用并且 return 错误代码:
Exception calling "Open" with "1" argument(s): "Connection has been unexpectedly closed. Server sent command exit status 0.
Authentication log (see session log for details):
Using username "username".
Authentication failed."
At C:\Users\me\Desktop\testeScript\CollectLog.ps1:41 char:5
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException
例如,当我在 db.csv
中只有两行时,脚本运行良好
HostName,IP
name1,10.10.1.1
name2,10.10.1.2
我尝试在 CSV 文档中使用 19 主机名和 Ip 地址行并且工作正常,但是当我只添加 1 个停止工作时。
19 件作品 20+ 不工作..
有什么想法吗? (谢谢你,对不起我的英语)
Add-Type -Path "WinSCPnet.dll"
$Name = @()
$ip = @()
Import-Csv db.csv |`
ForEach-Object {
$Name += $_.HostName
$ip += $_.IP
}
$inputID = Read-Host -Prompt "Type ID"
if ($Name -contains $inputID)
{
$Where = [array]::IndexOf($Name, $inputID)
Write-Host "IP: " $ip[$Where]
}
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "$ip"
UserName = "username"
Password = "password"
GiveUpSecurityAndAcceptAnySshHostKey = "true"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Transfer files
$session.GetFiles("/C:/Program Files/Common Files/logs/Device.log", "E:\loguri\Log\Arhive\*").Check()
}
finally
{
$session.Dispose()
}
Compress-Archive -Path "E:\loguri\Log\Arhive\Device.log" -DestinationPath "E:\loguri\Log\Arhive$inputID.zip" -Update
Remove-Item -Path "E:\loguri\Log\Arhive\Device.log" -Force
正如 Theo 所说,您无需将 CSV 分成两个数组。像这样导入
$db = import-csv 'db.csv'
您可以 $db[0]
、$db[1]
访问每一行,CSV 中的每一列都将是 属性,例如$db[0].Hostname
和 $db[0].IP
读入输入后,您只需 select 数组 $db
中的条目。也许像这样。但是,您的代码和我的代码都没有涵盖不匹配的情况!
$entry = $db -match $inputID
那么你的session会这样定义
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Scp
HostName = $entry.ip
UserName = "username"
Password = "password"
GiveUpSecurityAndAcceptAnySshHostKey = "true"
}
综上所述,鉴于您收到的错误消息,username/password 和 ip 的组合似乎无效。
Add-Type -Path "WinSCPnet.dll"
$db = import-csv 'db.csv'
$inputID = Read-Host -Prompt "Type ID"
$entry = $db -match $inputID
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = $entry.IP
UserName = "username"
Password = "password"
GiveUpSecurityAndAcceptAnySshHostKey = "true"
}
$session = New-Object WinSCP.Session
try {
# Connect
$session.Open($sessionOptions)
# Transfer files
$session.GetFiles("/C:/Program Files/Common Files/logs/Device.log", "E:\loguri\Log\Arhive\*").Check()
}
finally {
$session.Dispose()
}
if (Test-Path "E:\loguri\Log\Arhive\Device.log") {
Compress-Archive -Path "E:\loguri\Log\Arhive\Device.log" -DestinationPath "E:\loguri\Log\Arhive$inputID.zip" -Update
Remove-Item -Path "E:\loguri\Log\Arhive\Device.log" -Force
}