Powershell script to find details about OS Version CPU RAM Storage and type

Hi Friends,

As part of Migration some times we need to collect not just Database information but also OS as well. In such cases you can use the below script to gather the same.

Save the below script with *.ps1 extension and you can run it as per the image shown.















$header = @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>System Status Report</title>
<style type="text/css">
<!--
body {
background-color: #E0E0E0;
font-family: sans-serif
}
table, th, td {
background-color: white;
border-collapse:collapse;
border: 1px solid black;
padding: 5px
}
-->
</style>
"@
$body = @"
<h1>Server Status</h1>
<p>The following report was run on $(get-date).</p>
"@
$machineinfo=@()
Function Get-MachineType
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # ComputerName
        [Parameter(Mandatory=$false,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string[]]$ComputerName=$env:COMPUTERNAME,
        $Credential = [System.Management.Automation.PSCredential]::Empty
    )

    Begin
    {
    }
    Process
    {
        foreach ($Computer in $ComputerName) {
            Write-Verbose "Checking $Computer"
            try {
                $hostdns = [System.Net.DNS]::GetHostEntry($Computer)
                $ComputerSystemInfo = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer -ErrorAction Stop -Credential $Credential
               
                switch ($ComputerSystemInfo.Model) {
                   
                    # Check for Hyper-V Machine Type
                    "Virtual Machine" {
                        $MachineType="VM"
                        }

                    # Check for VMware Machine Type
                    "VMware Virtual Platform" {
                        $MachineType="VM"
                        }

                    # Check for Oracle VM Machine Type
                    "VirtualBox" {
                        $MachineType="VM"
                        }

                    # Check for Xen
                    # I need the values for the Model for which to check.

                    # Check for KVM
                    # I need the values for the Model for which to check.

                    # Otherwise it is a physical Box
                    default {
                        $MachineType="Physical"
                        }
                    }
                    function GetDriveInfo{
    Param($computer)
    # Get disk sizes
    $logicalDisk = Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" -ComputerName $computer
    foreach($disk in $logicalDisk)
    {
        $diskObj = "" | Select-Object Disk,Size,FreeSpace
        $diskObj.Disk = $disk.DeviceID
        $diskObj.Size = "{0:n0} GB" -f (($disk | Measure-Object -Property Size -Sum).sum/1gb)
        $diskObj.FreeSpace = "{0:n0} GB" -f (($disk | Measure-Object -Property FreeSpace -Sum).sum/1gb)

        $text = "{0}  {1}  Free: {2}" -f $diskObj.Disk,$diskObj.size,$diskObj.Freespace
        $msg += $text + [char]13 + [char]10
    }
    $msg
}
               
             
             
             
             
             
             
             
                # Building MachineTypeInfo Object
                $MachineTypeInfo = New-Object -TypeName PSObject -Property ([ordered]@{
                    ComputerName=$ComputerSystemInfo.PSComputername
                    Type=$MachineType
                    Manufacturer=$ComputerSystemInfo.Manufacturer
                    OSVERSION= (Get-WmiObject  -ComputerName $computer -class Win32_OperatingSystem).Caption
                    Logicalprocessors=$ComputerSystemInfo.NumberOfLogicalProcessors
                    physicalprocessors=$ComputerSystemInfo.NumberOfProcessors
                    Memory=[math]::Round($ComputerSystemInfo.TotalPhysicalMemory/1GB.tostring("N0"),3)
                    storage=GetDriveInfo $Computer
                   
                    })
                $MachineInfo =$MachineInfo+$MachineTypeInfo
                }
             
            catch [Exception] {
                Write-Output "$Computer`: $($_.Exception.Message)"
                }
            }
             $machineinfo | ConvertTo-Html -head $header -body $body |Out-File E:\install\serverdetails.html
             Invoke-Expression E:\install\serverdetails.html
    }
    End
    {

    }
}

Comments