Author Posts

April 14, 2015 at 4:02 pm

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 {
$Computers
#note the variable will show all the computers that is listed in PCList.txt
get-wmiobject -win32_bios -computername $Computers

End{}

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.

April 14, 2015 at 9:39 pm

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
{
    Begin
    {
        $Computers= Get-content -path C:\PCList.txt
    }
    process
    {
        get-wmiobject win32_bios -computername $Computers
    }
    End{}
}

April 15, 2015 at 5:44 am

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

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

April 15, 2015 at 8:42 am

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

April 15, 2015 at 6:56 pm

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?

April 16, 2015 at 9:25 am

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.

April 16, 2015 at 9:53 am

You could programmatically do that too:

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

a visual representation:

[attachment file="trim.png"]

April 17, 2015 at 11:24 am

Rob that works perfectly TY