Invoke-Command & (PSCX) Get-Clipboard

This is more of a powershell question than PSCX. I know the following command works:

 Invoke-Command -ComputerName (Get-Content Machines.txt) -ScriptBlock {(Get-Host).Version} 

It should because I pulled it from get-help invoke-command. But if I copy those same machines to the clipboard and issue the following command, why doesn’t this work?:

Invoke-Command -ComputerName $(Get-Clipboard | Split-String -NewLine) -ScriptBlock {Get-Service -Name Spooler}

The Get-Clipboard is from a PSCX module that returns a string object. The error I get is The argument for ComputerName is null or empty.


There may be a blank line in your list. Try this, for analysis:

$list = @(Get-Clipboard | Split-String -NewLine)
Write-Host "Total entries in list: $($list.Count)"

$blank = $list -notmatch '\S'
Write-Host "Blank or Whitespace-only entries in list: $($blank.Count)"

Depending on what you find here, the solution may be just to filter out the blank lines before passing them on to Invoke-Command.

I see that you’re running (Get-Host).Version. Please tell me you’re not doing this to determine the version of PowerShell on the remote computer. If you are, then you should know that your command is going to produce the wrong results. I wrote a post about this on Tuesday. You may want to take a look: