Skip to content

Instantly share code, notes, and snippets.

@codewithtyler
Last active August 18, 2020 16:14
Show Gist options
  • Save codewithtyler/b3a817f8bbb6f0d8704085f08abd8b29 to your computer and use it in GitHub Desktop.
Save codewithtyler/b3a817f8bbb6f0d8704085f08abd8b29 to your computer and use it in GitHub Desktop.
Get information about a PC (script may not work against any PC that's not running Windows Server)
# Prevents errors from showing in the console
$ErrorActionPreference = 'Stop'
$CPUInfo = Get-WmiObject Win32_Processor #Get CPU Information
$OSInfo = Get-WmiObject Win32_OperatingSystem #Get OS Information
$PhysicalMemory = Get-WmiObject CIM_PhysicalMemory | Measure-Object -Property capacity -Sum | % {[math]::round(($_.sum / 1GB),2)}
$TotalStorage = "C - " + ( [math]::Round( ( ( Get-Volume -DriveLetter C ).Size ) / 1GB, 1 ) ).ToString() + " GB"
$serverName = $CPUInfo.SystemName
if ( $CPUInfo.SystemName -is [array] ) {
$serverName = $CPUInfo.SystemName[0]
}
$cpu = $CPUInfo.Name
if ( $cpu -is [array] ) {
$cpuFormattedStr = $cpu[0] -replace '\(R\)', ''
$cpu = $cpu.Length.ToString() + ' CPUs both are ' + $cpuFormattedStr
}
# Check if IIS is installed
$iisVersion = ''
try {
$iisVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$env:SystemRoot\system32\inetsrv\InetMgr.exe").ProductVersion
}
catch [System.Exception] {
$iisVersion = 'Not Installed'
}
# Check if .NET Core is installed
$dotnetCoreVersion = ''
try {
$dotnetCoreVersion = (dir (Get-Command dotnet).Path.Replace('dotnet.exe', 'shared\Microsoft.NETCore.App')).Name
if ( $dotnetCoreVersion.Count -gt 1 ) {
$dotnetCoreVersion = $dotnetCoreVersion[-1]
}
}
catch [System.Exception] {
$dotnetCoreVersion = 'Not Installed'
}
# Check if Amazon Corretto is installed
$javaHome = [Environment]::GetEnvironmentVariable('JAVA_HOME', [EnvironmentVariableTarget]::Machine)
$javaVersion = ''
$correttoInstalled = 'Not Installed'
if ( $javaHome -like '*Amazon Corretto*' ) {
$correttoInstalled = 'Installed'
$javaVersion = $javaHome.split( '\' )[-1]
$javaVersion = $javaVersion -replace "jdk1.", "Java "
$javaVersion = $javaVersion -replace ".0_", " Update "
$dotnetCoreVersion = 'Not Intalled'
}
else {
$javaVersion = Get-WmiObject Win32_Product | Where-Object { $_.Name -like 'Java*Update*' } | Select-Object -First 1 | Select-Object Name -ExpandProperty Name
}
if ( [string]::IsNullOrEmpty( $javaVersion ) ) {
$javaVersion = 'Not Installed'
}
# .NET Framework Version
$gpParams = @{
Path = 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'
ErrorAction = 'SilentlyContinue'
}
$release = Get-ItemProperty @gpParams | Select-Object -ExpandProperty Release
$dotnetFrameworkVersion = "$(
switch ($release) {
({ $_ -ge 528040 }) { '4.8'; break }
({ $_ -ge 461808 }) { '4.7.2'; break }
({ $_ -ge 461308 }) { '4.7.1'; break }
({ $_ -ge 460798 }) { '4.7'; break }
({ $_ -ge 394802 }) { '4.6.2'; break }
({ $_ -ge 394254 }) { '4.6.1'; break }
({ $_ -ge 393295 }) { '4.6'; break }
({ $_ -ge 379893 }) { '4.5.2'; break }
({ $_ -ge 378675 }) { '4.5.1'; break }
({ $_ -ge 378389 }) { '4.5'; break }
default { ':4.5+ not installed.' }
}
)"
$infoObject = New-Object PSObject
#The following add data to the infoObjects.
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Server Name" -value $serverName
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Alias" -value $serverName
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Fully Qualified Name" -value "$env:computername.$env:userdnsdomain"
Add-Member -inputObject $infoObject -memberType NoteProperty -name "IP Address" -value ( Get-NetIPConfiguration | Where-Object { $_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne "Disconnected" } ).IPv4Address.IPAddress
Add-Member -inputObject $infoObject -memberType NoteProperty -name "OS" -value $OSInfo.Caption
Add-Member -InputObject $infoObject -MemberType NoteProperty -name "OS Architecture" -value $OSInfo.OSArchitecture
Add-Member -inputObject $infoObject -memberType NoteProperty -name "CPU" -value $cpu
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Memory (GB)" -value $PhysicalMemory
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Harddisk(s)" -value $TotalStorage
Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "IIS" -Value $iisVersion
Add-Member -InputObject $infoObject -MemberType NoteProperty -Name ".NET Framework" -value $dotnetFrameworkVersion
Add-Member -InputObject $infoObject -MemberType NoteProperty -Name ".NET Core" -value $dotnetCoreVersion
Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "Amazon Corretto" -Value $correttoInstalled
Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "Java" -Value $javaVersion
Clear-Host
$infoObject | Format-List
# In some cases PowerShell ISE may not be installed on the server.
# For those situations run the two commands below to install it.
Import-Module ServerManager
Add-WindowsFeature PowerShell-ISE
  1. IIS Version
  2. .NET Framework version
  3. .NET Core version
  4. Java version
  5. Amazon Coretto installed Yes/No
  6. WebLogic Version
  7. Tomcat Version
  8. Oracle Client
  9. SAP BI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment