3
\$\begingroup\$

My code is working, however, in the foreach section, when it runs the invoke-command if I don't use "-ErrorAction SilentlyContinue" the red error text that states a computer cannot be connected to shows up. The code still works fine, I just wanted to know if there is a way to add "nicer" text, so that when the invoke-command fails to connect, it'll show "This computer cannot be reached"

Any help is appreciated.

$output_dir = "$env:USERPROFILE\Desktop\ad_computer_list"
$output_file = "$output_dir\computerlist.txt"
$computers_input_file = "computerlist.txt"
$script_name = "Create Spectrum Protect exclusions script"

Import-Module ActiveDirectory
New-Item -Force -ItemType Directory $output_dir | Out-Null
New-Item -Force -ItemType File $output_file

Set-Location $env:USERPROFILE\Desktop\ad_computer_list

#
# MAIN code body
#

Write-Host "`n`n################################################################" -ForegroundColor Gray
Write-Host "Running $($script_name)..." -ForegroundColor Cyan
Write-Host "################################################################" -ForegroundColor Gray

function Get-ComputerList {
param(
$OU_Base = $(Read-Host "Enter distinguished name of OU")
)

Get-ADComputer -Filter "*" -SearchBase "$OU_Base" | 
Select-Object -ExpandProperty Name | 
Sort | 
Out-File -Encoding ASCII -FilePath $env:USERPROFILE\Desktop\ad_computer_list\computerlist.txt

try {
# Get entries in text file, strip away whitespace, blank lines and lines starting with '#' characters
$computers = Get-Content $computers_input_file -ErrorAction Stop | Where-Object { ($_.Trim() -ne '') -and ($_ -NotMatch "^#") }
}
catch {
    Write-Error "Unable to open $computers_input_file. Please create or move the file to the same directory as this script."
break
}

Write-Verbose "$($computers.Count) computer entries found in input file."

foreach ($computer in $computers) {

Write-Host "Opening connection to $computer ..."

try {Invoke-Command -ErrorAction SilentlyContinue -ComputerName $computer -ScriptBlock {if(!(Get-Content -Path "C:\program files\tivoli\tsm\baclient\dsm.opt" -ErrorAction SilentlyContinue | Select-String "Excludedir", "Program Files"))

{Write-Host "Spectrum Protect is not installed or exclusions already exist." -ForegroundColor "Yellow"} 

else 

{add-content -Path "C:\program files\tivoli\tsm\baclient\dsm.opt" -Value `r`n'EXCLUDE.DIR "C:\Program Files"'}}

} catch {
    Write-Host "Cannot connect to computer!"
}

}

}
Get-ComputerList
\$\endgroup\$
1
  • \$\begingroup\$ Use Try, Catch, and Finally blocks to respond to or handle terminating errors in scripts. Therefore, use -ErrorAction Stop inside the Try block. Note that you can classify trapped errors using (optional) list of error type specifications used with the Catch keyword. \$\endgroup\$
    – JosefZ
    Dec 17, 2019 at 21:14

2 Answers 2

2
\$\begingroup\$

Your try block will be unable to pass the error to catch block if you don't use -ErrorAction Stop. Also there is an -ErrorAction Ignore on powershell v3.0+ that completely ignores the error.

\$\endgroup\$
2
\$\begingroup\$

Instead of the try-catch block (or in addition to), you can check if the last command was successful by checking the value of $?. If it's not true, you can output your desired error message.

Invoke-Command -ErrorAction SilentlyContinue -ComputerName $computer -ScriptBlock {}
if (-not $?)
{
   Write-Host "This computer ($computer) cannot be reached"
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.