Invoke-Command & (PSCX) Get-Clipboard

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

  • Author
    Posts
  • #21102
    Profile photo of Sidney Moore
    Sidney Moore
    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

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

You must be logged in to reply to this topic.