Files
MSClientOnBoarding/start-audit.ps1

301 lines
9.1 KiB
PowerShell

<#
.SYNOPSIS
Získání základních informací o serveru.
.DESCRIPTION
A detailed description of the script's functionality and usage.
.PARAMETER <Parameter_Name>
Description of a parameter that the script takes.
.EXAMPLE
An example of how to use the script.
.NOTES
Author: Petr Štěpán
Company: Total Service a.s.
Version: 1.0
.LINK
Link to more information or related resources.
#>
#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 Role Id" = $ComputerSystem.DomainRole
"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
}
function Get-FirewallInfo {
param(
[string]$Target
)
$FirewallProfiles = Invoke-Command -ComputerName $Target -ScriptBlock {
Get-NetFirewallProfile
}
$FirewallInfo = @()
foreach ($profile in $FirewallProfiles) {
$FirewallInfo += @{
"Profile Name" = $profile.Name
"Enabled" = $profile.Enabled
"Default Inbound Action" = $profile.DefaultInboundAction
"Default Outbound Action" = $profile.DefaultOutboundAction
#"AllowInboundRules" = $profile.AllowInboundRules
#"AllowLocalFirewallRules" = $profile.AllowLocalFirewallRules
#"AllowLocalIPsecRules" = $profile.AllowLocalIPsecRules
}
}
return $FirewallInfo
}
function Get-Roles {
param(
[string]$Target
)
$Roles = Invoke-Command -ComputerName $Target -ScriptBlock { Get-WindowsFeature | Where-Object { $_.Installed -eq $true }}
$RolesInfo = @()
foreach ($role in $Roles) {
$RolesInfo += @{
"Role Name" = $role.Name
"Description" = $role.Description
"Status" = $role.InstallState
}
}
return $RolesInfo
}
function Get-LocalUserAdmins {
param(
[string]$Target
)
$LocalUserAdmins = Invoke-Command -ComputerName $Target -ScriptBlock {
$SIDs = Get-LocalGroupMember -Group "Administrators" -ErrorAction SilentlyContinue | Where-Object {$_.PrincipalSource -eq "Local"} | Select SID
if ($SIDs){
$LocalAdmins = Get-LocalUser -SID $SIDs.SID.Value
}else{
$LocalAdmins = ""
}
$LocalAdmins
}
$LocalAdminsInfo = @()
foreach ($user in $LocalUserAdmins) {
$lastLogon = if($user.LastLogon){
([string]((Get-Date) - $user.LastLogon).days) + " days ago"
}
$passwordLastSet = if($user.PasswordLastSet){
([string]((Get-Date) - $user.PasswordLastSet).days) + " days ago"
}
$LocalAdminsInfo += @{
"Username" = $user.Name
"Display Name" = $user.FullName
"Description" = $user.Description
"Enabled" = $user.Enabled
"Last Logon" = $lastLogon
"Password Last Set" = $passwordLastSet
}
}
return $LocalAdminsInfo
}
#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)
Write-Message -Message "Collecting firewall configuration"
$FirewallConfiguration = Get-FirewallInfo -Target $server
Write-Message -Type "Success"
Write-Verbose ($FirewallConfiguration | Out-String)
Write-Message -Message "Collecting server roles information"
if($GeneralInfo.'Domain Role Id' -ge 2) {
$ServerRoles = Get-Roles -Target $server
}else {
$ServerRoles = ""
}
Write-Message -Type "Success"
Write-Verbose ($ServerRoles | Out-String)
}