Array not retaining Data

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Brian Bourque Brian Bourque 1 year, 6 months ago.

  • Author
  • #24274

    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.

  • #24279
    Profile photo of

    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
  • #24289
    Profile photo of Rob Simmers
    Rob Simmers

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

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

    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

  • #24313
    Profile photo of Rob Simmers
    Rob Simmers

    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?

  • #24362

    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.

  • #24363
    Profile photo of Rob Simmers
    Rob Simmers

    You could programmatically do that too:

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

    a visual representation:

    [attachment file="trim.png"]

  • #24430

    Rob that works perfectly TY

You must be logged in to reply to this topic.