Invoke-Command & (PSCX) Get-Clipboard

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of tommymaynard tommymaynard 2 years, 6 months ago.

  • Author
  • #21102
    Profile photo of Sidney Moore
    Sidney Moore

    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.


  • #21106
    Profile photo of Dave Wyatt
    Dave Wyatt

    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.

  • #21108
    Profile photo of tommymaynard

    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: [url][/url].

You must be logged in to reply to this topic.