I have the script below that checks if a folder exists, and if it doesn’t it displays the message in Write-host below. The problem is that, it is displaying it, but it also displays the message in bold. I don’t want it to show the message in bold. Would a try / catch work better here?
if ((Test-Path $HomeDirectory) -eq $false)
{
Write-host "ATTENTION: "$Full_Name " home folder path was not found" -ErrorAction SilentlyContinue
}
NEW-ITEM : The network path was not found
At \hqfs1\users\tantony\PowerShell\HRSecurityForms\HRForm.ps1:112 char:13
NEW-ITEM –path $HomeDirectory -type directory -force | Ou ...
So… you’ve attached -SilentlyContinue to Write-Host, meaning if Write-Host fails, it will not display an error message. You have not in any way prevented Test-Path from throwing an error.
What’s the goal here? Have Test-Path not show an error? Then you add -SilentlyContinue to Test-Path. But your’e not going to know it failed, if that’s the case, so you get no chance to print an alternate message. If you need to CATCH the error and do something about it, you’re going to use -ErrorAction Stop, and a Try/Catch block. See, “The Big Book of PowerShell Error Handling” on our eBooks menu.
I have this working now. Some of our servers are named “server-” branch number, and some of them are named after the city. Since I don’t know if they’re named after the city or the branch number, I have a script that looks up the branch number, and if it doesn’t work, it looks up by the city.