Test-PSRemoting.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<#
.SYNOPSIS
Check PSRemoting state on remotes computers.
.DESCRIPTION
When you’re writing a script that depends on PowerShell Remoting, it’s often helpful to know that the remoting channel is open and will support the activities of your script.
 
PowerShell has a Test-WSMan command, but that only verifies that a WSMan connection is possible. There are other scenarios you could be impacted by:
 
  -Not having permission on the remote machine
  -Misconfiguration of the PowerShell endpoint
  -Corrupted installation
  -(etc)
 
As you dig deeper, you realize that the only way to really test the viability of the remoting channel is to just do something on it, and verify that you got the results you expected to.
Since the implementation was so simple, we didn’t write a cmdlet for it. In retrospect, the concept is more difficult than the implementation, so we probably should have written it anyways.
Here’s an example function that tests the remoting connection to a specific machine.
 
.INPUTS
One or more Computers objects.
 
.OUTPUTS
Returns True if PSRemoting is working or False if it doesn't.
If you target more than one computer, it only returns True if PSRemoting is working on all of them.
 
.NOTES
This module is an adaptation of function made by Lee Holmes found on related link. All credit for him.
I just brought it to the PowerShell Gallery to get it always online and ready for install anywhere.
 
Improvements:
 -Full Help
 -Multi string input
 
.LINK
http://www.leeholmes.com/blog/2009/11/20/testing-for-powershell-remoting-test-psremoting/
 
.EXAMPLE
PS C:\> Test-PSRemoting -ComputerName working-computer
True
 
Checking one computer where PSRemoting is working.
 
.EXAMPLE
PS C:\> Test-PSRemoting -ComputerName notworking-computer
False
 
Checking one computer where PSRemoting is NOT working.
 
.EXAMPLE
PS C:\> Test-PSRemoting -ComputerName working-computer, notworking-computer
False
 
Checking more than one computer where PSRemoting is NOT working at least in one of them.
#>


function Test-PSRemoting{
[CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [String[]]$ComputerName
    )
   
    try
    {
        $errorActionPreference = "Stop"
        $result = Invoke-Command -ComputerName $computername { 1 }
    }
    catch
    {
        Write-Verbose $_
        return $false
        
    }
   
    ## I've never seen this happen, but if you want to be
    ## thorough....
    if($result -ne 1)
    {
        Write-Verbose "Remoting to $computerName returned an unexpected result."
        return $false
    }
    
    $true   
    }