13

I have been looking and can't find specifically what I am looking for, I need a way that I can test about 900 machines and tell whether or not powershell remoting is enabled, I figured out that with the script below I can verify that powershell is installed but it doesn't check that it can actually remotely manage the machine, any ideas?

function Check-PSVersion { [CmdletBinding()] Param ( [Parameter(Mandatory = $true)] $ComputerName ) if (Test-Path $ComputerName) { $Computers = Get-Content $ComputerName } Else { $Computers = $ComputerName } Foreach ($Computer in $Computers) { Try { Write-Host "Checking Computer $Computer" $path = "\\$Computer\C$\windows\System32\WindowsPowerShell\v1.0\powershell.exe" if (test-path $path) { (ls $path).VersionInfo } else { Write-Host "Powershell isn't installed" -ForegroundColor 'Red' } Write-Host "Finished Checking Computer $Computer" } catch { } } } 

4 Answers 4

19

You can use the Test-WSMan cmdlet to check whether the WinRM service is running on a remote computer.

[bool](Test-WSMan -ComputerName 'ComputerName' -ErrorAction SilentlyContinue) 
Sign up to request clarification or add additional context in comments.

4 Comments

If it doesn't return an error it is on and if it is disabled it returns an error, thanks
any idea how to make that come back as a Boolean so it is less text to sort through?
[bool](Test-WSMan -ComputerName 'ComputerName' -ErrorAction SilentlyContinue)
also, though Test-WSMan can return true, you may not have access, so [bool](invoke-command -ComputerName 'ComputerName' -ScriptBlock {"hello from $env:COMPUTERNAME"} -ErrorAction SilentlyContinue)
3

at the risk of being off-topic ...

function server_available?( $_server_to_act_on ) { if ( Test-Connection -ComputerName $_server_to_act_on -Count 1 -Quiet ) { Write-Host "`nTest-Connection: $_server_to_act_on OK" -NoNewline if ( [bool](Test-WSMan -ComputerName $_server_to_act_on -ErrorAction SilentlyContinue) ) { Write-Host ", Test-WSMan: $_server_to_act_on OK" -NoNewline if ( [bool](Invoke-Command -ComputerName $_server_to_act_on -ScriptBlock {"hello from $env:COMPUTERNAME"} -ErrorAction SilentlyContinue) ) { Write-Host ", Invoke-Command: $_server_to_act_on OK" -NoNewline return $true } } } return $false } 

1 Comment

1. The ping (Test-Connection) should be, say, 3 in a row, at least 2, to ignore network glitches. 2. You also have to check that Get-Item wsman:\localhost\client\trustedhosts on each host contains the other host. 3. by default without non-default option Enable-PSRemoting -SkipNetworkProfileCheck it will only be enabled for private networks (which is probably what most people want). The code in this answer will still work (return accurate true/false pass/fail) but it won't give a hints about why it failed.
0

Working from Straffs code above, this is my working version of how to determine if PS Remoting is working on 1 or more machines. Save the code and put it in the same folder with a file named "PCList.txt" Run the script and it will display the status as each system in the list AND produce a new report each time you run it in .CSV format directly clickable into excel. Put one machine per line in the PCList.txt file. You must have local admin access to the remote machines for this to work, always "Run As Administrator"

function Test-CanRemoteSystetmRunPSCmd( $_server_to_act_on ) { Write-host "`n`n$Counter of $Total - Testing System:`t$_server_to_act_on`n" if ( Test-Connection -ComputerName $_server_to_act_on -Count 1 -Quiet ) { Write-Host "`nTest-Connection: $_server_to_act_on OK, " -NoNewline if ( [bool](Test-WSMan -ComputerName $_server_to_act_on -ErrorAction SilentlyContinue) ) { Write-Host "Test-WSMan: $_server_to_act_on OK, " -NoNewline if ( [bool](Invoke-Command -ComputerName $_server_to_act_on -ScriptBlock {"hello from $env:COMPUTERNAME"} -ErrorAction SilentlyContinue) ) { Write-Host "Invoke-Command: $_server_to_act_on OK. `n" return "$_server_to_act_on,SUCCESS" } Else { Return "$_server_to_act_on,FAILED Invoke-Command" Continue } } Else { Return "$_server_to_act_on,FAILED Test-WSMAN" Continue } } Else { Return "$_server_to_act_on,FAILED Test-Connection" Continue } } Clear-Host # Intialze the report name and header anchored in the current script folder $_NewRunTime = Get-Date # Initialize the time of the script being run $_LastRunTime = $_NewRunTime $_Year = $_NewRunTime.Year $_Month = $_NewRunTime.Month $_Day = $_NewRunTime.Day $_Hour = $_NewRunTime.Hour $_Min = $_NewRunTime.Minute $_Sec = $_NewRunTime.Second $_Report_DateTime_Header = "$_Year.$_Month.$_Day.$_Hour.$_Min.$_Sec" $_report_File = "$PSScriptRoot" + '\' + "$_Report_DateTime_Header" + '_PSRemotingStatusReport.csv' $_ReportHeader = "Computer Name,PS Remoting Status,Test Time" Out-File -FilePath $_report_File -InputObject $_ReportHeader -Encoding UTF8 # Add a line to the report $PCList = Get-Content -Path "$PSScriptRoot\PCList.txt" $Total = $PCList.Count $Counter = 0 ForEach ($PC in $PCList) { $Counter = $Counter + 1 $T = Measure-Command {$RemoteCommandCheck = Test-CanRemoteSystetmRunPSCmd $PC} $TestTimeTotal = $T.Totalseconds If($RemoteCommandCheck -like "*Fail*") { Write-Host "$RemoteCommandCheck,$TestTimeTotal" -ForegroundColor Red -BackgroundColor Yellow } Else { Write-Host "$RemoteCommandCheck,$TestTimeTotal" -ForegroundColor White -BackgroundColor DarkGreen } $RemoteCommandCheck + ',' + $TestTimeTotal | Out-File -FilePath $_report_File -Encoding UTF8 -Append } 

Comments

0

Here's a looping script to check that remote powershell is working, basically that the host is functioning. I've had it freeze up even though it could still be pinged and the port was still active. It runs in a sub-thread but you can still hear the beep and see the write-host's in the console.

# start-threadjob avdcheck.ps1 -StreamingHost $Host # $s2 is an array of new-pssession's while ($true) { foreach ($s in $using:s2) { icm $s { pwd } > $null if (! $?) { [console]::beep(500,300) write-host ($s.computername + " is not responding " + (get-date)) } } sleep (60*15) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.