PowerShell - 4 Complete scripts to run for VM Discovery
- Jon Boyette
- Jan 10, 2022
- 2 min read
Save the below as a .ps1. runs and asks for server name.
$A = Read-Host "Enter Server Name to Discover Hardware Details"
#Specify the list of PC names in the line above. "." means local system
Clear-Host
foreach ($Computer in $A)
{
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
$computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
$computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
$computerCPU = get-wmiobject Win32_Processor -Computer $Computer
$computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
"-------------------------------------------------------"
"Manufacturer: " + $computerSystem.Manufacturer
"Model: " + $computerSystem.Model
"Serial Number: " + $computerBIOS.SerialNumber
"CPU: " + $computerCPU.Name
"HDD Capacity C: " + "{0:N2}" -f ($computerHDD.Size[0]/1GB) + "GB"
"HDD Space C: " + "{0:P2}" -f ($computerHDD.FreeSpace[0]/$computerHDD.Size[0]) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace[0]/1GB) + "GB)"
"RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
"Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
"User logged In: " + $computerSystem.UserName
"Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
""
"-------------------------------------------------------"
}
2nd Next, to see if any drives are on the server, that are not being reported to that run. Also Prompts for server name.
$A = Read-Host "Enter ServerName to discover drive sizes and freespace"
Get-WmiObject -Class Win32_LogicalDisk -ComputerName "$A" |
Where-Object {$_.DriveType -eq 3} |
Select-Object DeviceID, Description,`
@{"Label"="DiskSize(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}}, `
@{"Label"="FreeSpace(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}} | FT -AutoSize
3rd script, now I want to get the IPconfig from script and get the DNS order.
$Computer = Read-Host Please Enter Host name
Get-WMIObject Win32_NetworkAdapterConfiguration -Computername $Computer | `
Where-Object {$_.IPEnabled -match "True"} | `
Select-Object -property DNSHostName,ServiceName,@{N="DNSServerSearchOrder";
E={"$($_.DNSServerSearchOrder)"}},
@{N='IPAddress';E={$_.IPAddress}},
@{N='DefaultIPGateway';E={$_.DefaultIPGateway}} | FT
4th - The last is a beautiful script for myself, as I define the c:\temp\services.txt services that I want to check per line in this text file, and make another in c:\temp\servers.txt, add all the servers to check, and this reports to screen, can out-grid or export, this just usually suffices for me.
$Machines = Get-Content -Path "c:\Temp\servers.txt"
$Services = Get-Content -Path "c:\Temp\services.txt"
foreach ($computer in $machines){
Write-host "Checking if service is running on $computer" -b "green" -foregroundcolor "red"
Get-Service -Displayname $Services -ComputerName "$computer"
}
The four above become a nice check on single servers, or the services check many, Make sure no spaces especially after last }.
Comments