locked
Is This Powershell Script A Bug Or Am I The Bug RRS feed

  • Question

  • Hello!

    I am currently creating a Powershell script in V 2.0 and have the following Issue - 

    I am running a script to obtain machine hardware information, installed software and macaddress. When doing so, the third query always comes back blank, however uses just as much space as it would if it was showing the data. 

    When run on there own, all lines work successfully, but when running in one script, it does not. The issue is only with the Macaddress and the Installed Software query, it appears that the only one that works is the one that is first in the script. I think there is a problem with running both of these queries in the same script as if i add a forth query, it comes back absolutely fine.

    Here is the script - 

    Clear-Host
    $ArrComputers =  "."
    foreach ($Computer in $ArrComputers)
    {
        $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
        $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
        $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
        $computerCPU = get-wmiobject Win32_Processor -Computer $Computer
        $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
        $ComputerSoftware = Get-WmiObject -Class Win32_Product | Select-Object -Property Name, version
        $ComputerMAC = Get-WmiObject win32_networkadapterconfiguration | select description, macaddress
        "-------------------------------------------------------"
    write-host "System Information for: " $computerSystem.Name -foregroundcolor "Green" -BackgroundColor "Black"
            "-------------------------------------------------------"
            "Manufacturer: " + $computerSystem.Manufacturer
            "Model: " + $computerSystem.Model
            "Serial Number: " + $computerBIOS.SerialNumber
            "CPU: " + $computerCPU.Name
            "HDD Capacity: "  + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
            "HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
            "RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
            "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
            "User logged In: " + $computerSystem.UserName
            "Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
            "-------------------------------------------------------"
    Write-host "Installed Software" -Foregroundcolor "Green" -Backgroundcolor "Black"
    $ComputerSoftware 
    "---------------------------------------------------------------"
    write-host "Networking Information" -Foregroundcolor "Green" -Backgroundcolor "Black"
    $computerMAC
    Write-Host finished
    "---------------------------------------------------------------"
    }



    Wednesday, September 23, 2015 3:21 PM

Answers

  • Hi Rainer,

    I have figured out your issue, its due to the PS default formatting. PS tries to guess and fit in the data using some automatic mechanism, its not always correct, hence sometimes leads to missing data.

    You should use '$variable | ft -auto' atleast to get slightly better view.

    If it fails you need to use something called Composite Formatting.

    Hence just $computerMAC will not work, replace it with the following code:

    write-host "Networking Information" -Foregroundcolor "Green" -Backgroundcolor "Black"
    
     #Formatting Header
     "{0,-20} {1,-30}" -f "MacAddress","Description"
     "{0,-20} {1,-30}" -f "----------","-----------"
    
     #Looping through all the data
     foreach ($MAC in $ComputerMAC){ "{0,-20} {1,-30}" -f $MAC.MacAddress,$MAC.Description }
    
     Write-Host finished

    CAUTION: This makes the output visible, but doesn't makes it correct. Manually validate it on few computers before you can trust its correct. The earlier points by me and Mike still holds true.

    References:

    Format PowerShell Output with an Easy-to-Use Table:

    http://blogs.technet.com/b/heyscriptingguy/archive/2011/02/23/format-powershell-output-with-an-easy-to-use-table.aspx

    Use PowerShell to Format Strings with Composite Formatting

    http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/12/use-powershell-to-format-strings-with-composite-formatting.aspx


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    Thursday, September 24, 2015 10:11 AM

All replies

  • Hi,

    You haven't added -ComputerName to either of those lines, so you're only getting information back about the local machine.

    PS - I recommend getting away from Write-Host and creating actual objects if you ever want to do anything with this outside of printing it in the console.


    Wednesday, September 23, 2015 3:27 PM
  • Hi Mike,

    The script runs well when changing the "." to "examplename" and retrieves the correct results, this has been tested.

    Wednesday, September 23, 2015 3:43 PM
  • Hi Mike,

    The script runs well when changing the "." to "examplename" and retrieves the correct results, this has been tested.

    That's not possible as currently written. Unless you specify the machine to connect to, you're always going to get back local results.


    Wednesday, September 23, 2015 3:51 PM
  • Hi Rainer,

    Mike is correct have you noticed that you are missing -ComputerName $Computer in these lines.  So locally this script will always give correct results, but not for remote (Results would be mixed).

    $ComputerSoftware = Get-WmiObject -Class Win32_Product | Select-Object -Property Name, version
    $ComputerMAC = Get-WmiObject win32_networkadapterconfiguration | select description, macaddress
    

    I think you mean the MAC details are coming blank but line feed continunes. To test what's its returning use GM or Get-Member or FT, most likely its not able to get any info hence returning blank.

    $ComputerMAC = Get-WmiObject win32_networkadapterconfiguration | ft


    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Thursday, September 24, 2015 9:43 AM
  • Hi Rainer,

    I have figured out your issue, its due to the PS default formatting. PS tries to guess and fit in the data using some automatic mechanism, its not always correct, hence sometimes leads to missing data.

    You should use '$variable | ft -auto' atleast to get slightly better view.

    If it fails you need to use something called Composite Formatting.

    Hence just $computerMAC will not work, replace it with the following code:

    write-host "Networking Information" -Foregroundcolor "Green" -Backgroundcolor "Black"
    
     #Formatting Header
     "{0,-20} {1,-30}" -f "MacAddress","Description"
     "{0,-20} {1,-30}" -f "----------","-----------"
    
     #Looping through all the data
     foreach ($MAC in $ComputerMAC){ "{0,-20} {1,-30}" -f $MAC.MacAddress,$MAC.Description }
    
     Write-Host finished

    CAUTION: This makes the output visible, but doesn't makes it correct. Manually validate it on few computers before you can trust its correct. The earlier points by me and Mike still holds true.

    References:

    Format PowerShell Output with an Easy-to-Use Table:

    http://blogs.technet.com/b/heyscriptingguy/archive/2011/02/23/format-powershell-output-with-an-easy-to-use-table.aspx

    Use PowerShell to Format Strings with Composite Formatting

    http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/12/use-powershell-to-format-strings-with-composite-formatting.aspx


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    Thursday, September 24, 2015 10:11 AM
  • Hi Satyajit,

    I have amended the script with the -computername $computer and also added in the extra section that you have mentioned and it has worked fantastically!

    I know my Powershell skills are very limited and you have helped me here. Thank you very much!

    Thursday, September 24, 2015 10:20 AM
  • Hi Rainer,

    Glad that we were able to help and put our point across.


    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Thursday, September 24, 2015 12:10 PM