Выход возвращает false, когда должно быть true - PullRequest
0 голосов
/ 28 августа 2018

Я пытаюсь переместить папку с одного компьютера на другой, используя скрипт, чтобы вывести результат в 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"

Ответы [ 2 ]

0 голосов
/ 28 августа 2018

Включите оператор test-Path в цикл foreach для повторной оценки для каждого компьютера:

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} 
0 голосов
/ 28 августа 2018

Я думаю, вы должны поместить это в цикл foreach:

$path = Test-Path "\\$computer\c$\Sun\Stephen\Install_me.ps1\"

Также я бы назначил $true и $false на $Output вместо строк.

...