以下是通过FTP上传数据的部分代码
foreach ($line in $FTPServer)
{
Start-Transcript -Path $results
Write-Host -Object "ftp url: $line"
If (Test-Connection $line -Count 1)
{
Set-FTPConnection -Credentials $FTPCredential -Server $line -Session MySession -UsePassive -ErrorAction SilentlyContinue
$Session = Get-FTPConnection -Session MySession
if($session.UsePassive -eq "True"){$connect="OK"}
else{$connect="!!!-FAIL-!!!"}
foreach ($item in (Get-ChildItem .\Upload))
{
Write-Host -Object "Uploading $item..."
$Send= Add-FTPItem -Session $Session -Path $FTPPlace -LocalPath .\Upload\$item -Overwrite -ErrorAction SilentlyContinue
if($Send.Name -eq $item.Name){$Rec="OK"}
else{$Rec="!!!-FAIL-!!!"}
$array = $line, $item, $connect, $Rec
$FailTable=New-Object -TypeName PSObject -Property ([ordered]@{"FTP Server"=$array[0]; "File"=$array[1];"Connected"=$array[2];"Uploaded"=$array[3]})
$FailTable|Out-File -Append '.\stats.txt'
}
Stop-Transcript
} Else {"$line">> .\DownServers.txt}
}
$Failtable
是一个哈希表,用于存储FTP服务器的IP($line
),上传文件的名称($item
),连接状态($connect
)和上传状态($Rec
)。哈希表通过管道传递给文件.\stats.txt
问题出在每次迭代中。\ stats.txt是保存的头文件:
FTP Server File Connected Uploaded
---------- ---- --------- --------
192.168.1.1 ConfigurationDivide.xml OK !!!-FAIL-!!!
FTP Server File Connected Uploaded
---------- ---- --------- --------
192.168.1.1 test.txt OK !!!-FAIL-!!!
所以我需要那个在另一个之下并像这样自动化:
FTP Server File Connected Uploaded
---------- ---- --------- --------
192.168.1.1 ConfigurationDivide.xml OK !!!-FAIL-!!!
192.168.1.1 test.txt OK !!!-FAIL-!!!
我尝试在循环之前放入哈希表声明$FailTable=New-Object -TypeName PSObject -Property ([ordered]@{"FTP Server"=$array[0]; "File"=$array[1];"Connected"=$array[2];"Uploaded"=$array[3]})
,然后在循环中添加值($line
,$item
,...),但是没有哈希表.add
方法。
答案 0 :(得分:1)
好的,不同答案:)
试试这个:
$FailTable = @()
$spam = New-Object PSObject
$spam | Add-Member -type NoteProperty -Name 'FTP Server' -Value ""
$spam | Add-Member -type NoteProperty -Name 'File' -Value ""
$spam | Add-Member -type NoteProperty -Name 'Connected' -Value ""
$spam | Add-Member -type NoteProperty -Name 'Uploaded' -Value ""
$FailTable += $spam
$FailTable | Out-File -Append '.\stats.txt'
foreach ($line in $FTPServer)
{
Start-Transcript -Path $results
Write-Host -Object "ftp url: $line"
If (Test-Connection $line -Count 1)
{
Set-FTPConnection -Credentials $FTPCredential -Server $line -Session MySession -UsePassive -ErrorAction SilentlyContinue
$Session = Get-FTPConnection -Session MySession
if($session.UsePassive -eq "True"){$connect="OK"}
else{$connect="!!!-FAIL-!!!"}
foreach ($item in (Get-ChildItem .\Upload))
{
Write-Host -Object "Uploading $item..."
$Send= Add-FTPItem -Session $Session -Path $FTPPlace -LocalPath .\Upload\$item -Overwrite -ErrorAction SilentlyContinue
if($Send.Name -eq $item.Name){$Rec="OK"}
else{$Rec="!!!-FAIL-!!!"}
$spam = New-Object PSObject
$spam | Add-Member -type NoteProperty -Name 'FTP Server' -Value $line
$spam | Add-Member -type NoteProperty -Name 'File' -Value $item
$spam | Add-Member -type NoteProperty -Name 'Connected' -Value $connect
$spam | Add-Member -type NoteProperty -Name 'Uploaded' -Value $Rec
$FailTable += $spam
$FailTable | Select-Object -Last 1 | Format-Table -HideTableHeaders | Out-File -Append '.\stats.txt'
}
Stop-Transcript
} Else {"$line">> .\DownServers.txt}
}
答案 1 :(得分:0)
您需要在循环外创建FailTable,您正在创建并将每个循环附加一个新的PSCustomObject添加到文件中。
尝试实现这个:
$FailTable = @()
#Example loop
for($i =0;$i -lt 5; $i++){
#Inside the loop add new object to the array
$failTable += @{"FTPServer"=$i;"File"=$i;"Connected"=$i;"Uploaded"=$i}
}
#foreach hashmap in the array cast to a PSCustomObject (which gives you the headers you want)
#and then Select-Object The order you want them in.
$FailTable.foreach{[PSCustomObject]$_} | Select-Object "FTPServer", "File","Connected", "Uploaded" | Out-file test.txt
编辑:
PowerShell v2 +兼容版本:
$FailTable = @()
$objTable = @()
#Example loop
for($i =0;$i -lt 5; $i++){
#Inside the loop add new object to the array
$failTable += @{"FTPServer"=$i;"File"=$i;"Connected"=$i;"Uploaded"=$i}
}
#foreach hashmap in the array cast to a PSCustomObject (which gives you the headers you want)
#and then Select-Object The order you want them in.
foreach($fail in $FailTable){
$objTable += New-Object -TypeName PSCustomObject -Property $fail
}
$objTable | Select-Object "FTPServer", "File","Connected", "Uploaded" | Out-file test.txt