Posts RSS Comments RSS 130 Posts and 202 Comments till now

Script to set the Description of Computer Object

I recently joined Experts-Exchange to help people understand the glory of Powershell and how useful it can be to your average Server Admin with little to no scripting or developer experience and trying to sell the idea you do NOT have to be a Developer or hard core scripter to utilize the power that is at your finger tips.

In one of my 200+ response post in the last week :) An admin wanted to collect data from all the servers/workstations in his environment. What was so fun to watch was how Powershell DESTROYED vbscript and I didnt really take an shortcuts.

Total Line Count Vbscript = 98
Total Line Count Powershell = 53

Effectively what it does is collect hardware info from the server/workstation and updates AD Descript field with the data as well as export the info to .CSV file

CPU 3.20GHz/2GB Ram/69,845GB/1.44/CDD

Here is the code.
Download here http://bsonposh.com/uploads/wordpress/2007/08/Set-ComputerDescription.ps1

$Servers = Get-Content "C:\computers.txt"
$myobjects = @()
foreach($server in $servers)
{
    write-host "Processing Server $server"
    $query = "Select statuscode FROM Win32_PingStatus WHERE Address=’$server’ and Timeout=300"
    $pingresult = Get-WmiObject -query $query
    if($pingresult.statuscode -eq 0)
    {
        $myobj = "" | Select Name,CPU,Memory,DiskSpace,CD,Floppy
        $myobj.Name = $server

        # Getting Computer Object
        $filter = "(&(objectcategory=computer)(cn=$server))"
        $ds = new-Object System.DirectoryServices.DirectorySearcher($filter)
        $comp = ($ds.findone()).GetDirectoryEntry()

        # CPU info
        $cpu = Get-WmiObject Win32_Processor -computername $server | select name -first 1 | foreach{$_.name}
        $cpu = (($cpu -replace "Intel" -replace ‘\(R\)’).trim() -replace "Xeon\(TM\)").trim()
        $myobj.CPU = $cpu

        # Memory
        $mem = (Get-WmiObject Win32_OperatingSystem -computername $server).TotalVisibleMemorySize
        $memory = $mem*1kb/1gb
        $memory = "{0}GB" -f [int]$memory
        $myobj.Memory = $memory

        # Disk
        $disks = Get-WmiObject Win32_LogicalDisk -computername $server | Where-Object{$_.DriveType -eq 3}
        $diskSz = (Get-WmiObject Win32_LogicalDisk -computername $server | Measure-Object -SUM size).SUM
        $diskSize = $diskSz/1gb
        $diskSize = "{0:n2}GB" -f $diskSize
        $myobj.DiskSpace = $diskSize
        if(Get-WmiObject Win32_FloppyDrive){$floppy = "1.44"}else{$floppy = "NoFloppy"}
        if(Get-WmiObject Win32_CDROMDrive){$CD = "CDD"}else{$CD = "NO CD"}

        # Formating String and setting the Description Field on the Servers
        $string = "{0}/{1} Ram/{2}/{3}/{4}" -f $cpu,$memory,$disksize,$floppy,$CD
        $comp.psbase.invokeSet("Description",$string)
        $comp.psbase.CommitChanges()
        $myobj.CD = $cd
        $myobj.floppy = $floppy

        # Adding custom object for export CSV
        $myobjects += $myobj
    }
    else
    {
        Write-Host "$Server is NOT pingable"
    }
}
$myobjects | export-csv C:\ComputerInfo.csv

No Responses to “Script to set the Description of Computer Object”

  1. on 27 Aug 2007 at 6:51 amaleksandar

    Great idea, a lot of things to learn, but it looks like you haven’t tested the code. :-(
    WMI classes Win32PingStatus, Win32Processor, Win32LogicalDisk… should be Win32_PingStatus, Win32_Processor, Win32_LogicalDisk…
    {$.name} should be {$_.name}
    {$.DriveType -eq 3} should be {$_.DriveType -eq 3}
    $string = “{0}/{1} Ram/{2}/{4}/{5}” -f $cpu,$memory,$disksize,$floppy,$CD should be $string = “{0}/{1} RAM/{2}/{3}/{4}” -f $cpu,$memory,$disksize,$floppy,$CD

    Regards,
    Aleksandar

  2. on 27 Aug 2007 at 7:35 amBrandon

    Thats for pointing that out Aleksander. I did test the code, but apparently I have a bug in my new highlight code that does NOT like _.

    I will work on the bug and change the string.

  3. on 27 Aug 2007 at 7:44 amBrandon

    I put a link to dl… try that one.

  4. on 27 Aug 2007 at 9:27 amaleksandar

    The script works with online machines only. It should be Address=

  5. on 27 Aug 2007 at 12:52 pmBrandon

    Im not sure I understand what you want… Are you suggesting that if the machine is not pingable to update the description field with not pingable?

  6. on 27 Aug 2007 at 4:55 pmBrandon

    BTW… I fixed the _ bug. Should be good now.

  7. on 28 Aug 2007 at 2:14 amaleksandar

    It should be Address=’$server’ in the query instead of Address=’$srv’. Right now it doesn’t check if machine is pingable or not, and everything works great when the machine is online, but you get an error “You cannot call a method on a null-valued expression.” when you run it against offline machine. I hope this is a better explanation. :-)

  8. on 28 Aug 2007 at 7:32 amBrandon

    thanks again… when I tested, I dont think any of the machines were down. Which is why I missed that.

Trackback this post | Feed on Comments to this post

Leave a Reply

CAPTCHA image