Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Custom Object to function as an Array

$
0
0

Hello,

I am trying to create a custom object that just refuses to act as an array. Here is a skeleton copy of the whole script that i am working. It gives me the output when I call it one line at a time, but it seems to be over writing it self with every path. What I am looking to do is load ALL the data into an array so that I can work with it as an object (sort and send to screen and file). I am working with Powershell 2.0

Thank you for your help

 

Import-Module Active*

# Initialize variables
$Ping = $null
$Computers = $null
$namespace = "ROOT\CIMV2"
$classname = "Win32_Volume"
$OS = '*Server*'
$Domain = ".domain.com"

# Define Output Object
$OutputProperties = @{Name      = $Null
                      Drive     = $Null
                      FreeSpace = $Null
                      Capacity  = $Null					  
                     }
$Output = New-Object PSObject –Prop $OutputProperties

# Assign Values to print
Function SetOutput ($Name,$Drive,$FreeSpace,$Capacity) {        
	$Output.Name      =  $Name
    $Output.Drive     =  $Drive
    $Output.FreeSpace =  $FreeSpace
    $Output.Capacity =   $Capacity
    }

# Searches Active Directory for Servers
  $Computers = Get-ADComputer  -Filter {OperatingSystem -like $OS} -Properties CN, DNSHostName, OperatingSystem  

# Ping each Server to confirm  that it exists on the physical network
  ForEach ($Computer in $Computers) {
    $Ping = Test-Connection -CN $Computer.DNSHostname -Count 1 -Quiet
	$Computer.DNSHostname = $Computer.DNSHostname.TrimEnd($Domain).ToUpper()
    IF($Ping -match 'True') {
        Try {
            $DriveDetail = Get-WmiObject -Class $classname -ComputerName $Computer.DNSHostname -ErrorAction Stop | Select Name, FreeSpace, Capacity | Where {$_.Name -like "C:\"}
            # If the query worked and the Service exists
            if ($DriveDetail) { #Data Retrieved
                $FreeSpaceGB = [Math]::round($DriveDetail.FreeSpace / 1GB,2)
                $DriveSizeGB = [Math]::round($DriveDetail.capacity / 1GB,2)
                SetOutput -Name $Computer.DNSHostname -Drive $DriveDetail.Name -FreeSpace $FreeSpaceGB -Capacity $DriveSizeGB	
			}   
        }			
		Catch {} #Removed for now		
   }
 }
$Output

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles