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

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

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

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

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

$using:computer

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.

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.