Script to maintain server inventory

Answered Script to maintain server inventory

  • Thursday, December 06, 2012 3:25 PM
     
     

    I found this very helpful script written by Jesse Hamrick. I have hacked it up to serve the purpose we need to inventory our servers. It was very helpful for the initial phase, but now I would like to retro fit it for maintaining the list for whenever a new server is built and deployed on a one by one basis.

    Currently I have got it to open up the inventory spreadsheet, and add the new server to the last line in the script. However, most servers we build out are "Spares" that are already in the list. I'd like to figure out a way to make the script search the list for the serial number of the server, and if it exists replace that row with the information, instead of adding it to the bottom. 

    Here is a copy of what I am working on now. The only function I care about at the moment is the Manual Entry.

    # ==============================================================================================

    # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2007

    # NAME: Server/Workstation Inventory (CompInv_v2.ps1)

    # AUTHOR: Jesse Hamrick
    # DATE  : 2/25/2009
    # Web : www.PowerShellPro.com
    # COMMENT: Script Inventories Computers and sends results to an excel file.

    # ==============================================================================================

    # ==============================================================================================
    # Functions Section
    # ==============================================================================================
    # Function Name 'WMILookup' - Gathers info using WMI and places results in Excel
    # ==============================================================================================
    $data = @()
    Function WMILookup {
    foreach ($StrComputer in $colComputers){
    $GenItems1 = gwmi Win32_ComputerSystem -Comp $StrComputer
    if(!$GenItems1){
    Write-Host “$StrComputer not found”
    continue
    }
    else{
            $GenItems2 = gwmi Win32_OperatingSystem -Comp $StrComputer
            $GenItems3 = gwmi Win32_PhysicalMemory  -Comp $StrComputer
    $SysItems1 = gwmi Win32_BIOS -Comp $StrComputer
    $NetItems = gwmi Win32_NetworkAdapterConfiguration -Comp $StrComputer |`
    where{$_.IPEnabled -eq "True"}
    }
    $ProcItems1 = gwmi Win32_Processor -Comp $StrComputer
            $details1 = Get-WmiObject Win32_NetworkLoginProfile -Comp $StrComputer | Sort -Descending LastLogon | Select * -First 1 | ? {$_.LastLogon -match "(\d{14})"} | % { New-Object PSObject -Property @{ Name=$_.Name ; LastLogon=[datetime]::ParseExact($matches[0], "yyyyMMddHHmmss", $null) } }
    $details2 = Get-Date;
            $time = $details2.TimeOfDay.TotalSeconds - $details1.LastLogon.TimeOfDay.TotalSeconds;
            $output = "$details2 - $env:username"
            $intRow = $Sheet1.UsedRange.Rows.Count + 1
            $iLO = New-Object System.Xml.XmlDocument
            $iLO.Load(“http://i$StrComputer.FQDN.com/xmldata?item=All”)
            
    # Populate General Sheet(1) with information
    foreach ($objItem in $GenItems1){
    $Sheet1.Cells.Item($intRow, 1) = $StrComputer
      }
            $Sheet1.Cells.Item($intRow, 2) = $Datacenter
            $Sheet1.Cells.Item($intRow, 3) = $Owner
            $Sheet1.Cells.Item($intRow, 4) = $Enviroment
            $Sheet1.Cells.Item($intRow, 5) = "Datacenter"
            
        
        foreach ($objitem in $iLO) {
            $Sheet1.Cells.Item($intRow, 6) = $objitem.RIMP.BLADESYSTEM.MANAGER.RACK
            $Sheet1.Cells.Item($intRow, 7) = $objitem.RIMP.BLADESYSTEM.MANAGER.ENCL 
            $Sheet1.Cells.Item($intRow, 8) = $objitem.RIMP.BLADESYSTEM.BAY 
             }
       $Sheet1.Cells.Item($intRow, 9) = "HP"
       }
    foreach ($objItem in $GenItems1){
    $Sheet1.Cells.Item($intRow, 10) = $objItem.Model
    }

    foreach ($objItem in $SysItems1){
    $Sheet1.Cells.Item($intRow, 11) = $objItem.SerialNumber



    foreach ($objItem in $GenItems2){
    $Sheet1.Cells.Item($intRow,12) = $objItem.Caption
    }
            
    foreach ($objItem in $GenItems1){
    $Sheet1.Cells.Item($intRow, 13) = $objitem.NumberOfProcessors
            $Sheet1.Cells.Item($intRow, 14) = $objitem.NumberOfLogicalProcessors
            $Sheet1.Cells.Item($intRow, 15) = $displayGB = [math]::round($objItem.TotalPhysicalMemory/1024/1024/1024, 0)
            }
        foreach ($user in $details1) {
            $Sheet1.Cells.Item($intRow, 16) = $user = $output
            }


       
       


    $null = $d.EntireColumn.AutoFit()  
    }
    }


    # =============================================================================================
    # Function Name 'ListComputers' - Enumerates ALL computer objects in AD
    # ==============================================================================================
    Function ListComputers {
    $strCategory = "computer"

    $objDomain = New-Object System.DirectoryServices.DirectoryEntry

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.Filter = ("(objectCategory=$strCategory)")

    $colProplist = "name"
    foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

    $colResults = $objSearcher.FindAll()

    foreach ($objResult in $colResults)
        {$objComputer = $objResult.Properties; $objComputer.name}
    }

    # ==============================================================================================
    # Function Name 'ListServers' - Enumerates ALL Servers objects in AD
    # ==============================================================================================
    Function ListServers {
    $strCategory = "computer"
    $strOS = "Windows*Server*"

    $objDomain = New-Object System.DirectoryServices.DirectoryEntry

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.Filter = ("(&(objectCategory=$strCategory)(OperatingSystem=$strOS))")

    $colProplist = "name"
    foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

    $colResults = $objSearcher.FindAll()

    foreach ($objResult in $colResults)
        {$objComputer = $objResult.Properties; $objComputer.name}
    }

    # ========================================================================
    # Function Name 'ListTextFile' - Enumerates Computer Names in a text file
    # Create a text file and enter the names of each computer. One computer
    # name per line. Supply the path to the text file when prompted.
    # ========================================================================
    Function ListTextFile {
    $strText = Read-Host "Enter the path for the text file"
    $colComputers = Get-Content $strText
    }

    # ========================================================================
    # Function Name 'SingleEntry' - Enumerates Computer from user input
    # ========================================================================
    Function ManualEntry {
    $colComputers = Read-Host "Enter Computer Name or IP" 
        $Datacenter = Read-Host "Enter Datacenter"
        $Owner = Read-Host "Enter Owner"
        $Enviroment = Read-Host "Enter Enviroment, PRD, DR, SPARE, ETC"
    }

    # ==============================================================================================
    # Script Body
    # ==============================================================================================
    $erroractionpreference = "SilentlyContinue"


    #Gather info from user.
    Write-Host "********************************"  -ForegroundColor Green
    Write-Host "Computer Inventory Script"  -ForegroundColor Green
    Write-Host "By: Jesse Hamrick"  -ForegroundColor Green
    Write-Host "Created: 04/15/2009"  -ForegroundColor Green
    Write-Host "Contact: www.PowerShellPro.com"  -ForegroundColor Green
    Write-Host "********************************"  -ForegroundColor Green
    Write-Host " "
    Write-Host "Admin rights are required to enumerate information."  -ForegroundColor Green
    Write-Host "Would you like to use an alternative credential?" -ForegroundColor Green
    $credResponse = Read-Host "[Y] Yes, [N] No"
    If($CredResponse -eq "y"){$cred = Get-Credential DOMAIN\USER}
    Write-Host " "
    Write-Host "Which computer resources would you like in the report?" -ForegroundColor Green
    $strResponse = Read-Host "[1] All Domain Computers, [2] All Domain Servers, [3] Computer names from a File, [4] Choose a Computer manually"
    If($strResponse -eq "1"){$colComputers = ListComputers | Sort-Object}
    elseif($strResponse -eq "2"){$colComputers = ListServers | Sort-Object}
    elseif($strResponse -eq "3"){. ListTextFile}
    elseif($strResponse -eq "4"){. ManualEntry}
    else{Write-Host "You did not supply a correct response, `
    Please run script again." -foregroundColor Red}
    Write-Progress -Activity "Getting Inventory" -status "Running..." -id 1

    #New Excel Application
    $strPath="$env:C:\inventory.xlsx"
    $a = new-Object -comobject Excel.Application  
    $a.visible = $True

    if (Test-Path $strPath)  

    {
    $b = $a.WorkBooks.Open($strPath)  
    $Sheet1 = $b.Worksheets.Item(1)  

    }  else {  
    # Create It  
    $b = $a.Workbooks.Add()  
    $Sheet1 = $b.Worksheets.Item(1)  
    $d = $Sheet1.UsedRange  
    $d.Interior.ColorIndex = 19  
    $d.Font.ColorIndex = 11  
    $d.Font.Bold = $True  
    }  

    # Assign each worksheet to a variable and
    # name the worksheet.

    #$Sheet1 = $Excel.Worksheets.Item(1)



    #Create Heading for General Sheet
    #$Sheet1.Cells.Item(1,1) = "Device_Name"
    #$Sheet1.Cells.Item(1,4) = "OS"
    #$Sheet1.Cells.Item(1,5) = "Processors"
    #$Sheet1.Cells.Item(1,6) = "Cores"
    #$Sheet1.Cells.Item(1,2) = "Serial"
    #$Sheet1.Cells.Item(1,3) = "Model"
    #$Sheet1.Cells.Item(1,7) = "Memory (GB)"
    #$Sheet1.Cells.Item(1,8) = "Last Logon"



    If($credResponse -eq "y"){WMILookupCred}
    Else{WMILookup}

    #Auto Fit all sheets in the Workbook
    foreach ($colorItem in $colSheets){
    $WorkBook = $colorItem.UsedRange
    $WorkBook.EntireColumn.AutoFit()
    clear
    }
    Write-Host "*******************************" -ForegroundColor Green
    Write-Host "The Report has been completed."  -ForeGroundColor Green
    Write-Host "*******************************" -ForegroundColor Green
    # ========================================================================
    # END of Script
    # ========================================================================
    • Edited by Cory D S Thursday, December 06, 2012 4:12 PM Sanitize
    •  

All Replies