Why can’t I pass a a variable to Invoke Command

Welcome Forums General PowerShell Q&A Why can’t I pass a a variable to Invoke Command

Viewing 5 reply threads
  • Author
    Posts
    • #201528
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      I have a file with two workstations I want to pull a software list from. This list will grow to about 20 or 30 soon. I wrote this little simple script but can’t get it to work. Can someone please point me in the right direction? It seems like it should work but doesn’t. I can write-output and show the variable is correct and I can use any of the computer names in the file by themselves in the invoke-command statement and it works fine. Any advice is appreciated.

      $computers=Get-Content C:\temp\UDDElist.txt
      Foreach ($computer in $computers) {Invoke-command -cn $computer -ScriptBlock { Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\uninstall\* |
      Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table -AutoSize > c:\temp\”$computer”Software_list.txt }}

      Invoke-command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri
      parameter, or pass URI objects instead of strings.
      At C:\Users\xxx.xxxx.wa\documents\Windows powershell\software.ps1:3 char:37
      + … computers) {Invoke-command -cn $computer -ScriptBlock { Get-ItemPrope …
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
      + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand

      • This topic was modified 3 weeks, 2 days ago by wyomoose63.
    • #201561
      Participant
      Topics: 0
      Replies: 3
      Points: 15
      Rank: Member

      There’s a formatting error in your C:\temp\UDDElist.txt file.

      Also, you don’t need to iterate over each computer name. You can just pass them all at once:

      Invoke-command -ComputerName $computers -ScriptBlock scriptBlock

    • #201747
      Jon
      Participant
      Topics: 13
      Replies: 226
      Points: 172
      Helping Hand
      Rank: Participant

      Make sure you don’t have any extra spaces anywhere in your text file,¬† happens to me from time to time.

    • #201759
      js
      Participant
      Topics: 28
      Replies: 760
      Points: 2,140
      Helping Hand
      Rank: Community Hero

      You need the using: scope inside the scriptblock to refer to a local variable:

      $using:computer
      
    • #201798
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      Thanks guys. The file did have white space and I changed it with the suggestions. Now it works and I know a little more then I did.

    • #202262
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      is there anyway to pull a name and version from the following? HKLM:\Software\Microsoft\Internet Explorer and HKLM:\Software\Microsoft\Media Player¬† these do not have Display names I can get the version but how do I get the name of the windows embedded software from the registry? I could add a key on every box called DisplayName but I don’t like that idea.

Viewing 5 reply threads
  • You must be logged in to reply to this topic.