I am using PowerShell scripts to do various tasks on remote computers.
PS Remoting is enabled on all computers but sometimes I still get some issues with some where I can't connect- these are normally easily solved by checking computer is not on a public network, firewall rules etc - fixing the connection issue is not the question here.
SOI want a reliable way to check it can be accessed before running rest of my script.
So I firstly test machine is online using Test-Connection - this works okay to exclude any machines offline or not responding.
Then I need to check machine can be accessed via winrm - I tried Test-WSMan but even on some machines I couldn't connect to this came back as being okay - so that is not much use.
For example response from Test-WSMan - all looks okay, and same output I get from working machine:
wsmid : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd
ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
ProductVendor : Microsoft Corporation
ProductVersion : OS: 0.0.0 SP: 0.0 Stack: 3.0
But if now try Invoke-Command on same machine I get error below:-
[Lxxxx] Connecting to remote server Lxxxx failed with the following error message : WinRM cannot process the request. The following error with errorcode 0x80090322 occurred while using Kerberos authentication: An unknown security error occurred. Possible causes are: -The user name or password specified are invalid. -Kerberos is used when no authentication method and no user name are specified. -Kerberos accepts domain user names, but not local user names. -The Service Principal Name (SPN) for the remote computer name and port does not exist. -The client and remote computers are in different domains and there is no trust between the two domains. After checking for the above issues, try the following: -Check the Event Viewer for events related to authentication. -Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or use HTTPS transport. Note that computers in the TrustedHosts list might not be authenticated. -For more information about WinRM configuration, run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic. + CategoryInfo : OpenError: (Lxxxx:String) [], PSRemotingTransportException + FullyQualifiedErrorId : -2144108387,PSSessionStateBroken
So I have been using the below to check connection, and if response is 1 then it is okay to continue with script, and it it comes back with error then add computer name to $FailedRemoting variable to check later, and skip to next machine in list.
Invoke-Command -ComputerName L21217 { 1 }
Is that the best method, or is there another way I should be checking access via winrm?
Thanks