Public/OS/Get-WindowsInfo.ps1

<#
Copyright © 2024 Integris. For internal company use only. All rights reserved.
#>


FUNCTION Get-WindowsInfo {
    <#
    .SYNOPSIS
    Retrieves detailed information about the Windows operating system and its configuration.
 
    .DESCRIPTION
    This function gathers and returns various details about the Windows operating system, including domain type, power mode, BitLocker status, and more.
 
    .PARAMETER None
    This function does not take any parameters.
 
    .EXAMPLE
    Get-WindowsInfo
    Retrieves and displays detailed information about the current Windows operating system.
 
    .NOTES
    The function checks for domain join status, BitLocker encryption status, and other system details.
    #>


    $Results = @()

    $ContextElevated = Test-AdministratorElevation
    IF (!($ContextElevated)) {
        Write-Host
        Write-Warning “You are not running this command in an elevated privilege context.`nSome results will not be available. Please re-run this command as an Administrator for complete results.”
    }

    $OS = Get-CIMInstance Win32_OperatingSystem
    $TimeZone = Get-TimeZone
    $DomainType = ""
    $PowerMode = ""

    ### Check DomainJoin Type
    IF ($True) {
        $DomainServicesRegistration = dsregcmd /status

        IF ($DomainServicesRegistration -like "*DomainJoined : YES*") { $DomainType = "Local AD Domain" }
        ELSEIF ($DomainServicesRegistration -like "*AzureAdJoined : YES*" -and $DomainServicesRegistration -like "*DomainJoined : NO*") { $DomainType = "AzureAD" }
        ELSEIF ($DomainServicesRegistration -like "*AzureAdJoined : NO*" -and $DomainServicesRegistration -like "*DomainJoined : NO*") { $DomainType = "None (Workgroup)" }
        ELSE { $DomainType = "Error" }
    }

    IF ($ContextElevated -eq $True) {
        TRY {
            $BitLockerEncryptedDrives = Get-BitLockerVolume -ErrorAction SilentlyContinue | Where-Object { $_.VolumeStatus -like "*encrypted*" }
            $BitLockerEncryptedDrives = $BitLockerEncryptedDrives.MountPoint 
        } CATCH { $BitLockerEncryptedDrives = "[Error.FunctionNotFound.Get-BitLockerVolume]" }
        TRY {
            $UnencryptedDrives = Get-BitLockerVolume -ErrorAction SilentlyContinue | Where-Object { $_.VolumeStatus -like "*decrypted*" }
            $UnencryptedDrives = $UnencryptedDrives.MountPoint
        } CATCH { $UnencryptedDrives = "[Error.FunctionNotFound.Get-BitLockerVolume]" }
    }
    ELSE {
        $BitLockerEncryptedDrives = "[Elevation Required]"
        $UnencryptedDrives = "[Elevation Required]"
    }

    $ProductType = "Unknown"
    IF ($OS.ProductType -eq 1) { $ProductType = "Workstation" }
    IF ($OS.ProductType -eq 2) { $ProductType = "Domain Controller" }
    IF ($OS.ProductType -eq 3) { $ProductType = "Server" }

    $LastBootTimeSpan = (Get-Date) - ($OS.LastBootUpTime)

    IF ($LastBootTimeSpan.Days -eq 1) { $LastBootTimeSpanString = "$([math]::round($($LastBootTimeSpan.Days),0)) Days + $([math]::round($LastBootTimeSpan.Hours,0).ToString().Padleft(2,"0")):$([math]::round($LastBootTimeSpan.Minutes,0).ToString().Padleft(2,"0")):$([math]::round($LastBootTimeSpan.Seconds,0).ToString().Padleft(2,"0"))" }
    ELSE { $LastBootTimeSpanString = "$([math]::round($($LastBootTimeSpan.Days),0)) Days + $([math]::round($LastBootTimeSpan.Hours,0).ToString().Padleft(2,"0")):$([math]::round($LastBootTimeSpan.Minutes,0).ToString().Padleft(2,"0")):$([math]::round($LastBootTimeSpan.Seconds,0).ToString().Padleft(2,"0"))" }
 
    IF (Test-IsLaptop) { $PowerMode = (Get-PowerMode).PowerModeName }

    $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
        Hostname = $env:COMPUTERNAME
        OSName = $OS.Caption
        Type = $ProductType        
        DomainType = $DomainType
        PowerMode = $PowerMode
        Architecture = $OS.OSArchitecture 
        BuildNumber = $OS.BuildNumber
        TimeZone = $TimeZone.DisplayName
        InstallDate = $OS.InstallDate
        LastBootTime = $OS.LastBootUpTime
        LastBootTimeSpan = $LastBootTimeSpanString
        LastWindowsUpdate = (Get-HotFix | Sort-Object -Property InstalledOn -Descending | Select-Object -First 1).InstalledOn
        BitLockerEncryptedDrives = $BitLockerEncryptedDrives
        UnencryptedDrives = $UnencryptedDrives
    }

    RETURN $Results | Select-Object Hostname, OSName, Type, BuildNumber, PowerMode, DomainType, Architecture, TimeZone, InstallDate, LastBootTime, LastBootTimeSpan, LastWindowsUpdate, BitLockerEncryptedDrives, UnencryptedDrives
}