Created
October 15, 2015 21:26
-
-
Save adbertram/9b751cdc917730d186c6 to your computer and use it in GitHub Desktop.
get-computerinfo-revision
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -version 2.0 | |
function Get-ADSiteFromComputer | |
{ | |
<# | |
.Synopsis | |
Return the site that a computer is member of | |
.DESCRIPTION | |
Given a computer name, it looks up its IP address and checks which AD site's subnet it is in | |
.EXAMPLE | |
Get-ADSiteFromComputer -ComputerName "server1" | |
.EXAMPLE | |
"server1" | Get-ADSiteFromComputer | |
.NOTES | |
Adapted from http://www.itadmintools.com/2011/08/tcpip-subnet-math-with-powershell-part.html | |
#> | |
[CmdletBinding()] | |
#[OutputType([string])] | |
Param | |
( | |
# Computer Name to look up | |
[Parameter(Mandatory=$true, | |
ValueFromPipeline=$true, | |
ValueFromPipelineByPropertyName=$true, | |
Position=0)] | |
$ComputerName | |
) | |
#Helper function, convert dotted decimal IP address to binary | |
function toBinary ($dottedDecimal){ | |
$dottedDecimal.split(".") | %{$binary=$binary + $([convert]::toString($_,2).padleft(8,"0"))} | |
return $binary | |
} | |
#main function | |
$hostEntry= [System.Net.Dns]::GetHostByName($ComputerName) | |
if($hostEntry){ | |
$ipAddress=toBinary ($hostEntry.AddressList[0].IPAddressToString) | |
}else{ | |
Write-Warning "$ComputerName not found!" | |
Return #Exit | |
} | |
$sites=@{} | |
$subnetsDN="LDAP://CN=Subnets,CN=Sites," + $([adsi] "LDAP://RootDSE").Get("ConfigurationNamingContext") | |
Write-Verbose "`nGathering Site Information..." | |
foreach ($subnet in $([adsi] $subnetsDN).psbase.children){ | |
$site=[adsi] "LDAP://$($subnet.siteObject)" | |
if($site.cn -ne $null){ | |
($networkID,$netbits)=$($subnet.cn).split("/") | |
$binNetID=(toBinary $networkID).substring(0,$netbits) | |
$sites[$binNetID]=([string]$site.cn) #.toUpper() | |
} | |
} | |
$i=32 | |
do {$tryNetID=$ipAddress.substring(0,$i); | |
if($sites[$tryNetID]){ | |
Write-Verbose "`n$ComputerName is in site $($sites[$tryNetID])`n" | |
Return $sites[$tryNetID] #Exit | |
} | |
$i-- | |
} while ($i -gt 0) | |
Write-Warning "`n$ComputerName is not in a defined site`n" | |
} | |
function Get-ComputerInfo | |
{ | |
<# | |
.Synopsis | |
Gets info on a computer | |
.DESCRIPTION | |
Collects into from server via WMI. | |
.EXAMPLE | |
Get-ComputerInfo.ps1 | |
gets info on local server | |
.EXAMPLE | |
Get-ComputerInfo.ps1 -ComputerName Server1 | |
gets info for Server1 | |
.EXAMPLE | |
("Server1","Server2") | .\Invoke-PopulateITDB.ps1 | |
gets info for servers Server1 and Server2 | |
#> | |
[CmdletBinding()] | |
Param | |
( | |
# Computer Name or IP Address | |
[Parameter(Mandatory = $false, | |
ValueFromPipeline = $true, | |
ValueFromPipelineByPropertyName = $true, | |
Position = 0)] | |
[string]$ComputerName | |
) | |
Begin | |
{ | |
#If Get-ADSiteFromComputer function is removed from the start of this file | |
#and the following line is uncommented, it runs without errors (& it works!) | |
#. .\Get-ADSiteFromComputer.ps1 | |
#If no computer specified, use the local machine | |
$LocalHost = [system.environment]::MachineName | |
if ($ComputerName -eq "") | |
{ | |
$ComputerName = $LocalHost | |
} | |
} | |
Process | |
{ | |
foreach ($Computer in $ComputerName) | |
{ | |
Write-Verbose "Getting BIOS Info..." | |
$BIOS = Get-WmiObject -ComputerName $Computer -Class Win32_BIOS | |
Write-Verbose "Getting OS Info ..." | |
$OperatingSystem = Get-WmiObject -ComputerName $Computer -Class Win32_OperatingSystem | |
Write-Verbose "Getting Network Info..." | |
$NetworkConfig = Get-WmiObject -ComputerName $Computer -Class win32_NetworkAdapterConfiguration -filter "IPENabled=$True" | |
#alternate method | |
#$NetworkConfig = Get-WmiObject -ComputerName $Computer -query "select * from win32_NetworkAdapterConfiguration where IPENabled=$Tr | |
Write-Verbose "Getting CPU Info..." | |
$CPU = Get-WmiObject -ComputerName $Computer -Class Win32_Processor | |
Write-Verbose "Getting Computer Info..." | |
$Machine = Get-WmiObject -ComputerName $Computer -Class Win32_ComputerSystem | |
Write-Verbose "Getting Drive Info..." | |
$Drives = Get-WmiObject -ComputerName $Computer -Class Win32_Volume | |
foreach ($item in $Drives) | |
{ | |
$Drive = $item.Caption | |
$Capacity = $item.Capacity | |
$FreeSpace = $item.FreeSpace | |
} | |
$site = Get-ADSiteFromComputer -ComputerName $Computer | |
New-Object psobject -Property @{ | |
HostName = $OperatingSystem.CSName | |
ServiceTag = $BIOS.SerialNumber | |
OS = $OperatingSystem.Caption | |
BootTime = $OperatingSystem.ConvertToDateTime($OperatingSystem.LastBootUpTime) | |
InstallDate = $OperatingSystem.ConvertToDateTime($OperatingSystem.InstallDate) | |
PhysicalMemory = $OperatingSystem.TotalVisibleMemorySize | |
IP = $NetworkConfig.IPAddress[0] | |
IPSubNet = $NetworkConfig.IPSubnet[0] | |
CPU = $CPU.CurrentClockSpeed #should this be MaxClockSpeed? | |
CPUCount = $Machine.NumberOfProcessors | |
Make = $Machine.Manufacturer | |
Model = $Machine.Model | |
Domain = $Machine.Domain | |
Drive = $Drive | |
Capacity = $Capacity | |
FreeSpace = $FreeSpace | |
Site = $Site | |
} | |
} | |
} | |
End | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment