PowerShell Server Details


    Server Details to Email 

    Query Active Directory and Test access to servers  / workstations.
    Exports failed access to file and attaches to email 
    Exports all findings to CSV and attaches to email 
    Formats output and places into HTML in body of email 

    Requires Rights Remote Machines  
    Author: Scott Head
    Min PSVer: Powershell 2.0
    Version: 1.0 (7/8/2019) Script Created     


        #Get Info Manufactuer 
        $Manufacturer=Get-WmiObject Win32_Computersystem | Select -ExpandProperty Manufacturer 

        #Get Prcessor Info 
        $CPU_Name=Get-WMIobject -Class Win32_Processor | Select -ExpandProperty Name | Select-Object -First 1

        #Use PSDrive Cmdlet to get Hard Drive Information
        $MyHardDrive=Get-PSDrive C | Select Used, Free

        #Translate from Bytes to GB

        #Use Math Function 2 Decimal Places

        #Translate from Bytes to GB

        #Use Math Function 2 Decimal Places

        #Math Add 2 together for Total Drive Space
        $Total= ($Used+$Free)
        $Total= $Total.ToString() + " GB"

        $Used=$Used.ToString() + " GB"
        $Free=$Free.ToString() + " GB"

        #Get RAM DATA
        $RAM=Get-WmiObject -Class Win32_OperatingSystem -Namespace root/cimv2 -ComputerName Localhost
        $FreeRAM = (“{0:N2}” -f ($RAM.FreePhysicalMemory  / 1000000)) + " GB"
        $AllRAM=(“{0:N2}” -f ($RAM.TotalVisibleMemorySize  / 1000000)) + " GB"
        $UsedRam=(“{0:N2}” -f ($RAM.TotalVisibleMemorySize  / 1000000))  - (“{0:N2}” -f ($RAM.FreePhysicalMemory  / 1000000)) 
        $UsedRam=$UsedRam.ToString() + " GB"

        $Dates = Get-Date
        #Get Last Boot Time 
        $BootTime= Get-WmiObject win32_operatingsystem | select csname, @{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}

        #Compiles all data into obect for return                         
        $Obj=New-Object PSObject 
        $Obj |Add-Member NoteProperty Manufacturer ($Manufacturer)
        $Obj |Add-Member NoteProperty CPU_Name ($CPU_Name)
        $Obj |Add-Member NoteProperty C_Drive_Used ($Used)
        $Obj |Add-Member NoteProperty C_Drive_Free ($Free)
        $Obj |Add-Member NoteProperty C_Drive_Total ($Total)
        $Obj |Add-Member NoteProperty RAM_Used ($UsedRam)
        $Obj |Add-Member NoteProperty RAM_Free ($FreeRAM)
        $Obj |Add-Member NoteProperty RAM_Total ($AllRAM)
        $Obj |Add-Member NoteProperty Last_Reboot ($BootTime)
        $Obj |Add-Member NoteProperty Date ($Dates)
        Return $Obj      

Function WinRM-Status($Computers){   

        #Test Path to each system and throttle limit of concurrnet processes to 256
        $WinRMJob = Invoke-Command -ScriptBlock {Test-Path "C:\"} -ComputerName $Computers  -ThrottleLimit 256 -AsJob | Wait-Job

        #Group the results together, then sort into 2 string arrays with servername only
        $Grouped = $WinRMJob.ChildJobs | Group-Object -Property State

        #Pulls Successfule items to one array
        [Array]$WinrmSuccess = ($Grouped | where {$_.Name -eq "Completed"}).Group | %{$_.Location}

        #Pulls Failed Items to another array
        [Array]$noWinrm = Compare-Object -ReferenceObject $Computers -DifferenceObject $WinrmSuccess | Select-Object -ExpandProperty  Inputobject -ErrorAction SilentlyContinue

        #Assigns Array values to object
        $properties = @{ 
            WinRMFails = $noWinrm;  
            WinRMPass = $WinrmSuccess;  

        #Create Object and Assign to Var
        $ReturnObj = New-Object -TypeName PSObject -Property $properties

        #Returns the Value
        Return $ReturnObj


    #Get List of Systems to be Checked.
    $ComputerList= Get-ADComputer -Properties * -filter {(Enabled -eq $True) -and (Operatingsystem -like "Windows*")} | Select -ExpandProperty Name

    #Runs the Function and Applies list of Computers
    $Results=WinRM-Status $ComputerList

    #Output to Screen
    $Results.WinRMFails | Out-File C:\temp\SystemsFailed.txt

    $MyData=Invoke-Command     $Results.WinRMPass -ScriptBlock $MyCommand 
    $head = "<style>"
    $head = $head + "BODY{background-color:white;}"
    $head = $head + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
    $head = $head + "TH{border-width: 1px;padding: 4px;border-style: solid;border-color: black;background-color:Yellow}"
    $head = $head + "TD{border-width: 1px;padding: 8px;border-style: solid;border-color: black;background-color:white}"
    $head = $head + "</style>"

    $MyData | Select PSComputerName,Manufacturer,CPU_Name,C_Drive_Used,C_Drive_Free,C_Drive_Total,Ram_Used,RAM_Free,RAM_Total,Last_Reboot,Date | Export-CSV C:\temp\Server_Details.csv -NoTypeInformation
    $MyData | Select PSComputerName,Manufacturer,CPU_Name,C_Drive_Used,C_Drive_Free,C_Drive_Total,Ram_Used,RAM_Free,RAM_Total,Last_Reboot,Date | ConvertTo-HTML -Head $head -Body $strMail | Out-File "C:\temp\Server_Details.Html"


   # SMTP info
    $smtpServer = "mailserver.com" 
    $strFrom = "Scripts@Company.com"
    $strTo = "YourEmail@Company.com"
    $strSubject = “Test"
    $Body=Get-Content "C:\temp\Server_Details.Html" | Out-String 
    Send-Mailmessage -SmtpServer $SmtpServer -from $strFrom -Attachments $Attachement -to $strTo -Subject $strSubject -BodyasHTML $Body 

2020 | Scott W. Head  |  Sr. Systems Admin | Certified Microsoft Windows Server Administrator