Вы находитесь на странице: 1из 10

clear

$Host.UI.RawUI.WindowTitle = "System Inventory Tool Designed by Chaitanyakumar G


utala"
Write-Host `n
Write-Host Author: Chaitanyakumar Gutala -BackgroundColor black -ForegroundColor
white
Write-Host Contact: powershellguy@outlook.com -BackgroundColor Black -Foregroun
dColor white
Write-Host `n
Write-Host This powershell script collects windows system details and writes it
to an excel file. -ForegroundColor yellow
Write-Host `n
Write-Host Prerequisites: -ForegroundColor yellow
Write-Host `n
Write-Host "1) Admin rights on your servers and excel application installed in t
his local machine." -ForegroundColor yellow
write-host `n
Write-Host "2) MS Office version 2010 or lower need to be installed in the machi
ne you execute this script." -ForegroundColor yellow
write-host `n
Write-Host "If you dont have the prerequisites, please cancel this script by clo
sing this window and run it when you have all the prerequisites." -ForegroundCol
or yellow
write-host `n
Read-Host Press Enter to continue
Write-Host "Choose hostnames(servernames list) text file" -ForegroundColor yello
w
################################################################################
######
#File Prompt
################################################################################
######
function Read-OpenFileDialog([string]$WindowTitle, [string]$InitialDirectory, [s
tring]$Filter = "All files (*.*)|*.*", [switch]$AllowMultiSelect)
{
Add-Type -AssemblyName System.Windows.Forms
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.Title = $WindowTitle
if ($InitialDirectory -eq $Null) { $openFileDialog.InitialDirectory = $Initi
alDirectory }
$openFileDialog.Filter = $Filter
if ($AllowMultiSelect) { $openFileDialog.MultiSelect = $true }
$openFileDialog.ShowHelp = $true
# Without this line the ShowDialog() fun
ction may hang depending on system configuration and running from console vs. IS
E.
$openFileDialog.ShowDialog() > $null
if ($AllowMultiSelect) { return $openFileDialog.Filenames } else { return $o
penFileDialog.Filename }
}
$var = Read-OpenFileDialog("Select hostnames file:","c:\")
$filepath = Get-Content $var
Start-Sleep -s 3
################################################################################
######
#Folder Prompt

################################################################################
######
Write-Host `n
Write-Host Choose folder to create output files -ForegroundColor yellow
function Read-FolderBrowserDialog([string]$Message, [string]$InitialDirectory)
{
$app = New-Object -ComObject Shell.Application
$folder = $app.BrowseForFolder(0, $Message, 0, $InitialDirectory)
if ($folder) { return $folder.Self.Path } else { return '' }
}
$directory = Read-FolderBrowserDialog ("Select your folder to save output files"
)
################################################################################
######
#get only date and time
$a = Get-Date
$Date = $a.ToShortDateString()
$Time = $a.ToShortTimeString()
#Creating new excel object
start-sleep -Seconds 3
Write-Host `n
Write-Host "Please wait.. " -ForegroundColor magenta
$excel = New-Object -ComObject excel.application
$excel.visible = $False
$workbook = $excel.Workbooks.Add()
$workbook.Worksheets.Add() | Out-Null
$workbook.Worksheets.Add() | Out-Null
$workbook.Worksheets.Add() | Out-Null
$excel.DisplayAlerts = $False
$excel.Rows.Item(1).Font.Bold = $true
$value1= $workbook.Worksheets.Item(1)
$value1.Name = 'Server Information'
$value1.Cells.Item(1,1) = "Machine Name"
$value1.Cells.Item(1,2) = "OS Running"
$value1.Cells.Item(1,3) = "Total Physical Memory"
$value1.Cells.Item(1,4) = "Last Boot Time"
$value1.Cells.Item(1,5) = "Bios Version"
$value1.Cells.Item(1,6) = "Serial Number"
$value1.Cells.Item(1,7) = "CPU Name"
$value1.Cells.Item(1,8) = "CPU Count"
$value1.Cells.Item(1,9) = "CPU Max Speed"
$value1.Cells.Item(1,10) = "Disk Info"
$value1.Cells.Item(1,11) = "System Model"
$value1.Cells.Item(1,12) = "Manufacturer"
$value1.Cells.Item(1,13) = "Description"
$value1.Cells.Item(1,14) = "PrimaryOwnerName"
$value1.Cells.Item(1,15) = "Systemtype"
#Network details
$sheet2 = $workbook.worksheets.item(2)
$sheet2.Name = 'Network Details'
$sheet2.Rows.Item(1).Font.Bold = $true
$sheet2.Cells.Item(1,1) = "Machine Name"
$sheet2.Cells.Item(1,2) = "Description"
$sheet2.Cells.Item(1,3) = "IPAddress"

$sheet2.Cells.Item(1,4)
$sheet2.Cells.Item(1,5)
$sheet2.Cells.Item(1,6)
$sheet2.Cells.Item(1,7)
$sheet2.Cells.Item(1,8)

=
=
=
=
=

"DHCPServer"
"DefaultIPGateway"
"DNSDomain"
"DHCPEnabled"
"MACAddress"

#Pagefile details
$sheet3 = $workbook.worksheets.item(3)
$sheet3.Name = 'PageFile Details'
$sheet3.Rows.Item(1).Font.Bold = $true
$sheet3.Cells.Item(1,1) = "Machine Name"
$sheet3.Cells.Item(1,2) = "Path"
$sheet3.Cells.Item(1,3) = "Initial PageFile"
$sheet3.Cells.Item(1,4) = "Maximum PageFile"
$sheet3.Cells.Item(1,5) = "Total Memory in GB"
#Installed apps
$sheet5 = $workbook.worksheets.item(4)
$sheet5.Name = 'Installed Apps'
$sheet5.Rows.Item(1).Font.Bold = $true
#miscellenious details
$sheet4 = $workbook.worksheets.item(5)
$sheet4.Name = 'Miscellenious Details'
$sheet4.Rows.Item(1).Font.Bold = $true
$sheet4.Cells.Item(1,1) = "Machine Name"
$sheet4.Cells.Item(1,2) = "TimeZone"
$sheet4.Cells.Item(1,3) = "StandardName"
$sheet4.Cells.Item(1,4) = "BootConfiguration"
$sheet4.Cells.Item(1,5) = "Administrators"
#Paged Pool Details
$sheet6 = $workbook.worksheets.item(6)
$sheet6.Name = 'Paged Pool Memory Details'
$sheet6.Rows.Item(1).Font.Bold = $true
$sheet6.cells.item(1,1) = "Server Name"
$sheet6.cells.item(1,2) = "PoolUsageMaximum configured(Yes or No)"
$row = 2
$sheet2row = 2
$Page2row = 2
$misc1row = 2
$serverCount123 = 2
$sheet6column = 2
$sheet6row = 2
$appscolumn = 1
$serverCount = 1
write-host `n
Write-Host "Excel application created successfully. Writing data to rows and col
umns.." -ForegroundColor magenta
foreach ($computer in $filepath) {
write-host `n
if (Test-Connection -ComputerName $computer -Quiet)
{
write-host Processing server $computer -ForegroundColor yellow
$column = 1

$Opt = New-CimSessionOption -Protocol Dcom


$Session = New-CimSession -ComputerName $computer -SessionOption $Opt
$OS = Get-CimInstance -Class Win32_OperatingSystem CimSession $session
$OS1 = Get-CimInstance -class Win32_PhysicalMemory CimSession $session |MeasureObject -Property capacity -Sum
$Bios = Get-CimInstance -Class Win32_BIOS CimSession $session
$SerialNumber = $Bios | Select-Object -ExpandProperty serialnumber
$CS = Get-CimInstance -Class Win32_ComputerSystem CimSession $session
$CPU = Get-CimInstance -Class Win32_Processor CimSession $session
$drives = Get-CimInstance -Class Win32_LogicalDisk CimSession $session
$OSRunning = $OS.caption + " " + $OS.OSArchitecture + " SP " + $OS.ServicePackM
ajorVersion
$TotalAvailMemory = ([math]::round(($OS1.Sum / 1GB),2))
$TotalMem = "{0:N2}" -f $TotalAvailMemory
$date = Get-Date
#Posh 3 directly gives date values in dd/mm/yy format. So, no need to use conve
rttodatetime function. If you are using ISE < posh3, then use converttodatetime
function.
#$uptime = $OS.ConvertToDateTime($OS.lastbootuptime)
$uptime = $OS.LastBootUpTime
#$BiosVersion = $Bios.Manufacturer + " " + $Bios.SMBIOSBIOSVERSION + " " + $Bio
s.ConvertToDateTime($Bios.Releasedate)
$BiosVersion = $Bios.Manufacturer + " " + $Bios.SMBIOSBIOSVERSION + " " + $Bios
.Releasedate
$CPUCount = $cpu | select name | measure | Select -ExpandProperty count
$CPUInfo = $CPU.Name
$CPUMaxSpeed = ($CPU[0].MaxClockSpeed/1000)
$Model = $CS.Model
$Manufacturer = $CS.Manufacturer
$Description = $CS.Description
$PrimaryOwnerName = $CS.PrimaryOwnerName
$Systemtype = $CS.Systemtype
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++

$column) = $computer
$column) = $OSRunning
$column) = "$TotalMem GB"
$column) = $uptime
$column) = $BiosVersion
$column) = "$SerialNumber"
$column) = $CPUInfo
$column) = $CPUCount
$column) = $CPUMaxSpeed

$driveStr = ""
foreach($drive in $drives)
{
if ($drive.size -ne $null) {

$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 (Percen
t): $freeb % ` "
} else {
$freea = "NA"
$freeb = "NA"
$ID = $drive.DeviceID
$driveStr += "$ID = Total Space: NA / Free Space: NA / Free (Percent): NA ` "
}
}
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++
$value1.Cells.Item($row,
$column++
$row++

$column) = $driveStr
$column) = $Model
$column) = $Manufacturer
$column) = $Description
$column) = $PrimaryOwnerName
$column) = $Systemtype

#network details
$networkinfo = Get-CimInstance -ClassName win32_networkadapterconfiguration -Cim
Session $Session | where {$_.ipenabled -eq "true" -and $_.IPAddress -ne "0.0.0.0
"}
$Description = $networkinfo.description
$IPAddress = $networkinfo.IPAddress
$DHCPServer = $networkinfo.dhcpserver
$DefaultIPGateway = $networkinfo.DefaultIPGateway
$DNSDomain = $networkinfo.DNSDomain
$DHCPEnabled = $networkinfo.DHCPEnabled
$MACAddress = $networkinfo.MACAddress
$sheet2column = 1
$sheet2.Cells.Item($sheet2row, $sheet2column) = $computer
$sheet2column++
$sheet2.Cells.Item($sheet2row, $sheet2column) = $Description
$sheet2column++
$sheet2.Cells.Item($sheet2row, $sheet2column) = $IPAddress
$sheet2column++
$sheet2.Cells.Item($sheet2row, $sheet2column) = $DHCPServer
$sheet2column++
$sheet2.Cells.Item($sheet2row, $sheet2column) = $DefaultIPGateway
$sheet2column++
$sheet2.Cells.Item($sheet2row, $sheet2column) = $DNSDomain
$sheet2column++

$sheet2.Cells.Item($sheet2row, $sheet2column) = $DHCPEnabled


$sheet2column++
$sheet2.Cells.Item($sheet2row, $sheet2column) = $MACAddress
$sheet2row++
$usedRange1 = $sheet2.UsedRange
$usedRange1.EntireColumn.AutoFit() | Out-Null
#Pagefile Details
$page = Get-CimInstance -ClassName win32_pagefilesetting -CimSession $Session
if (($page| select Name).count -gt 1) {
$captionn = $page[0].Name + "," + $page[1].Name + "," + $page[2].Name + "," + $
page[3].Name
} else {
$captionn = $page.Name
}
if (($page | select MaximumSize).count -gt 1) {
$maxPFconfigured = $page[0].MaximumSize + $page[1].MaximumSize + $page[2].Maxim
umSize + $page[3].MaximumSize
} else {
$maxPFconfigured = $page.MaximumSize
}
if (($page | select InitialSize).count -gt 1) {
$inipfconfigured = $page[0].InitialSize + $page[1].InitialSize + $page[2].Initi
alSize + $page[3].InitialSize
} else {
$inipfconfigured = $page.InitialSize
}
$page2column = 1
$sheet3.Cells.Item($page2row,
$page2column++
$sheet3.Cells.Item($page2row,
$page2column++
$sheet3.Cells.Item($page2row,
$page2column++
$sheet3.Cells.Item($page2row,
$page2column++
$sheet3.Cells.Item($Page2row,
$Page2row++

$page2column) = $computer
$page2column) = $captionn
$page2column) = $inipfconfigured
$page2column) = $maxPFconfigured
$page2column) = "$TotalMem GB"

$usedRange2 = $sheet3.UsedRange
$usedRange2.EntireColumn.AutoFit() | Out-Null
#Miscellenious Details
$misc = Get-CimInstance -ClassName win32_TimeZone -CimSession $Session
$timezone = $misc.caption
$standardname = $misc.StandardName
$bootconfig = (Get-CimInstance -ClassName Win32_BootConfiguration -CimSession $S
ession).ConfigurationPath
###############################################################################
function get-localusers {
param(
[Parameter(Mandatory=$true,valuefrompipeline=$true)]
[string]$strComputer)
begin {}

Process {
$adminlist =""
#$powerlist =""
$computer = [ADSI]("WinNT://" + $strComputer + ",computer")
$AdminGroup = $computer.psbase.children.find("Administrators")
#$powerGroup = $computer.psbase.children.find("Power Users")
$Adminmembers= $AdminGroup.psbase.invoke("Members") | %{$_.GetType().Inv
okeMember("Name", 'GetProperty', $null, $_, $null)}
#$Powermembers= $PowerGroup.psbase.invoke("Members") | %{$_.GetType().In
vokeMember("Name", 'GetProperty', $null, $_, $null)}
foreach ($admin in $Adminmembers) { $adminlist = $adminlist + $admin + "
," }
#foreach ($poweruser in $Powermembers) { $powerlist = $powerlist + $powe
ruser + "," }
#$Computer = New-Object psobject
#$computer | Add-Member noteproperty ComputerName $strComputer
#$computer | Add-Member noteproperty Administrators $adminlist
#$computer | Add-Member noteproperty PowerUsers $powerlist
Write-Output $adminlist
}
end {}
}
$admins = get-localusers $computer
################################################################################
#######
$misc1column = 1
$sheet4.Cells.Item($misc1row,$misc1column) = $computer
$misc1column++
$sheet4.Cells.Item($misc1row,$misc1column) = $timezone
$misc1column++
$sheet4.Cells.Item($misc1row,$misc1column) = $standardname
$misc1column++
$sheet4.Cells.Item($misc1row,$misc1column) = $bootconfig
$misc1column++
$sheet4.Cells.Item($misc1row,$misc1column) = $admins
$misc1row++
$usedRange3 = $sheet4.UsedRange
$usedRange3.EntireColumn.AutoFit() | Out-Null
#Installed Apps
$sheet5.Cells.Item(1,$serverCount) = "$computer"
$serverCount++
$appsrow = 2
$RegBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.Reg
istryHive]::LocalMachine,$Computer)
$RegUninstall = $RegBase.OpenSubKey('SOFTWARE\Microsoft\Windows\CurrentVersion\U
ninstall')
$apps123 = $RegUninstall.GetSubKeyNames() |
ForEach-Object {
($RegBase.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$_")).
GetValue('DisplayName')
}

$apps = $apps123 | sort


foreach ($app in $apps) {
if ($app -ne $null) {
$sheet5.Cells.Item($appsrow, $appscolumn) = $app
$appsrow++
}
}
$appscolumn++
$usedRange4 = $sheet5.UsedRange
$usedRange4.EntireColumn.AutoFit() | Out-Null
#PagedPool Memory info
$sheet6.Cells.Item($serverCount123,1) = "$computer"
$serverCount123++
$RegBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.Reg
istryHive]::LocalMachine,$computer)
$RegMemMgmt = $RegBase.OpenSubKey('System\CurrentControlSet\Control\Session Mana
ger\Memory Management')
if ($RegMemMgmt.GetValueNames() -contains 'PoolUsageMaximum') {
$sheet6.cells.item($sheet6row,$sheet6column) = "Yes (Configured Value is $($
RegMemMgmt.GetValue('PoolUsageMaximum')))"
$sheet6row++
} else {
$sheet6.cells.item($sheet6row,$sheet6column) = "Not Configured"
$sheet6row++
}
$usedrange5 = $sheet6.usedrange
$usedrange5.entirecolumn.autofit() | Out-Null
} else {
$outp = "$computer not online"
write-host $outp.ToUpper() -ForegroundColor Black -BackgroundColor Cyan
}
}
#write data to excel file and save it
$usedRange = $value1.UsedRange
$usedRange.EntireColumn.AutoFit() | Out-Null
$workbook.SaveAs("$directory\ServerConfiguration.xlsx")
$excel.Quit()
Write-Host `n
Write-Host "ServerConfiguration Report completed successfully" -ForegroundColor
yellow
Write-Host `n
Write-Host "Collecting Hotfix Details... " -ForegroundColor yellow
#release the excel object created and remove variable
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Remove-Variable excel

#Compare Hotfixes in all servers mentioned.


# ---------------------------------------------------------------------------------------------------

foreach ($object in $filepath) {


if (Test-Connection -ComputerName $object -Quiet)
{
$object | Out-File "$directory\filenames.txt" -Append
}
}
$computers = Get-Content "$directory\filenames.txt"
# --------------------------------------------------------------------------------------------------$hotfixes = @()
$result = @()
foreach ($computer in $computers)
{
foreach ($hotfix in (get-hotfix -computer $computer | select HotfixId))
{
$h = New-Object System.Object
$h | Add-Member -type NoteProperty -name "Server" -value $computer
$h | Add-Member -type NoteProperty -name "Hotfix" -value $hotfix.HotfixI
d
$hotfixes += $h
}
}
$ComputerList = $hotfixes | Select-Object -unique Server | Sort-Object Server
foreach ($hotfix in $hotfixes | Select-Object -unique Hotfix | Sort-Object Hotfi
x)
{
$h = New-Object System.Object
$h | Add-Member -type NoteProperty -name "Hotfix" -value $hotfix.Hotfix
foreach ($computer in $ComputerList)
{
if ($hotfixes | Select-Object |Where-Object {($computer.server -eq $_.se
rver) -and ($hotfix.Hotfix -eq $_.Hotfix)})
{$h | Add-Member -type NoteProperty -name $computer.server -value "Insta
lled"}
else
{$h | Add-Member -type NoteProperty -name $computer.server -value "Not I
nstalled"}
}
$result += $h
}
$result | export-csv "$directory\HotfixDetails.csv" -NoTypeInformation
Write-Host `n
Write-Host Hotfix data file created successfully -ForegroundColor yellow
Write-Host `n
Remove-Item "$directory\filenames.txt"
Write-Host Files saved in Folder $directory -ForegroundColor Cyan
Write-Host `n
Write-Host FileNames: -ForegroundColor yellow
Write-Host ServerConfiguration.xlsx -ForegroundColor yellow
Write-Host HotfixDetails.csv -ForegroundColor yellow
Write-Host `n
Write-Host "Post your queries or feedback to powershellguy@outlook.com" -Foregro

undColor white -BackgroundColor blue


Write-Host `n
Read-Host "Press Enter to exit"

Вам также может понравиться