jump to next server in the for each loop if pre checks fails

Viewed 31

This is start of my script where I do some things to my servers. First I get servers from txt file and check that I have access to net share so can access the package needed and after that move forward using invoke command and script block

foreach ($server in (Get-Content .\myservers.txt)) {


# check access to net share

            if (test-path "\\mynetshare\package"){
                 write-host "I have access to net share on"  -ForegroundColor Green
               
            }
            else {
                write-host "I have not access to net share" -ForegroundColor Red
            }

# check the c:\temp
            if (test-path "\\$Server\c$\Temp"){
                 write-host "server has C:\Temp folders." -ForegroundColor Green
            }
            else {
                write-host "server has no  C:\Temp. Let's create one."
                New-Item -Type Directory "\\$Server\c$\Temp" -Force
            }

    Copy-Item -path \\mynetshare\package\package.msi -Destination "\\$server\C$\Temp" -Recurse
    Invoke-Command -ComputerName $server -ScriptBlock {

my powershell script does magic

}

}

So what do I need to add / change of if I have no access to server (e.g Windows Remote Management (WinRm) problems) so it would not go forward from Invoke-Command -ComputerName $server -ScriptBlock part but instead just jumps the next server in the list?

0 Answers
Related