Array not retaining Data

hello all,
I am working on a simple script to stop and then restart some services on some remote PC’s that is pulled from a .txt file I am able to get the var to show the all the computers from the text file, but when I try to use the list as an array it seems to be empty.

Here is the Test code I started to work with that shows me the array is not loading correctly.

Function Test-array {
Begin{ $Comptuers = Get-content -path c:\PCList.txt}
process {
#note the variable will show all the computers that is listed in PCList.txt
get-wmiobject -win32_bios -computername $Computers


now when I run that in powershell I get the following error:
get-wmiobject : cannot validate argument on perameter ‘Computername’. the argument is null or empty.

but when i look at the Variable it says it has data, i can select-objects with it ect.

not sure where the data is going.

also I am on PS version 2.

thank you in advance.

Hi Bourque,
Reasons for why you are unable to execute the code:

  1. If you keenly observer your code you can find that you did typo with the variable ( Comptuers & Computers).
  2. In Get-WMIObject command you prefixed “-” to the class name “Win32_BIOS”. Your code can be executed smoothly either you should write like (get-wmiobject -Class win32_bios -computername $Computers) or (get-wmiobject win32_bios -computername $Computers)
Function Test-array
        $Computers= Get-content -path C:\PCList.txt
        get-wmiobject win32_bios -computername $Computers

You can also use the CIM cmdlet as Get-WMIObject is deprecated:

Get-CIMInstance -ClassName Win32_Bios -ComputerName (Get-Content -Path C:\PCList.txt)

Hello and thanks for the responses,
sadly the typo’s was not the cause as I typed out the Code from the PC i am doing this on as that is a closed network system, but I di double check them.
also Get-CIMInstance is not supported in Powershell V2

I totally missed the V2 requirement at the bottom, you are correct. At this point, you have hand typed code into the forum for us to troubleshoot and indicated that the typos are not your issue. Can you provide the actual code you are running and the error you receiving? If you run the code from Vidya’s post, what error are you getting? Are there other requirements? What does “closed network system” mean?

so the issue as it turns out is the txt file it self.
seems the file has extra spaces in it and that seems to be what is causing the problem.
because when I removed the spaces from the file, the test array script above worked with out issue.

You could programmatically do that too:

$Computers= Get-content -path C:\PCList.txt | foreach{$_.Trim()}

a visual representation:

[attachment file=“trim.png”]

Rob that works perfectly TY