Add general, network and hw info

This commit is contained in:
2024-07-24 17:26:08 +02:00
parent 551ba6d42d
commit d32022dfc1

View File

@@ -21,3 +21,183 @@
#> #>
#Requires -RunAsAdministrator
#Requires -Modules PSWriteHTML
param(
[string[]]$Target = $env:COMPUTERNAME,
[switch]$Verbose
)
#refion Set Environment
$ErrorActionPreference = 'Stop'
if($Verbose) {
$VerbosePreference = 'Continue'
} else {
$VerbosePreference = 'SilentlyContinue'
}
#region Functions
# Function to write messages to the console
function Write-Message {
param(
[string]$Message,
[ValidateSet("Info", "Warning", "Error", "Success")]
[string]$Type,
[int]$FixedWidth = 100
)
if ($Message) {
$timestamp = Get-Date -Format "dd.M.yyyy HH:mm:ss"
$totalLength = $fixedWidth - $timestamp.Length - 10 - 6 # Length of timestamp, brackets, spaces, and status
$paddedMessage = $message.PadRight($totalLength, ".")
$formattedMessage = "[{0}] {1} " -f $timestamp, $paddedMessage
Write-Host $formattedMessage -NoNewline
}
if($Type) {
$Color = switch ($Type) {
"Info" { "White" }
"Warning" { "Yellow" }
"Error" { "Red" }
"Success" { "Green" }
Default { "White" }
}
Write-Host "[" -NoNewline
Write-Host $Type -ForegroundColor $Color -NoNewline
Write-Host "]"
}
}
function Get-GeneralInfo {
param(
[string]$Target
)
$ComputerSystem = Get-WmiObject -computername $Target Win32_ComputerSystem
$OperatingSystem = Get-WmiObject -computername $Target Win32_OperatingSystem
switch ($ComputerSystem.DomainRole){
0 { $ComputerRole = "Standalone Workstation" }
1 { $ComputerRole = "Member Workstation" }
2 { $ComputerRole = "Standalone Server" }
3 { $ComputerRole = "Member Server" }
4 { $ComputerRole = "Domain Controller" }
5 { $ComputerRole = "Domain Controller" }
default { $ComputerRole = "Information not available" }
}
$uptime = (Get-Date) - $OperatingSystem.ConvertToDateTime($OperatingSystem.Lastbootuptime)
$GeneralInfo = @{
"ComputerName" = $ComputerSystem.Name
"OS" = $OperatingSystem.Caption
"OS Version" = $OperatingSystem.Version
"OSInstallDate" = $OperatingSystem.ConvertToDateTime($OperatingSystem.InstallDate)
"Domain Role" = $ComputerRole
"Domain" = $ComputerSystem.Domain
"Uptime" = ("{0} days, {1} hours, {2} minutes, {3} seconds" -f $uptime.Days, $uptime.Hours, $uptime.Minutes, $uptime.Seconds)
}
return $GeneralInfo
}
function Get-HardwareInfo {
param (
[string]$Target
)
$ComputerSystem = Get-WmiObject -computername $Target Win32_ComputerSystem
$Processor = Get-WmiObject -computername $Target Win32_Processor
$Memory = Get-WmiObject -computername $Target Win32_PhysicalMemory
$Disk = Get-WmiObject -computername $Target Win32_LogicalDisk
$LogicalDrives = @()
Foreach ($LDrive in ($Disk | Where {$_.DriveType -eq 3})){
$Details = @{
"Drive Letter" = $LDrive.DeviceID
"Label" = $LDrive.VolumeName
"File System" = $LDrive.FileSystem
"Disk Size (GB)" = [math]::round(($LDrive.size / 1GB))
"Disk Free Space" = [math]::round(($LDrive.FreeSpace / 1GB))
"% Free Space" = [Math]::Round(($LDrive.FreeSpace /1GB) / ($LDrive.Size / 1GB) * 100)
}
$LogicalDrives += $Details
}
$HardwareInfo = @{
"Manufacturer" = $ComputerSystem.Manufacturer
"Model" = $ComputerSystem.Model
"Processor Cores" = $Processor.NumberOfCores
"Memory" = (($Memory | Measure-Object -Property capacity -Sum).sum /1gb)
"Disk" = $LogicalDrives
}
return $HardwareInfo
}
function Get-NetworkConfiguration {
param(
[string]$Target
)
$NetworkAdapter = Get-WmiObject -computername $Target Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled -eq $true }
$NetworkConfiguration = @()
foreach ($Adapter in $NetworkAdapter) {
$netAdapter = Get-WmiObject -computername $Target Win32_NetworkAdapter | Where-Object { $_.DeviceID -eq $adapter.Index }
$NetworkInfo = @{
"Interface Name" = $netAdapter.Name
"IP Address" = ($Adapter.IPAddress -join ", ")
"Subnet Mask" = ($Adapter.IPSubnet -join ", ")
"Default Gateway" = ($Adapter.DefaultIPGateway -join ", ")
"DHCP Enabled" = $Adapter.DHCPEnabled
"MAC Address" = $Adapter.MACAddress
"DNS Servers" = ($Adapter.DNSServerSearchOrder -join ", ")
}
$NetworkConfiguration += $NetworkInfo
}
return $NetworkConfiguration
}
#endregion
Write-Host "
_____ ____ _ _ _ _
|_ _/ ___| / \ _ _ __| (_) |_
| | \___ \ / _ \| | | |/ _` | | __|
| | ___) | / ___ \ |_| | (_| | | |_
|_| |____/ /_/ \_\__,_|\__,_|_|\__|
"
Write-Message -Message "Number of targets selected for audit $($Target.Count)" -Type "Info"
foreach ($server in $Target) {
Write-Message -Message "Starting audit on $server" -Type "Info"
Write-Message -Message "Testing connection to $server"
if (Test-Connection -ComputerName $server -Count 1 -Quiet) {
Write-Message -Type "Success"
} else {
Write-Message -Type "Error"
break
}
Write-Message -Message "Collecting general information"
$GeneralInfo = Get-GeneralInfo -Target $server
Write-Message -Type "Success"
Write-Verbose ($GeneralInfo | Out-String)
Write-Message -Message "Collecting hardware information"
$HardwareInfo = Get-HardwareInfo -Target $server
Write-Message -Type "Success"
Write-Verbose ($HardwareInfo | Out-String)
Write-Message -Message "Collecting network configuration"
$NetworkConfiguration = Get-NetworkConfiguration -Target $server
Write-Message -Type "Success"
Write-Verbose ($NetworkConfiguration | Out-String)
}