none
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
    "---------------------------------------------------------------"
    }
    Thursday, September 24, 2015 9:16 AM

Answers

  • The answer is still the same.  You need to fix the code as note by Mike.

    You also need to spend some time learning how to use PowerShell. What you have here has many issues.


    \_(ツ)_/

    Thursday, September 24, 2015 9:44 AM

All replies

    • Edited by jrv Thursday, September 24, 2015 9:24 AM
    Thursday, September 24, 2015 9:24 AM
  • It was far from the answer, it was completely not to do with the problem. Not only that, the argument is invalid, run the script and change "." to the network machine name and it DOES return the remote machine results. as I said, it was tried and tested. I've had the company developers look at this and they also cannot see there being a problem and say it looks like a bug. 

    Wasn't sure how to bump threads so I created a new one.




    Thursday, September 24, 2015 9:28 AM
  • The answer is still the same.  You need to fix the code as note by Mike.

    You also need to spend some time learning how to use PowerShell. What you have here has many issues.


    \_(ツ)_/

    Thursday, September 24, 2015 9:44 AM
  • I do agree that my Powershell script is far from the best, it's only a little project I am working on at work currently.

    I appreciate all your feedback and will try to amend the script and add -computername $computer where necessary 

    Thursday, September 24, 2015 9:50 AM
  • Here is a fix for many of the obvious errors and poor constructs.

    $computers='.'
    $computers |
    ForEach-Object{
    	$computer=$_
    	$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
    	"-------------------------------------------------------"
    	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} Gb' -f ($computerHDD.Size/1GB)
    	'HDD Space: {0:P2}Gb Free {1:N2}Gb' -f ($computerHDD.FreeSpace/$computerHDD.Size),($computerHDD.FreeSpace/1GB)
    	'RAM: {0:N2} Gb' -f ($computerSystem.TotalPhysicalMemory/1GB)
    	"Operating System:$($computerOS.caption), Service Pack:$($computerOS.ServicePackMajorVersion)"
    	"User logged In: $($computerSystem.UserName)"
    	"Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
    	"-------------------------------------------------------"
    	write-host 'Networking Information' -Foregroundcolor Green -Backgroundcolor Black
    	Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $computer | select Caption, MacAddress
    	Write-Host finished
    	'----------------------------'
    }
    Note that anything output with "Write-Host" cannot be saved or printed.


    \_(ツ)_/

    Thursday, September 24, 2015 10:22 AM
  • Hi JRV,

    I had an answer that has fixed the selected issue. 

    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 -ComputerName $Computer | Select-Object -Property Name, version
    $ComputerMAC = Get-WmiObject win32_networkadapterconfiguration -ComputerName $Computer | 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"
    #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
    "---------------------------------------------------------------"

    This is now working and getting all information for networked machines one their name is added to $arrcomputers.  thank you for your time

    Thursday, September 24, 2015 10:25 AM
  • So you are going to ignore the rest of your issues.  Any change in your environment and what you posted will malfunction.


    \_(ツ)_/

    Thursday, September 24, 2015 10:50 AM