Server Inventory script

A Server inventory script that generates a HTML Output

The current script contains only the following items.

  • Shares
  • Administrators
  • Network info
  • CPU Info

More comming soon…

##############################################################################################
#	PowerShell Script.
##############################################################################################
#	Name : ServerInventory.ps1
#	Version : 1.0.0
#	Author : Wessel van Sandwijk
#	Usage : 
#	ServerInventory.ps1 -Path "c:\Scripts" -Servers <Server1>,<Server2> -logfile <Logname>.log
##############################################################################################
#
#	Lists all enabled network adapters and their settings for defined list of machines.
#	Lists all Administators of defined list of machines
#	Lists all Shares and there Descriptions for defined list of machines
#	Outputs all to CSV, HTML and Combined HTML file
#	Prepared by Wessel van Sandwijk. September 2013.
#
###############################################################################################
#	Versions :
#	1.0.0	25-Sep-2013	Initial Version
###############################################################################################

Param(
  [string]$path,
  [array]$servers,
  [string]$Logfile,
  [string]$inputfile
) 

if (!$servers){
    write-host "Provide Servers to Check (-servers <server1>,<server2>,...)"
    logwrite "Provided Manual Server : $Servers"
    }
#if (!$servers){	Write-host "Servers is empty",	Exit}
#if (!$path) { Write-Host "Path is empty", exit}

############################################
# HTML Contents (Head and Body)
############################################

$htmlhead = "<style>"
$htmlhead = $htmlhead + "BODY{background-color:green;}"
$htmlhead = $htmlhead + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$htmlhead = $htmlhead + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}"
$htmlhead = $htmlhead + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod}"
$htmlhead = $htmlhead + "</style>"

$body = @"
<p><a href="http://www.useful-it.nl" target=_blank>
<img src="http://www.useful-it.nl/wp-content/uploads/2013/08/cropped-Useful-IT-trans2.png" alt="Useful-it.nl Website" title="Visit site : http://www.useful-it.nl" width=200 style="float: right;"></a></p>
<br>
"@

############################################
# Functions
############################################

Function LogWrite {
    param([string]$logstring)

    Add-Content $path\$Logfile -Value $logstring
}

function Get-Networking {
$totalNetwork = @()	
	foreach ($comp in $servers){
	    $NIC = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $comp -Filter "IPEnabled='TRUE'" | select DNSHostName, Description, MACAddress, DHCPenabled, IPAddress, IPSubnet, DefaultIPGateway, DNSServerSearchOrder
	    $obj=New-Object PSObject
	    $obj | Add-Member -MemberType NoteProperty -Name "ServerName" -Value $comp
	    $obj | Add-Member -MemberType NoteProperty -Name "Description" -Value $NIC.Description
	    $obj | Add-Member -MemberType NoteProperty -Name "MACAddress" -Value $NIC.MACAddress
	    $obj | Add-Member -MemberType NoteProperty -Name "DHCPenabled" -Value $NIC.DHCPenabled
	    $obj | Add-Member -MemberType NoteProperty -Name "IPAddress" -Value ([string]::Join(', ',$NIC.IPAddress))
	    $obj | Add-Member -MemberType NoteProperty -Name "IPSubnet" -Value ([string]$NIC.IPSubnet)
	    $obj | Add-Member -MemberType NoteProperty -Name "DefaultIPGateway" -Value ([string]$NIC.DefaultIPGateway)
	    $obj | Add-Member -MemberType NoteProperty -Name "DNSServerSearchOrder" -Value ([string]::Join(', ',$NIC.DNSServerSearchOrder))
	    $totalNetwork += $obj
	}
	$totalNetwork
	Logwrite "Get-Network Run"
}

function Get-Shares { 
	$totalShares = @()

	foreach ($comp in $servers){
	    $objshares = Get-WmiObject -Class Win32_Share -ComputerName $comp
	    foreach ($objshare in $objshares){
	        $obj=$null
			$obj=New-Object PSObject
	        $obj | Add-Member -MemberType NoteProperty -Name "ServerName" -Value $comp
	        $obj | Add-Member -MemberType NoteProperty -Name "ShareName" -Value $objshare.Name
	        $obj | Add-Member -MemberType NoteProperty -Name "SharePath" -Value $objshare.Path
	        $obj | Add-Member -MemberType NoteProperty -Name "Description" -Value $objshare.Description
	        $totalShares += $obj
	        }
	    }
		$totalShares
		logwrite "Get-Shares Run"
}

Function Get-CPUInfo {
    $cpuinfo = @()
    Foreach ($Comp in $servers) {
        $objcpu = Get-WmiObject -class "Win32_Processor" -namespace "root/CIMV2" -computername $Comp 
        foreach ($objItem in $objcpu) {
            $obj=New-Object PSObject
            $obj | Add-Member -MemberType NoteProperty -Name "Computer" -Value $objItem.PSComputerName
            $obj | Add-Member -MemberType NoteProperty -Name "CPU ID" -Value $objItem.DeviceID
            $obj | Add-Member -MemberType NoteProperty -Name "CPU Model" -Value $objItem.Name
            $obj | Add-Member -MemberType NoteProperty -Name "CPU Cores" -Value $objItem.NumberOfCores
            $obj | Add-Member -MemberType NoteProperty -Name "CPU Max Speed" -Value $objItem.MaxClockSpeed
            $obj | Add-Member -MemberType NoteProperty -Name "CPU Status" -Value $objItem.Status
            $CPUInfo += $obj
            }
        }
        $CPUInfo
        Logwrite "Get-CPUInfo run"
}

function Get-Admins {

    Foreach ($Comp in $Servers){
        $TotalAdmins = @()
	    $Online = Test-Connection -ComputerName $Comp -Quiet
        if ($Online -eq "True"){
            $GroupName = Get-WmiObject win32_group -ComputerName $Comp | ? {$_.SID -eq 'S-1-5-32-544'} | select name -ExpandProperty name
            $LocalGroup =[ADSI]"WinNT://$Comp/$GroupName"
            $GroupMembers = @($LocalGroup.psbase.Invoke("Members"))
            $Members = $GroupMembers | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
            foreach ($Member in $Members){
            $obj = New-Object System.Object
            $obj | Add-Member -MemberType NoteProperty -Name "Computer" -Value $Comp
            $obj | Add-Member -MemberType NoteProperty -Name "AdminGroup" -Value $GroupName
            $obj | Add-Member -MemberType NoteProperty -Name "AdminGroupMembers" -Value $Member
            $totalAdmins +=$obj
            }
        }
	    $TotalAdmins
	    Logwrite "Get-Admins run"
    }

}

############################################
# Script Start
############################################

$Network = Get-Networking
$Shares = Get-Shares
$Admins = Get-Admins
$Cpu = Get-CPUInfo

############################################
#Export to CSV
############################################

$Shares | Export-Csv -NoTypeInformation -Encoding 'Unicode' -Path "$path\Shares.csv"
$Network | Export-Csv -NoTypeInformation -Encoding 'Unicode' -Path "$path\Network.csv"
$Admins | Export-Csv -NoTypeInformation -Encoding 'Unicode' -Path "$path\Admins.csv"
$CPU | Export-Csv -NoTypeInformation -Encoding 'Unicode' -Path "$path\CpuInfo.csv"
logwrite "Export to CSV Run"

############################################
#Export to HTML (individual)
############################################

$Network | ConvertTo-HTML -Head $htmlhead -Body "$body <center><h1>Networking</h1></center>" -title "Networking" | Out-File $path\Networking.htm
$Shares | ConvertTo-HTML -Head $htmlhead -Body "$body <h1>Shares</h1>" -title "Shares" | Out-File $path\Shares.htm
$Admins | ConvertTo-HTML -Head $htmlhead -Body "$body <h1>Admins</h1>" -title "Admins" | Out-File $path\Admins.htm
$CPU | ConvertTo-HTML -Head $htmlhead -Body "$body <h1>CPUInfo</h1>" -title "CPU Info" | Out-File $path\CPUInfo.htm
Logwrite "Export to HTML (Individual)"

############################################
#Export to HTML (Combined)
############################################

$null | ConvertTo-Html -Head $htmlhead -body $body -title "Servers Overview" | Out-File $path\Combined.htm
$Network | ConvertTo-HTML -Body "<center><h1>Networking</h1></center>" | Out-File -Append $path\Combined.htm
$Shares | ConvertTo-HTML -Body "<h1>Shares</h1>" | Out-File -Append $path\Combined.htm
$Admins | ConvertTo-HTML -Body "<h1>Admins</h1>" | Out-File -Append $path\Combined.htm
$CPU | ConvertTo-HTML -Body "<h1>CPUInfo</h1>" | Out-File -Append $path\Combined.htm
LogWrite "Export to HTML (Combined Run)"

Copy the contents into a empty file or click the dowload button : [wpdm_file id=2]

About the Author

wvsandwijk

I am a Technical Specialist - Cloud Infrastructures from the Netherlands. My Skills are mainly PowerShell, VMWare, Hyper-V, Microsoft System Center products and Windows Azure Pack. I work for a company in Utrecht The Netherlands.

No Comments

Comments are closed.