I have multiple try catch
blocks and all of them are using the same catch
block.
try {
invoke-command -cn $host -Credential $cred -ScriptBlock {
param($name)
statement...
} -ArgumentList $name
} catch {
$formatstring = "{0} : {1}`n{2}`n" +
" + CategoryInfo : {3}`n" +
" + FullyQualifiedErrorId : {4}`n"
write-host ($formatstring)
exit 1
}
...
try {
another statement...
} catch {
$formatstring = "{0} : {1}`n{2}`n" +
" + CategoryInfo : {3}`n" +
" + FullyQualifiedErrorId : {4}`n"
write-host ($formatstring)
exit 1
}
I want to ask if it is possible to create a function which has the catch
block so that I can call and use the function instead of writing the same catch block
multiple times.
I am using poweshell 5