Array not retaining Data

Welcome Forums General PowerShell Q&A Array not retaining Data

This topic contains 7 replies, has 3 voices, and was last updated by

 
Member
3 years, 8 months ago.

  • Author
    Posts
  • #24274

    Member
    Points: 0
    Rank: Member

    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.

  • #24279

    Inactive
    Points: 0
    Rank: Member

    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{}
    }
    
  • #24289

    Participant
    Points: 355
    Helping Hand
    Rank: Contributor

    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

    Member
    Points: 0
    Rank: Member

    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

    Participant
    Points: 355
    Helping Hand
    Rank: Contributor

    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

    Member
    Points: 0
    Rank: Member

    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

    Participant
    Points: 355
    Helping Hand
    Rank: Contributor

    You could programmatically do that too:

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

    a visual representation:

  • #24430

    Member
    Points: 0
    Rank: Member

    Rob that works perfectly TY

The topic ‘Array not retaining Data’ is closed to new replies.