I will be converting this snip to two separate functions. One to test for general access to a remote computer and the other to test a WsMan connection. To test if access is granted on a remote computer I’m just doing Get-WMIObject just to see if I can access the remote computer. If I can’t access it I know the specific user group is not added to the local administrators group and I can just exit here.
My question: is there a better way to test remote administrator access on a computer?
Write-Verbose -Message "testing remote administrator access and WinRM on $ComputerName" -Verbose try{ $test = Get-WmiObject -Class win32_operatingsystem -ComputerName $ComputerName -ErrorAction Stop $access = $true } catch{ $access = $false } if($access -eq $false){ Write-Warning -Message 'Access denied: Please ensure that a required user group is added to the Local Administrators group. (domain\AD group)' Exit } if([bool](Test-WSMan -ComputerName $ComputerName -ErrorAction SilentlyContinue) -eq $false){ Write-Warning -Message "WinRM might not be configured on $ComputerName. Please correct this then run script again" Exit } Write-Verbose -Message 'Remote administrator access passed and WinRM access passed' -Verbose