I have a script that will contact all servers in AD and then see if they are Virtual Machines. for any server that doesn’t have the appropriate registry value an error is returned. Here are the relevant lines:
$RegBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$Server) $VMHostName = $RegBase.OpenSubKey("SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters\$_").GetValue('PhysicalHostName')
The error returned is “You cannot call a method on a null-valued expression.”
-erroraction silently continue is not supported here, and I know that I can set $ErrorActionPreference= ‘silentlycontinue’ to stop all errors, but really I want to see any other genuine errors.
So, is there a way to stop errors that only show when the above registry key does not exist?