Need help to Complete the Script

So i found Scrip on online and ever since i am trying modify it to use it for my liking.

basically i am looking to create a function in Powershell which can list out some WMI objects like SystemName,PageFile,FreeSpace percentage... etc.. 

The below script runs but the second Loop of Get-WmiObject seems to be ignored.

I am very new and unable to spot the issue. 

function Get-DiskInfo {  
param(  
[String[]]$ComputerName = $ENV:ComputerName, 
[Switch]$LogOffline  
)  
  
$ComputerName | ForEach-Object {  
try { 
    $Computer = $_ 
     
    $Params = @{ 
        Namespace    = 'root\cimv2' 
        class        = 'win32_volume' 
        ComputerName = $Computer  
        ErrorAction  = 'Stop' 
        } 
   $Params2 = @{ 
        Namespace    = 'root\cimv2' 
        class        = 'win32_PhysicalMemory' 
        ComputerName = $Computer  
        ErrorAction  = 'Stop' 
        } 
     
    Get-WmiObject @Params  | ForEach-Object {  
            $hash=@{  
               ComputerName     = $_.__SERVER  
               FreeSpace        = ($_.freespace/$_.capacity * 100)
               Drive            = $_.Caption
               Name             = $_.Label
                           
               }  
           New-Object psobject -Property $hash 
         }#Foreach-Object(Adapter)  

    Get-WmiObject @Params2  | ForEach-Object {  
            $hash2=@{  
                ComputerName     = $_.__SERVER 
                TotalPhysical    = $_.Capacity 
                                
                }  
            New-Object psobject -Property $hash2
        }#Foreach-Object(Adapter) 
}#try 
catch { 
    Write-Warning -Message $_ 
    if ($LogOffline) 
    { 
        "$Computer is offline or not supported" >> "$home\desktop\Offline.txt" 
    } 

 
}#Foreach-Object(Computer)  
  
}#Get-DiskInfo

July 30th, 2015 6:21am

Hi,

see the answer from jrv here

"

When run as a script the two objects are merged and only the first is displayed.

Try this to see what I mean:

[PSCustomObject]@{Rec=;LoginName='One';DbLoginName=''}
[PSCustomObject]@{Rec=2;DBLoginName='Two'}

Place in file and run.  Look at the output then remove the DBLoginName from the first line and try again."
Free Windows Admin Tool Kit Click here and download it now
July 30th, 2015 6:50am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics