End user of my script may have two accounts [Exchange(User_ex) and Lync(User_sup)]. When he configured his local PS profile only for exchange (user_ex) and trying to get logical disk info of Lync server(which needs User_sup) then my script suppress error messages for WMI classes, especially for AccessDenied errors.
I tried by setting the global PS profile variable $ErrorActionPreference=”SilentlyContinue” which was worked but I don’t want to use this as this is not a best practice of coding.
Both Exchange and Lync are in the same domain
PS C:> Get-WmiObject -Class Win32_LogicalDisk -ComputerName hzwas251 -ErrorVariable Err -ErrorAction “SilentlyContinue”
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:1 char:14
- Get-WmiObject <<<< -Class Win32_LogicalDisk -ComputerName hzwas251 -ErrorVariable Err -ErrorAction "SilentlyContinue"
- CategoryInfo : NotSpecified: ( [Get-WmiObject], UnauthorizedAccessException
- FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand