PCIT - Powershell Samples - AD Computer Information

Create a Powershell script that will provide information on all computers on your domain.  Performance is not there as we are still working on that, but this is optimized for small networks where you need to gather computer information quickly and easily.

<p>PCIT - Powershell Samples - AD Computer Information</p>

 

Ever need to log information to a database or text file to audit every computer on a small to medium Active Directory network.  This will be a big time saver for you.  Below you will see how I built this using powershell, the next step is to schedule it to pull the information when you need it to a central location.  Great for Home and Small Business usages.  The first attempt is available here.

First create a function to pull all computer information

function GetAllComputers()
{
    #This is the array that will hold all your values
    $compVals = @()
    #Now we will get all of the computers in the Domain
    $comps = Get-ADComputer -Filter * -Property *
    #Lets cycle through all the computers in the domain
    foreach($comp in $comps)
    {
        #Get all infomration about each computer in an Active Directory Domain
        $compVal = ComputerInformation($comp.Name)
        #Add the computer information to the array
        $compVals+=$compVal
    }
    #output the array of AD computers to the screen
    $compVals | Format-Table -Wrap -AutoSize

    }

Then you need to create a function to pull the information you desire from each computer on the network.  This can be easily modified to fit custom needs using Powershell.

function  ComputerInformation ($computer)
{
#Create a new powershell object
$obj = New-Object -TypeName psobject
#Add the computer name as a property to all results
$obj|Add-Member -MemberType NoteProperty -Name ServerName -Value $computer
#Check if the computer is online using Powershell
if(Test-Connection $computer -Count 1 -Quiet)
{
    #Get properties that will be used to load the data table - SilentlyContinue command deals with errrs, you can turn this off if needed for debugging
    $Processor = Get-WmiObject -Class Win32_Processor -ComputerName $computer -ErrorAction SilentlyContinue
    $Bios = Get-WmiObject -Class Win32_BIOS -ComputerName $computer -ErrorAction SilentlyContinue
    $OSInfo = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer -ErrorAction SilentlyContinue
    #Note we have excluded all non physical drives
    $Disks = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" -ComputerName $computer -ErrorAction SilentlyContinue
    #Add the remaining properties to the object
    $obj|Add-Member -MemberType NoteProperty -Name Status -Value "Online"
    $obj|Add-Member -MemberType NoteProperty -Name Processor -Value $Processor.Name 
    $obj|Add-Member -MemberType NoteProperty -Name Bios -Value $Bios.Manufacturer
    $obj|Add-Member -MemberType NoteProperty -Name OS  -Value $OSInfo.Caption
    $dVal = ""
    #Cyclt thourh the disks using powershell
    foreach($d in $Disks)
    {
    try
    {
        $dVal +=  $d.DeviceID + "`t" + [Math]::Round($d.FreeSpace/$d.Size,2) + "% Free" +  "`r`n"
        }

        catch{
        $dVal +=  $d.DeviceID + "`t" +  "`r`n"
        }
        
    }  
    #Finalize the output for powershell
    $obj|Add-Member -MemberType NoteProperty -Name Disks -Value $dVal

    
}
else
{
    #create a blank offline powershell object
    $obj|Add-Member -MemberType NoteProperty -Name Status -Value "Offline"
    $obj|Add-Member -MemberType NoteProperty -Name Processor -Value ""
    $obj|Add-Member -MemberType NoteProperty -Name Bios -Value ""
    $obj|Add-Member -MemberType NoteProperty -Name OS  -Value ""
    $obj|Add-Member -MemberType NoteProperty -Name Disks -Value ""
}
return $obj

}

 

Below is the full code, you can use this to schedule a task on each computer using GPO or a sceduled task on a central server.

function  ComputerInformation ($computer)
{
#Create a new powershell object
$obj = New-Object -TypeName psobject
#Add the computer name as a property to all results
$obj|Add-Member -MemberType NoteProperty -Name ServerName -Value $computer
#Check if the computer is online using Powershell
if(Test-Connection $computer -Count 1 -Quiet)
{
    #Get properties that will be used to load the data table - SilentlyContinue command deals with errrs, you can turn this off if needed for debugging
    $Processor = Get-WmiObject -Class Win32_Processor -ComputerName $computer -ErrorAction SilentlyContinue
    $Bios = Get-WmiObject -Class Win32_BIOS -ComputerName $computer -ErrorAction SilentlyContinue
    $OSInfo = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer -ErrorAction SilentlyContinue
    #Note we have excluded all non physical drives
    $Disks = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" -ComputerName $computer -ErrorAction SilentlyContinue
    #Add the remaining properties to the object
    $obj|Add-Member -MemberType NoteProperty -Name Status -Value "Online"
    $obj|Add-Member -MemberType NoteProperty -Name Processor -Value $Processor.Name 
    $obj|Add-Member -MemberType NoteProperty -Name Bios -Value $Bios.Manufacturer
    $obj|Add-Member -MemberType NoteProperty -Name OS  -Value $OSInfo.Caption
    $dVal = ""
    #Cyclt thourh the disks using powershell
    foreach($d in $Disks)
    {
    try
    {
        $dVal +=  $d.DeviceID + "`t" + [Math]::Round($d.FreeSpace/$d.Size,2) + "% Free" +  "`r`n"
        }

        catch{
        $dVal +=  $d.DeviceID + "`t" +  "`r`n"
        }
        
    }  
    #Finalize the output for powershell
    $obj|Add-Member -MemberType NoteProperty -Name Disks -Value $dVal

    
}
else
{
    #create a blank offline powershell object
    $obj|Add-Member -MemberType NoteProperty -Name Status -Value "Offline"
    $obj|Add-Member -MemberType NoteProperty -Name Processor -Value ""
    $obj|Add-Member -MemberType NoteProperty -Name Bios -Value ""
    $obj|Add-Member -MemberType NoteProperty -Name OS  -Value ""
    $obj|Add-Member -MemberType NoteProperty -Name Disks -Value ""
}
return $obj

}
function GetAllComputers()
{
    #This is the array that will hold all your values
    $compVals = @()
    #Now we will get all of the computers in the Domain
    $comps = Get-ADComputer -Filter * -Property *
    #Lets cycle through all the computers in the domain
    foreach($comp in $comps)
    {
        #Get all infomration about each computer in an Active Directory Domain
        $compVal = ComputerInformation($comp.Name)
        #Add the computer information to the array
        $compVals+=$compVal
    }
    #output the array of AD computers to the screen
    $compVals | Format-Table -Wrap -AutoSize

    }

GetAllComputers
Last Updated: 12/28/2016 11:40:53 PM