我正在尝试使用脚本将文件夹从一台计算机移动到另一台计算机,以将结果输出到csv文件中,说明该文件是否已存在。目前,通过查看机器可以看到文件夹已移至该文件夹,但是在打开CSV文件时,它返回false。 一切都顺利到另一台机器,但我认为代码中可能有某些内容关闭了,但我不确定。
请参见下面的代码:
#>
# This is the file that contains the list of computers you want to send a file to.
# Change this path to point to the txt file. (IE. c:\Script\PClist.txt)
# You can list by host name or IP address.
$computers = gc "C:\Users\stephenstewart\Desktop\Stephen BAT\AssetList.txt"
# This is the FILE you want to copy to the remote/destination computer(s) (IE. c:\file.zip)
$source = "C:\Users\stephenstewart\Desktop\stephen"
# This is the location on the remote/destination computer where you want the file copied to
#(IE.c$\Windows\Temp) use the admin "$"
$dest = "c$\Sun"
# This is the location used to check for Results
$path = Test-Path "\\$computer\c$\Sun\stephen\Install_me.ps1\"
$results = @()
# There is nothing to change/edit below this line.
Write-Host "Copying Starling folder to all assets listed in AssetList"
foreach ($computer in $computers) {
if (test-Connection -Cn $computer -quiet) {
Copy-Item $source -Destination \\$computer\$dest -Recurse
} else {
"$computer is not online"
}
}
Write-Host "Operation Complete"
Write-Host "Preparing Results..."
foreach ($computer in $computers) {
if ($path -eq $true) {
$Output = "True"
} else {
$Output = "False"
}
$details = @{
Computer_Name = $computer
Output = $Output
}
$results += New-Object PSObject -Property $details
}
$results | select-object -property Computer_Name, Output | Export-csv 'C:\Users\stephenstewart\Desktop\StephenBAT\stephenResults.csv' -NoTypeInformation
Write-Host "Completed"
答案 0 :(得分:0)
我认为您必须将其放入foreach循环中
$path = Test-Path "\\$computer\c$\Sun\Stephen\Install_me.ps1\"
我也将$true
和$false
分配给$Output
而不是字符串。
答案 1 :(得分:0)
在foreach循环中包含test-Path语句,以便为每台计算机重新评估:
foreach ($computer in $computers) {
$output= Test-Path "\\$computer\c$\Sun\stephen\Install_me.ps1\"
}
$ output将具有$ True或$ False的布尔值,可以在下一条语句中轻松使用该值,例如
if ($output) {Invoke-Command -ComputerName $computername -Command #MyCommands}
else
{#do something else}