VMware Cloud Community
dwchan
Enthusiast
Enthusiast
Jump to solution

How to set error output to null

let say I want to check if a specific os customization spec exist

if ((Get-OSCustomizationSpec –Name “Win2016 Spec”) -eq $null){
write-host "It does not exist"
}

Even though the logic work, everytime I run it, where the entry does not exist, it will also output the ugly error

Get-OSCustomizationSpec : 12/3/2020 8:49:40 PM Get-OSCustomizationSpec Could not find Customization Specification with name
'Win2016 Spec'.
At D:\VMware\WindowsVM.ps1:86 char:6
+ if ((Get-OSCustomizationSpec –Name “Win2016 Spec”) -eq $null){
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Win2016 Spec:String) [Get-OSCustomizationSpec], VimException
+ FullyQualifiedErrorId : Common_CommonUtil_FilterCollection_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.
GetOSCustomizationSpec

It does not exist

How do i rewrite the code so it does show the error output?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can use the ErrorAction parameter for that.

if ((Get-OSCustomizationSpec –Name “Win2016 Spec” -ErrorAction SilentlyContinue) -eq $null){
    write-host "It does not exist"
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

You can use the ErrorAction parameter for that.

if ((Get-OSCustomizationSpec –Name “Win2016 Spec” -ErrorAction SilentlyContinue) -eq $null){
    write-host "It does not exist"
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

dwchan
Enthusiast
Enthusiast
Jump to solution

Thank you again.  Worked like a champ

0 Kudos