locked
Get computer details from AD RRS feed

  • Question

  • Hey guys, 

    hope all is good:)

    I am trying to challenge myself with the following script, get some outcomes like CPU, Machine name, RAM Allocation, but do not get disk capacity and 

    The purpose is to get this details as CSV or HTML.

    caution:

    I am not programmer :)

    Import-Module ActiveDirectory
    $ADComputer = Get-ADComputer -Filter {Enabled -eq $true -and Operatingsystem -like "Windows Server*"} -Properties * | select name, operatingsystem
    
    
    
    foreach ($comp in $ADComputer)
    {
    	$servername = $comp.name  
    
    	$computerName = Get-WmiObject win32_computersystem -ComputerName $servername -ErrorAction SilentlyContinue | Select-Object Name,Manufacturer,systemname 
    
        $PhysicalRAM = (Get-WMIObject -class Win32_PhysicalMemory -ComputerName $servername -ErrorAction SilentlyContinue | Measure-Object -Property capacity -Sum | % {[Math]::Round(($_.sum / 1GB),2)}) 
    
       # $CPU =Get-WmiObject Win32_ComputerSystem -ComputerName $servername -ErrorAction SilentlyContinue | Select NumberOfLogicalProcessors, NumberOfProcessors,systemname,name 
        $Proccesor = Get-WmiObject –class Win32_processor -ComputerName $servername -ErrorAction SilentlyContinue | select systemname,Name,DeviceID,NumberOfCores,NumberOfLogicalProcessors, Addresswidth 
        $Disk = Get-WmiObject Win32_logicaldisk -ComputerName $servername -ErrorAction SilentlyContinue | Format-Table DeviceID, 
        @{Name="Drive Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}},
        @{Name="Drive Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}},
        @{Name="Drive Free pct";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}} -AutoSize
        $FreeSpace = [System.Math]::Round((($Disk.FreeSpace) / 1GB)) 
    
    
    	Write-Host "Computer Name:"$servername,"Machine Type:"$servername.Manufacturer -ForegroundColor yellow "RAM allocation:" $PhysicalRAM "NumberOfLogicalProcessors:" $Proccesor.NumberOfLogicalProcessor"NumberOfProcessors:" $Proccesor.NumberOfProcessors, "CPU Type:" $Proccesor.Name  $disk | Format-list
    
    
    }
    
    
    
    


    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    Monday, August 21, 2017 9:15 AM

Answers

  • Hey,

    Surprise! fixed and now it works like charm,

    JRV, thank you unused comment.


    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    • Marked as answer by Meir Peleg Monday, August 21, 2017 11:42 AM
    Monday, August 21, 2017 11:16 AM
  • Import-Module ActiveDirectory
    $ADComputer = Get-ADComputer -Filter {Enabled -eq $true -and Operatingsystem -like "Windows Server*"} -Properties * | select name, operatingsystem
    
    $Report = @()
    foreach ($comp in $ADComputer)
    {
    	$servername = $Comp.Name
    	$TestConnection = Test-Connection -ComputerName $servername -Count 1 -ErrorAction SilentlyContinue
    	if ($TestConnection)
    	{
    		$computerName = Get-WmiObject win32_computersystem -ComputerName $servername -ErrorAction SilentlyContinue
    		$OS = Get-WmiObject win32_operatingsystem -ComputerName $servername -ErrorAction SilentlyContinue
    		$Bios = Get-WmiObject win32_bios -ComputerName $servername -ErrorAction SilentlyContinue
    		$PhysicalRAM = (Get-WMIObject -class Win32_PhysicalMemory -ComputerName $servername -ErrorAction SilentlyContinue | Measure-Object -Property capacity -Sum | % { [Math]::Round(($_.sum / 1GB), 2) })
    		$CPU = Get-WmiObject Win32_ComputerSystem -ComputerName $servername -ErrorAction SilentlyContinue | Select NumberOfLogicalProcessors, NumberOfProcessors, systemname, name
    		$Proccesor = Get-WmiObject –class Win32_processor -ComputerName $servername -ErrorAction SilentlyContinue
    		$Disk = Get-WmiObject Win32_logicaldisk -ComputerName $servername -ErrorAction SilentlyContinue | select @{Name="Drive Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}},@{Name="Drive Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}},	@{Name="Drive Free pct";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}}
    		$Report += New-Object System.Management.Automation.PSObject -Property @{
    			'Server Name'    = $servername
    			'OS Name'		  = $OS.Caption
    			'Manufacturer'    = $computerName.Manufacturer
    			'Model'		      = $computerName.Model
    			'Serial Number'  = $Bios.SerialNumber
    			'Total RAM (GB)'= $PhysicalRAM
                'CPU - NumberOfLogicalProcessors' = $CPU.NumberOfLogicalProcessors
                'CPU - NumberOfProcessors' = $CPU.NumberOfProcessors
    
                		}
    	}
    
    	else
         
    
    	{
    		Write-Warning "Computer Name: $servername Isn't Available"
    
    	}
    $Report  | export-csv c:\x.CSV
    }
    
     
    



    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    • Marked as answer by Meir Peleg Tuesday, August 22, 2017 10:01 AM
    Tuesday, August 22, 2017 10:00 AM

All replies

  • Is there a question?

    \_(ツ)_/

    Monday, August 21, 2017 9:23 AM
  • Is there a question?

    \_(ツ)_/

    Hey, Yes there is,

    In the outcome, I don't get DISK information and instead of this I get this:



    Computer Name: XXXMachine Type: RAM allocation: 16 NumberOfLogicalProcessors:

    NumberOfProcessors: CPU Type: Intel(R) Xeon(R) CPU E5-2603 v3 @ 1.60GHz Intel(R) Xeon(R) CPU E5-2603

    v3 @ 1.60GHz Intel(R) Xeon(R) CPU E5-2603 v3 @ 1.60GHz Intel(R) Xeon(R) CPU E5-2603 v3 @ 1.60GHz + n

    Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal

    .Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell

    .Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData

    Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.

    Format.FormatEndData




    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    Monday, August 21, 2017 9:32 AM
  • That is because you are sing Format-Table then trying to use Write-Host.  Remove the Format-Table.


    \_(ツ)_/

    Monday, August 21, 2017 9:39 AM
  • Hey again Jrv, 

    Thanks for your comment, appreciate it.:

    I tried to remove it but now it failed about something else.

    Query:

    Import-Module ActiveDirectory
    $ADComputer = Get-ADComputer -Filter {Enabled -eq $true -and Operatingsystem -like "Windows Server*"} -Properties * | select name, operatingsystem
    
    
    
    foreach ($comp in $ADComputer)
    {
    	$servername = $comp.name  
    
    	$computerName = Get-WmiObject win32_computersystem -ComputerName $servername -ErrorAction SilentlyContinue | Select-Object Name,Manufacturer,systemname 
    
        $PhysicalRAM = (Get-WMIObject -class Win32_PhysicalMemory -ComputerName $servername -ErrorAction SilentlyContinue | Measure-Object -Property capacity -Sum | % {[Math]::Round(($_.sum / 1GB),2)}) 
    
       # $CPU =Get-WmiObject Win32_ComputerSystem -ComputerName $servername -ErrorAction SilentlyContinue | Select NumberOfLogicalProcessors, NumberOfProcessors,systemname,name 
        $Proccesor = Get-WmiObject –class Win32_processor -ComputerName $servername -ErrorAction SilentlyContinue | select systemname,Name,DeviceID,NumberOfCores,NumberOfLogicalProcessors, Addresswidth 
        $Disk = Get-WmiObject Win32_logicaldisk -ComputerName $servername -ErrorAction SilentlyContinue 
        @{Name="Drive Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}},
        @{Name="Drive Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}},
        @{Name="Drive Free pct";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}}
        $FreeSpace = [System.Math]::Round((($Disk.FreeSpace) / 1GB)) 
    
    
    	Write-Host "Computer Name:"$servername`n "Machine Type:"$servername.Manufacturer`n "RAM allocation:" $PhysicalRAM`n "NumberOfLogicalProcessors:" $Proccesor.NumberOfLogicalProcessor`n "NumberOfProcessors:" $Proccesor.NumberOfProcessors`n "CPU Type:" $Proccesor.Name`n$disk
    
    
    }

    outcome:

    Method invocation failed because [System.Object[]] doesn't contain a method named 'op_Division'.
    At line:20 char:5
    +     $FreeSpace = [System.Math]::Round((($Disk.FreeSpace) / 1GB))
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Division:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
     
    Computer Name: TREND
     Machine Type:  
     RAM allocation: 4
     NumberOfLogicalProcessors:  
     NumberOfProcessors:  
     CPU Type: Intel(R) Xeon(R) CPU           E5606  @ 2.13GHz 
    \\TREND\root\cimv2:Win32_LogicalDisk.DeviceID="A:" \\TREND\root\cimv2:Win32_LogicalDisk.DeviceID="C:" \\TREND\root\cimv2:Win32_LogicalDisk.DeviceID="D:"
    Again, thanks in advance for you helping!


    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    Monday, August 21, 2017 10:36 AM
  • I think you need to start by learning basic PowerShell.  You are pasting together things with no understanding of what they are or what they do.\

    Go back to the original code you copied and try to understand how it works.  Note that Write-Host cannot be used to output to HTML or to a Csv.

    help Export-CSv -Full

    help Select-Object -full

    read all including all examples.


    \_(ツ)_/

    Monday, August 21, 2017 10:47 AM
  • Hey,

    Surprise! fixed and now it works like charm,

    JRV, thank you unused comment.


    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    • Marked as answer by Meir Peleg Monday, August 21, 2017 11:42 AM
    Monday, August 21, 2017 11:16 AM
  • Hey,

    Surprise! fixed and now it works like charm,

    JRV, thank you unused comment.


    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    This is not an answer to anything.  It is just a statement.  An "answer" states a solution and is marked as a way to help others.  THis is a public forum and the results are used to help others.  Please tell us what you did to fix this.


    \_(ツ)_/

    Monday, August 21, 2017 3:50 PM
  • Delete

    • Proposed as answer by MeirPeleg Tuesday, August 22, 2017 9:57 AM
    • Edited by MeirPeleg Tuesday, August 22, 2017 9:58 AM TXT
    Tuesday, August 22, 2017 9:57 AM
  • Import-Module ActiveDirectory
    $ADComputer = Get-ADComputer -Filter {Enabled -eq $true -and Operatingsystem -like "Windows Server*"} -Properties * | select name, operatingsystem
    
    $Report = @()
    foreach ($comp in $ADComputer)
    {
    	$servername = $Comp.Name
    	$TestConnection = Test-Connection -ComputerName $servername -Count 1 -ErrorAction SilentlyContinue
    	if ($TestConnection)
    	{
    		$computerName = Get-WmiObject win32_computersystem -ComputerName $servername -ErrorAction SilentlyContinue
    		$OS = Get-WmiObject win32_operatingsystem -ComputerName $servername -ErrorAction SilentlyContinue
    		$Bios = Get-WmiObject win32_bios -ComputerName $servername -ErrorAction SilentlyContinue
    		$PhysicalRAM = (Get-WMIObject -class Win32_PhysicalMemory -ComputerName $servername -ErrorAction SilentlyContinue | Measure-Object -Property capacity -Sum | % { [Math]::Round(($_.sum / 1GB), 2) })
    		$CPU = Get-WmiObject Win32_ComputerSystem -ComputerName $servername -ErrorAction SilentlyContinue | Select NumberOfLogicalProcessors, NumberOfProcessors, systemname, name
    		$Proccesor = Get-WmiObject –class Win32_processor -ComputerName $servername -ErrorAction SilentlyContinue
    		$Disk = Get-WmiObject Win32_logicaldisk -ComputerName $servername -ErrorAction SilentlyContinue | select @{Name="Drive Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}},@{Name="Drive Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}},	@{Name="Drive Free pct";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}}
    		$Report += New-Object System.Management.Automation.PSObject -Property @{
    			'Server Name'    = $servername
    			'OS Name'		  = $OS.Caption
    			'Manufacturer'    = $computerName.Manufacturer
    			'Model'		      = $computerName.Model
    			'Serial Number'  = $Bios.SerialNumber
    			'Total RAM (GB)'= $PhysicalRAM
                'CPU - NumberOfLogicalProcessors' = $CPU.NumberOfLogicalProcessors
                'CPU - NumberOfProcessors' = $CPU.NumberOfProcessors
    
                		}
    	}
    
    	else
         
    
    	{
    		Write-Warning "Computer Name: $servername Isn't Available"
    
    	}
    $Report  | export-csv c:\x.CSV
    }
    
     
    



    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    • Marked as answer by Meir Peleg Tuesday, August 22, 2017 10:01 AM
    Tuesday, August 22, 2017 10:00 AM
  • Hi guys,

    I am working on PS script which knows to show me Server Details such as Name, CPU, and so on but I have a small problem while running DISK capacities in for each, can't get information correctly.

    Import-Module ActiveDirectory
    $ADComputer = Get-ADComputer -Filter {Enabled -eq $true -and Operatingsystem -like "Windows Server*"} -Properties * | select name, operatingsystem
    
    $Report = @()
    foreach ($comp in $ADComputer)
    {
    	$servername = $Comp.Name
    	$TestConnection = Test-Connection -ComputerName $servername -Count 1 -ErrorAction SilentlyContinue
    	if ($TestConnection)
    	{
    		$computerName = Get-WmiObject win32_computersystem -ComputerName $servername -ErrorAction SilentlyContinue
    		$OS = Get-WmiObject win32_operatingsystem -ComputerName $servername -ErrorAction SilentlyContinue
    		$Bios = Get-WmiObject win32_bios -ComputerName $servername -ErrorAction SilentlyContinue
    		$PhysicalRAM = (Get-WMIObject -class Win32_PhysicalMemory -ComputerName $servername -ErrorAction SilentlyContinue | Measure-Object -Property capacity -Sum | % { [Math]::Round(($_.sum / 1GB), 2) })
    		$CPU = Get-WmiObject Win32_ComputerSystem -ComputerName $servername -ErrorAction SilentlyContinue | Select NumberOfLogicalProcessors, NumberOfProcessors, systemname, name
    		$Proccesor = Get-WmiObject –class Win32_processor -ComputerName $servername -ErrorAction SilentlyContinue
    		$Disk = Get-WmiObject Win32_logicaldisk -ComputerName $servername -ErrorAction SilentlyContinue | select @{Name="Drive Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}},@{Name="Drive Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}},	@{Name="Drive Free pct";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}}
    		$Report += New-Object System.Management.Automation.PSObject -Property @{
    			'Server Name'    = $servername
    			'OS Name'		  = $OS.Caption
    			'Manufacturer'    = $computerName.Manufacturer
    			'Model'		      = $computerName.Model
    			'Serial Number'  = $Bios.SerialNumber
    			'Total RAM (GB)'= $PhysicalRAM
                'CPU - NumberOfLogicalProcessors' = $CPU.NumberOfLogicalProcessors
                'CPU - NumberOfProcessors' = $CPU.NumberOfProcessors
    
                		}
    	}
    
    	else
         
    
    	{
    		Write-Warning "Computer Name: $servername Isn't Available"
    
    	}
    $Report  | export-csv c:\XCSV
    }
    
     
    

    I am using this for getting disk details:

    $Disk = Get-WmiObject Win32_logicaldisk -ComputerName $servername -ErrorAction SilentlyContinue | select @{Name="Drive Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}},@{Name="Drive Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}},	@{Name="Drive Free pct";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}}
    
    My purpose is to get this useful disk info into my CV but it is not working good, do you have any advice for me?


    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    • Merged by jrv Tuesday, August 22, 2017 1:26 PM Duplicate
    Tuesday, August 22, 2017 10:08 AM
  • Hello Meir,

    well, that really depends on what you consider useful disk info.

    • Do you care about your physical disks? (win32_diskdrive)
    • Do you care about your OS partition? (win32_operatingsystem, win32_volume)
    • Do you care about your logical disks? (win32_logicaldisk)

    What is the information you actually need from this?

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Tuesday, August 22, 2017 11:36 AM
  • Hey dear Fred,

     actually, I need all of them,  I can get them as I want, Why are you asking that?  is that so difficult to get this?


    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    Tuesday, August 22, 2017 12:17 PM
  • Hello Meir,

    my issue is understanding what it is you need help with. This may be partially a language issue - some languages have a very different way of framing questions which is reflected in their usage of English and doesn't translate perfectly into the way I frame questions (not a native speaker either).

    Let's drill down a bit:

    1. Do you need help finding the information you need?
    2. Do you wish for advice, what disk/storage information is relevant?
    3. Do you need help in how to add more information to your output as extra columns?

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Tuesday, August 22, 2017 1:11 PM
  • Hey again.

    Sorry for the misunderstanding and let me explain to you my need. 

    I would like to get the disk information, what information are you asking?

    Volume Label, how many disks, free space, used spaces. if I am running it on a particular computer it works, but, while I combine it with my "foreach" command it does not work properly, it turns out untidy, the GB convert doesn't work and it is not clear.

    All other WMI queries work fine, just disk value not.

    Drill down:

    I want to get disk information (Volume Label, how many disks, free space, used spaces.) per machine into my outcome (CSV), which will know to run as foreach, because my script as you can see runs on AD servers.

    Hope I was clear.

    Thanks!


    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    Tuesday, August 22, 2017 1:38 PM
  • Hey again.

    Sorry for the misunderstanding and let me explain to you my need. 

    I would like to get the disk information, what information are you asking?

    Volume Label, how many disks, free space, used spaces. if I am running it on a particular computer it works, but, while I combine it with my "foreach" command it does not work properly, it turns out untidy, the GB convert doesn't work and it is not clear.

    All other WMI queries work fine, just disk value not.

    Drill down:

    I want to get disk information (Volume Label, how many disks, free space, used spaces.) per machine into my outcome (CSV), which will know to run as foreach, because my script as you can see runs on AD servers.

    Hope I was clear.

    Thanks!


    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    This  is what happens when you try to copy code and change it without sufficient understanding of the code or the technology.  Take some time to try to understand the code and the answers being given.  Once you understand the code the rest will make more sense.


    \_(ツ)_/

    Tuesday, August 22, 2017 1:43 PM
  • Thanks for your comment, learning code is needed, no doubt agree with you, but it is not mean that you can't type a code right? and try new things right? or even use PowerShell right?

    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    Tuesday, August 22, 2017 1:56 PM
  • Thanks for your comment, learning code is needed, no doubt agree with you, but it is not mean that you can't type a code right? and try new things right? or even use PowerShell right?

    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    Guessing without understanding will not help you learn code or understand answers.


    \_(ツ)_/

    Tuesday, August 22, 2017 2:01 PM
  • Hi Meir,

    it looks like what you want can be read from almost Win32_Volume alone:

    # Names
    $volumes = Get-WmiObject win32_volume
    $volumeLabels = $volumes | Select-Object -ExpandProperty Label
    $volumeNames = $volumes | Select-Object -ExpandProperty Name
    
    # Total Space numbers
    $totalFree = $volumes | Select-Object -ExpandProperty FreeSpace | Measure-Object -Sum | Select-Object -ExpandProperty Sum
    $totalCapacity = $volumes | Select-Object -ExpandProperty Capacity | Measure-Object -Sum | Select-Object -ExpandProperty Sum
    $totalOccupied = $totalCapacity - $totalFree
    
    # Nice looking total space numbers
    $totalFreeGB = "{0:N2}GB" -f ($totalFree / 1GB)
    $totalCapacityGB = "{0:N2}GB" -f ($totalCapacity / 1GB)
    $totalOccupiedGB = "{0:N2}GB" -f ($totalOccupied / 1GB)
    
    # Number of disks
    $diskCount = (Get-WmiObject win32_diskdrive | Measure-Object).Count

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Tuesday, August 22, 2017 2:52 PM
  • Hi Fred, 

    Thanks for your comment, this info I could get this info per a machine by myself, the  main problem is to combine it into my script, let me show you what I have done with your values:

    This is the script:

    mport-Module ActiveDirectory
    $ADComputer = Get-ADComputer -Filter {Enabled -eq $true -and Operatingsystem -like "Windows Server*"} -Properties * | select name, operatingsystem
    
    $Report = @()
    foreach ($comp in $ADComputer)
    {
    	$servername = $Comp.Name
    	$TestConnection = Test-Connection -ComputerName $servername -Count 1 -ErrorAction SilentlyContinue
    	if ($TestConnection)
    	{
    		$computerName = Get-WmiObject win32_computersystem -ComputerName $servername -ErrorAction SilentlyContinue
    		$OS = Get-WmiObject win32_operatingsystem -ComputerName $servername -ErrorAction SilentlyContinue
    		$Bios = Get-WmiObject win32_bios -ComputerName $servername -ErrorAction SilentlyContinue
    		$PhysicalRAM = (Get-WMIObject -class Win32_PhysicalMemory -ComputerName $servername -ErrorAction SilentlyContinue | Measure-Object -Property capacity -Sum | % { [Math]::Round(($_.sum / 1GB), 2) })
    		$CPU = Get-WmiObject Win32_ComputerSystem -ComputerName $servername -ErrorAction SilentlyContinue | Select NumberOfLogicalProcessors, NumberOfProcessors, systemname, name
    		$Proccesor = Get-WmiObject –class Win32_processor -ComputerName $servername -ErrorAction SilentlyContinue
    		#$Disk = Get-WmiObject Win32_logicaldisk -ComputerName $servername -ErrorAction SilentlyContinue | select @{Name="Drive Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}},@{Name="Drive Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}},	@{Name="Drive Free pct";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}}
             # Names
    $volumes = Get-WmiObject win32_volume -ComputerName $servername
    $volumeLabels = $volumes | Select-Object -ExpandProperty Label
    $volumeNames = $volumes | Select-Object -ExpandProperty Name
    
    # Total Space numbers
    $totalFree = $volumes | Select-Object -ExpandProperty FreeSpace | Measure-Object -Sum | Select-Object -ExpandProperty Sum
    $totalCapacity = $volumes | Select-Object -ExpandProperty Capacity | Measure-Object -Sum | Select-Object -ExpandProperty Sum
    $totalOccupied = $totalCapacity - $totalFree
    
    # Nice looking total space numbers
    $totalFreeGB = "{0:N2}GB" -f ($totalFree / 1GB)
    $totalCapacityGB = "{0:N2}GB" -f ($totalCapacity / 1GB)
    $totalOccupiedGB = "{0:N2}GB" -f ($totalOccupied / 1GB)
    
    # Number of disks
    $diskCount = (Get-WmiObject win32_diskdrive | Measure-Object).Count
    		$Report += New-Object System.Management.Automation.PSObject -Property @{
    			'Server Name'    = $servername
    			'OS Name'		  = $OS.Caption
    			'Manufacturer'    = $computerName.Manufacturer
    			'Model'		      = $computerName.Model
    			'Serial Number'  = $Bios.SerialNumber
    			'Total RAM (GB)'= $PhysicalRAM
                'CPU - NumberOfLogicalProcessors' = $CPU.NumberOfLogicalProcessors
                'CPU - NumberOfProcessors' = $CPU.NumberOfProcessors
                'Volumes'= $volumes
                'volume Labels' = $volumeLabels 
                'volumeNames' = $volumeNames
    
                		}
    
    
    	}
    
    	else
         
    
    	{
    		Write-Warning "Computer Name: $servername Isn't Available"
    
    	}
    #$Report  | export-csv c:\1.CSV

    While it runs I get this info:

    Total RAM (GB)                  : 48
    CPU - NumberOfProcessors        : 1
    Manufacturer                    : VMware, Inc.
    Server Name                     : VSQLLAB
    volumeNames                     : {\\?\Volume{e73d00ed-0317-11e7-81ee-806e6f6e6963}\, E:\, C:\, D:\}
    CPU - NumberOfLogicalProcessors : 4
    Volumes                         : {\\VSQLLAB\root\cimv2:Win32_Volume.DeviceID="\\\\?\\Volume{e73d00ed-0317-11e7-81ee-806e6f6e
                                      6963}\\", \\VSQLLAB\root\cimv2:Win32_Volume.DeviceID="\\\\?\\Volume{f44cbe3b-e651-46e8-980c
                                      -7727ca834261}\\", \\VSQLLAB\root\cimv2:Win32_Volume.DeviceID="\\\\?\\Volume{e73d00ee-0317-
                                      11e7-81ee-806e6f6e6963}\\", \\VSQLLAB\root\cimv2:Win32_Volume.DeviceID="\\\\?\\Volume{e73d0
                                      0f5-0317-11e7-81ee-806e6f6e6963}\\"}
    Serial Number                   : VMware-56 4d af 00 01 1b d1 20-8a 66 b5 d8 9c 4c d1 26
    Model                           : VMware Virtual Platform
    volume Labels                   : {System Reserved, DATA}
    OS Name                         : Microsoft Windows Server 2012 R2 Standard
    

    The out come in foreach doesn't work well.

    this is my problem, I was able to retrieve DISK information per a machine successfully, but in for each, it does not work well.


    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    Wednesday, August 23, 2017 5:44 AM
  • Some basic PowerShell methods:

    # Total Space numbers
    $totalFree = ($volumes | Measure-Object Freespace -Sum).Sum
    $totalCapacity = ($volumes | Measure-Object Capacity -Sum).Sum
    $totalOccupied = $totalCapacity - $totalFree

    Also formatting things correctly would make it easier to see what is happening in your code:

    Example:

    $report = Get-ADComputer -Filter { Enabled -eq $true -and Operatingsystem -like "Windows Server*" } -Properties * |
    	ForEach-Object {
    		$servername = $_.Name
    		if(Test-Connection -ComputerName $servername -Count 1 -Quiet){
    			
    			$OS = Get-WmiObject win32_operatingsystem -ComputerName $servername -ErrorAction SilentlyContinue
    			$Bios = Get-WmiObject win32_bios -ComputerName $servername -ErrorAction SilentlyContinue
    			$PhysicalRAM = (Get-WMIObject -class Win32_PhysicalMemory -ComputerName $servername -ErrorAction SilentlyContinue | Measure-Object -Property capacity -Sum | ForEach-Object { [Math]::Round(($_.sum / 1GB), 2) })
    			$CPU = Get-WmiObject Win32_ComputerSystem -ComputerName $servername -ErrorAction SilentlyContinue | Select-Object NumberOfLogicalProcessors, NumberOfProcessors, systemname, name
    			$Proccesor = Get-WmiObject –class Win32_processor -ComputerName $servername -ErrorAction SilentlyContinue
    
    			# Total Space numbers
    			$totalFree = ($volumes | Measure-Object Freespace -Sum).Sum
    			$totalCapacity = ($volumes | Measure-Object Capacity -Sum).Sum
    			$totalOccupied = $totalCapacity - $totalFree
    			
    			# Nice looking total space numbers
    			$totalFreeGB = "{0:N2}GB" -f ($totalFree / 1GB)
    			$totalCapacityGB = "{0:N2}GB" -f ($totalCapacity / 1GB)
    			$totalOccupiedGB = "{0:N2}GB" -f ($totalOccupied / 1GB)
    			
    			# Number of disks
    			$diskCount = (Get-WmiObject win32_diskdrive -ComputerName $servername).Count
    			[pscustomobject]@{
    				'Server Name'	  = $servername
    				'OS Name'		  = $OS.Caption
    				'Manufacturer'    = $computerName.Manufacturer
    				'Model'		      = $computerName.Model
    				'Serial Number'   = $Bios.SerialNumber
    				'Total RAM (GB)'  = $PhysicalRAM
    				'CPU - NumberOfLogicalProcessors' = $CPU.NumberOfLogicalProcessors
    				'CPU - NumberOfProcessors' = $CPU.NumberOfProcessors
    				'Volumes'		  = $volumes
    				'volume Labels'   = $volumes.Label
    				'volumeNames'	  = $volumes.Name
    				
    			}
    			
    		}else{
    			Write-Warning "Computer Name: $servername Isn't Available"
    		}
    	}


    \_(ツ)_/


    • Edited by jrv Wednesday, August 23, 2017 6:00 AM
    Wednesday, August 23, 2017 6:00 AM
  • this is my problem, I was able to retrieve DISK information per a machine successfully, but in for each, it does not work well.

    That's one of the basic issues with condensing multiple rows into values of a single row.

    The usual way to do this is either:

    • Use XML as a storage medium, as it can handle nested tables (but is unsuitable for display in Excel, since it is no longer a table)

    or

    • Combine multiple values into a single string value using -join
    # Instead of 
    'volumeNames' = $volumeNames
    
    # Do this
    'volumeNames' = $volumeNames -join ", "

    There's no perfect solution, alas.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Wednesday, August 23, 2017 6:16 AM
  • Yeah, it is very difficult probably because I don't understand the code enough.

    BYW, It still doesn't work, but guys thank your comment, I will try to back to script and try to realize why can't I get the disk details as expected.

    Thanks!


    Windows IT MVP 2015 /2016 www.PelegIT.co.il Thank you!

    Wednesday, August 23, 2017 6:56 AM