Pimp My Function

PashPash Member Posts: 1,601 ■■■■■□□□□□
Well I thought this might be an interesting one to start the New Year off....or not for some :p. I am gonna post a function below from my pshell profile that I work from. It's not finished yet but any improvments or suggestions always welcome. Maybe others can do the same!
function GETPCINFO {

$cred = Get-Credential

$computers = Get-Content "C:\scripts\computers.txt"

foreach ($computer in $computers)
{
#### Set Variables
$user = $env:USERNAME

#### Win32 class short name assignment - add -credential $cred where needed
$OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer 
$Bios = Get-WmiObject -Class Win32_BIOS -ComputerName $computer 
$CS = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer 
$CPU = Get-WmiObject -Class Win32_Processor -ComputerName $computer
$HDDSC = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $computer -Filter "name='C:'"

#### Set Variable information with short name
$OSRunning = $OS.caption + " " + $OS.OSArchitecture + " SP " + $OS.ServicePackMajorVersion
$DomainRole = $CS.DomainRole
$TotalAvailMemory = $OS.totalvisiblememorysize/1kb 
$TotalVirtualMemory = $OS.totalvirtualmemorysize/1kb
$TotalFreeMemory = $OS.FreePhysicalMemory/1kb
$TotalFreeVirtualMemory = $OS.FreeVirtualMemory/1kb
$TotalMem = "{0:N2}" -f $TotalAvailMemory
$TotalVirt = "{0:N2}" -f $TotalVirtualMemory
$FreeMem = "{0:N2}" -f $TotalFreeMemory
$FreeVirtMem = "{0:N2}" -f $TotalFreeVirtualMemory
$date = Get-Date
$uptime = $OS.ConvertToDateTime($OS.lastbootuptime)
$BiosVersion = $Bios.Manufacturer + " " + $Bios.SMBIOSBIOSVERSION + " " + $Bios.ConvertToDateTime($Bios.Releasedate)
$CPUInfo = $CPU.Name + " & has " + $CPU.NumberOfCores + " Cores & the FSB is " + $CPU.ExtClock + " Mhz"
$CPULOAD = $CPU.LoadPercentage

echo "#### INFO FOR $computer which is running $OSRunning#### `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt"
if (($DomainRole -eq "0") -or ($DomainRole -eq "1"))
{
echo "This system is a Workstation. `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
}
elseif (($DomainRole -eq "2") -or ($DomainRole -eq "3"))
{
echo "This system is a Member Server. `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
}
elseif (($DomainRole -eq "4") -or ($DomainRole -eq "5"))
{
echo "This system is a Domain Controller. `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
}
else
{
echo "Unknown System Type. `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
}
echo "######## Memory Information ######## `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
echo "Total Physical Memory = $TotalMem MB / Total Free Physical Memory = $FreeMem MB" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append 
echo "Total Virtual Memory = $TotalVirt MB / Total Free Virtual Memory = $FreeVirtMem MB `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
echo "######## Last Boot Time for this PC ######## `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
echo "Last Boot Time $uptime `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
echo "######## Bios Version ######## `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
echo "The Bios Version is $BiosVersion `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
echo "######## CPU INFO ########`n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
echo "The CPU is a $CPUInfo `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
echo "The CPU Load Percentage for the last minute of activity is $CPULOAD % `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
echo "######## HDD Partition Information ######## `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append

### Need way of reducing code for following ###
$freec = [Math]::Round($HDDSC.freespace/1gb)
$sizec = [Math]::Round($HDDSC.size/1gb)
echo "C: `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
echo "Total Space: $sizec GB" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append
echo "Free Space: $freec GB `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append

echo "######## Installed Software ######## `n" | Out-File "C:\Scripts\SERVERINFO\$computer.txt" -Append

}

}

I will probably move the output to excel at some point, when I have everything there I need. PS. The get credential is not being used at the moment coz I am testing on a local machine running shell as admin, so it is not required.
DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.

Comments

  • PashPash Member Posts: 1,601 ■■■■■□□□□□
    Well this might be a bit of a dead horse now, but hey ill still give it a go.
    $ErrorActionPreference = "SilentlyContinue"
    
    #### Spreadsheet Location
    $strPath = "C:\Scripts\SERVERINFO\serverinfo.xls"
    
    $onerror = "SilentlyContinue"
    $a = New-Object -comobject Excel.Application
    $a.Visible = $True
    
    $b = $a.Workbooks.Add()
    $c = $b.Worksheets.Item(1)
    
    $c.Cells.Item(1,1) = "Machine Name"
    $c.Cells.Item(1,2) = "OS Running"
    $c.Cells.Item(1,3) = "Domain Role"
    $c.Cells.Item(1,4) = "Total Physical Memory"
    $c.Cells.Item(1,5) = "Total Free Physical Memory"
    $c.Cells.Item(1,6) = "Total Virtual Memory"
    $c.Cells.Item(1,7) = "Total Free Virtual Memory"
    $c.Cells.Item(1,[IMG]https://us.v-cdn.net/6030959/uploads/images/smilies/icon_cool.gif[/IMG] = "Last Boot Time"
    $c.Cells.Item(1,9) = "Bios Version"
    $c.Cells.Item(1,10) = "CPU Info"
    $c.Cells.Item(1,11) = "Disk Info"
    
    $d = $c.UsedRange
    $d.Interior.ColorIndex = 19
    $d.Font.ColorIndex = 11
    $d.Font.Bold = $True
    $d.EntireColumn.AutoFit()
    
    $intRow = 2
    $cred = Get-Credential
    ### For Use Later with Quest AD Cmdlets installed
    ### get-QADcomputer | format-table -hidetableheaders -property Name >> C:\Scripts\computers.txt
    $computers = Get-Content "C:\scripts\computers.txt"
    
    
    foreach ($computer in $computers)
    {
    #### Set Variables
    $user = $env:USERNAME
    
    #### Win32 class short name assignment - add -credential $cred where needed
    TRY {
    $OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer
    $Bios = Get-WmiObject -Class Win32_BIOS -ComputerName $computer 
    $CS = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer 
    $CPU = Get-WmiObject -Class Win32_Processor -ComputerName $computer 
    $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
    
    
    
    #### Set Variable information with short name
    $OSRunning = $OS.caption + " " + $OS.OSArchitecture + " SP " + $OS.ServicePackMajorVersion 
    $DomainRole = $CS.DomainRole
    $TotalAvailMemory = $OS.totalvisiblememorysize/1kb 
    $TotalVirtualMemory = $OS.totalvirtualmemorysize/1kb
    $TotalFreeMemory = $OS.FreePhysicalMemory/1kb
    $TotalFreeVirtualMemory = $OS.FreeVirtualMemory/1kb
    $TotalMem = "{0:N2}" -f $TotalAvailMemory
    $TotalVirt = "{0:N2}" -f $TotalVirtualMemory
    $FreeMem = "{0:N2}" -f $TotalFreeMemory
    $FreeVirtMem = "{0:N2}" -f $TotalFreeVirtualMemory
    $date = Get-Date 
    $uptime = $OS.ConvertToDateTime($OS.lastbootuptime)
    $BiosVersion = $Bios.Manufacturer + " " + $Bios.SMBIOSBIOSVERSION + " " + $Bios.ConvertToDateTime($Bios.Releasedate)
    $CPUInfo = $CPU.Name + " & has " + $CPU.NumberOfCores + " Cores & the FSB is " + $CPU.ExtClock + " Mhz"
    $CPULOAD = $CPU.LoadPercentage
    if (($DomainRole -eq "0") -or ($DomainRole -eq "1"))
    {
    $Role = "Work Station"
    }
    elseif (($DomainRole -eq "2") -or ($DomainRole -eq "3"))
    {
    $Role = "Member Server"
    }
    elseif (($DomainRole -eq "4") -or ($DomainRole -eq "5"))
    {
    $Role = "Domain Controller"
    }
    else
    {
    $Role = "Unknown"
    }
    }
    CATCH
    {
    $pcnotfound = "true"
    }
    #### Pump Data to Excel
    if ($pcnotfound -eq "true")
    {
    $c.Cells.Item($intRow, 1) = "PC Not Found"
    }
    else
    {
    $c.Cells.Item($intRow, 1) = $computer
    $c.Cells.Item($intRow, 2) = $OSRunning
    $c.Cells.Item($intRow, 3) = $Role
    $c.Cells.Item($intRow, 4) = "$TotalMem MB"
    $c.Cells.Item($intRow, 5) = "$FreeMem MB"
    $c.Cells.Item($intRow, 6) = "$TotalVirt MB"
    $c.Cells.Item($intRow, 7) = "$FreeVirtMem MB"
    $c.Cells.Item($intRow, [IMG]https://us.v-cdn.net/6030959/uploads/images/smilies/icon_cool.gif[/IMG] = $uptime
    $c.Cells.Item($intRow, 9) = $BiosVersion
    $c.Cells.Item($intRow, 10) = $CPUInfo
    
    $driveStr = ""
    foreach($drive in $drives)
    {
    $size1 = $drive.size / 1GB
    $size = "{0:N2}" -f $size1
    $free1 = $drive.freespace / 1GB
    $free = "{0:N2}" -f $free1
    $freea = $free1 / $size1 * 100
    $freeb = "{0:N2}" -f $freea
    $ID = $drive.DeviceID
    $driveStr += "$ID = Total Space: $size GB / Free Space: $free GB / Free (Percent): $freeb % ` " 
    }
    $c.Cells.Item($intRow, 11) = $driveStr
    }
    
    $d.EntireColumn.AutoFit()
    
    $intRow = $intRow + 1
    $pcnotfound = "false"
    }
    
    IF(Test-Path $strPath)
    {
    Remove-Item $strPath
    $a.ActiveWorkbook.SaveAs($strPath)
    }
    ELSE
    {
    $a.ActiveWorkbook.SaveAs($strPath)
    }
    

    Improvements are:-

    - Added pump to excel format
    - Added Try and catch to grab any errors with unfound desktops or access denied scenarios. Probably a bit lazy and slow, but it works.
    - Added Quest AD cmdlet to pull computer list from AD. Funnily enough, the hidetableheader option leaves a line space in the text file, I still havent found a way of automatically rectifying that.
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • RobertKaucherRobertKaucher Member Posts: 4,298
    I am going to pilfer it for my own.... Mhhuhuhuhuhahhaha!

    Good post! Thanks!
  • PashPash Member Posts: 1,601 ■■■■■□□□□□
    Cheers Rob.

    I don't mind anyone using it obviously, but I just thought these sort of threads are good for people who are slow at learning like me. There might be improvements or techniques I don't know about which can help.
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
Sign In or Register to comment.