0

I would like to create a script that tell the user whether or not PSRemoting is enabled on multiple remote machines. Here is what I have so far:

function Test-PSRemoting { Param( [Parameter(Mandatory=$True, ValueFromPipeline=$True)] [string[]] $ComputerName, [string] $Credential) $file = Read-Host "Enter file location for all error details: " try { $result = Invoke-Command -ComputerName $ComputerName -Credential $Credential { 1 } -ErrorAction SilentlyContinue -ErrorVariable Problem If ($Problem){ $result = 0} } catch { $result = 0} If ($result -eq 1){ write-host "PSRemoting is enabled on: " $ComputerName } else { write-host "PSRemoting is not enabled/working on: " $ComputerName $Problem | Out-File $file -Append} } 

The function works perfect if I only specify one computer:

Test-PSRemoting -ComputerName Server1 - Credentials Admin 

However, I cannot get the function to work if I specify multiple computers:

Test-PSRemoting -ComputerName Server1, Server2 - Credentials Admin Server1, Server2 | Test-PSRemoting -Credentials Admin 

2 Answers 2

3

I would use the Process{} block in this case (also answers your comment about why it was only checking Server2).

function test-ps { param( [Parameter(ValueFromPipeline=$true)] [string[]] $CN ) process{ Write-Host $CN "h" } } 'test1','test2' | test-ps 

http://ss64.com/ps/syntax-function-input.html

6
  • OK, using the Process{} block Server1, Server2 | test-psremoting -credentials Admin works fine but now test-psremoting -computername Server1, Server2 -credential Admin isn't working.... Commented Aug 7, 2015 at 16:16
  • @user2278281 $Credential shouldn't be a string..... it should be a credential object. blogs.msdn.com/b/koteshb/archive/2010/02/13/… Commented Aug 7, 2015 at 16:27
  • @Colyn1337 I tried using credential object and it broke script. I kept getting an error message "WinRM cannot complete the operation" on machines I know PSRemoting is enabled. I'll keep playing with it though Commented Aug 7, 2015 at 16:40
  • @robbp I would rather have Server1, Server2 | [command] working than [command] Server1, Server2 so we're all good. Commented Aug 7, 2015 at 16:42
  • @user2278281 another thing you can try is by using kerberos authentication with your invoke-command. That's typically what I do. Commented Aug 7, 2015 at 16:46
1

Try the following, comments below note key points.

function Test-PSRemoting { [CmdletBinding()] param ( [Parameter(Mandatory = $True, ValueFromPipeline = $True)] [string[]]$ComputerName, [Parameter(Mandatory = $False)] [System.Management.Automation.PSCredential]$Credential = (Get-Credential), [Parameter(Mandatory = $True)] [String]$OutFile ) process { # Pass computer name along the pipeline to allow for more then one. $ComputerName | % ` { $pcName = $_; $problem = $Null; try { $result = Invoke-Command -ComputerName $pcName -Credential $Credential { 1 } -ErrorAction SilentlyContinue -ErrorVariable problem; if ($problem -eq $Null -and $result -eq 1) { # Use Write-Verbose instead. Test-* commands are meant to return true/false. # But additional status can be viewed by adding the -Verbose parameter to Test-PSRemoting. i.e. Test-PSRemoting -Verbose Write-Verbose "PSRemoting is enabled on: $pcName"; return $True; } else { Write-Verbose "PSRemoting is not enabled/working on: $pcName"; $problem | Out-File $OutFile -Append; return $False; } } catch { return $False } } } } 

Key points are:

  • Use of $ComputerName | to allow for Test-PSRemoting -ComputerName Server1,Server2 usage by handling you conditional logic per computer name specified. Invoke-Command naturally handles multiple computer names but your if statements do not.
  • Moved $file to $OutFile parameter instead of Read-Host and marked as mandatory parameter to force input. Leaving Read-Host in a process { } block would mean that the file path is requested multiple times if Server1,Server2 | Test-PSRemoting is used. Also has the advantage of being able to be passed as a parameter instead of manually typed each time.
  • Moved verbose output to Write-Verbose instead of Write-Host. Test-* commands should return True/False instead of a string. Use -Verbose to see verbose output. Additionally you could change the Write-Verbose "PSRemoting is not enabled/working on: $pcName"; line to a Write-Warning instead.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.