I am having issues understanding the use of the try, catch statement.
I want to use it as a general error detection case so that is there are any issues in the given function the script will output "Server Offline"
The below snippet queries the WMI object on a server for it's list of HDD's and formats it into a table. If there server or WMI is not available I want to have "Server Offline" appended to the txt output file.
Currently the catch statement is never invoked when there is an error.
Can try catch be used this way?
try {
Get-WmiObject Win32_LogicalDisk -ComputerName $server | ft $server,deviceid,@{Label="Size(GB)";Expression={($_.Size/ 1gb) -as [int]}},@{Label="FreeSpace(Mb)";Expression={($_.FreeSpace/1mb) -as [int]}} -auto | Out-File c:\temp\ServersHDDAudit_OUTPUT-$a-$b.txt -Append
}
catch {
$server + ": Server Offline" | Out-File c:\temp\ServersHDDAudit_OUTPUT-$a-$b.txt -Append
}
Samuel