Invoke-Command & (PSCX) Get-Clipboard

Welcome Forums General PowerShell Q&A Invoke-Command & (PSCX) Get-Clipboard

This topic contains 2 replies, has 3 voices, and was last updated by

 
Member
3 years, 10 months ago.

  • Author
    Posts
  • #21102

    Member
    Points: 0
    Rank: Member

    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.

    R/
    Sid

  • #21106

    Member
    Points: 0
    Rank: Member

    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

    Member
    Points: 0
    Rank: Member

    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: http://tommymaynard.com/ql-get-the-version-of-powershell-not-its-host.

The topic ‘Invoke-Command & (PSCX) Get-Clipboard’ is closed to new replies.