I found the following more readable, opposed to trying to parse out if/elseif. What are your thoughts about this ? Is there a better way ?
Example code where I want to check a bunch of things about an EXE on a remote system before replacing it with a different version:
# Testing Section - Reachable? Already done? Process Running? Folder RW? etc.
If ($true) {
$ProcessList = Get-Process -ComputerName $PCname -ErrorAction SilentlyContinue
if ($null -eq $ProcessList) {
Log -Msg "Error - Get process list from host failed, Host down? Skipping." -PCname $PCName
$Failed += $PCName
Continue
}
$Process = $ProcessList | Where-Object { $_.ProcessName -match "SOMEPRSS$" }
if ($SearchPss -eq [string]($Process.ProcessName)) {
Log -Msg "Error - Process is still running. Skipping." -PCname $PCName
$Failed += $PCName
Continue
}
If (-Not (Test-RemoteFolderReadWrite -Path $RemoteFolderPath)) {
Log -Msg "Error - Unable to Read/Write to remote filesystem. Skipping" -PCname $PCName
$Failed += $PCName
Continue
}
if (Test-FileLock -Path $RemoteFilePath) {
Log -Msg "Error - Lock on remote file. Skipping." -PCname $PCName
$Failed += $PCName
Continue
}
}
>Solution :
I think this is answered better at https://codereview.stackexchange.com/
Comments sum up this topic is eye of the beholder.