Sunday, April 24, 2011

The 2011 Scripting Games Advanced Event 5: Use PowerShell to Determine Upgrade to Windows 7 Eligibility

The 2011 Scripting Games Advanced Event 5: Use PowerShell to Determine Upgrade to Windows 7 Eligibility

My personal script:
http://2011sg.poshcode.org/1130
Average Rating: 4.00 by 2 users.
(Download it)


#
#
# 2011 Scripting Games Advanced Event 5: Use PowerShell to Determine Upgrade to Windows 7 Eligibility
#
# by F.Richard 2011-04
#
#

#Requires -Version 2.0

[CmdletBinding()]
Param(
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeLine = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[String[]] $computername = $Env:COMPUTERNAME

)




Function Get-ComputerInfos {
<#
.SYNOPSIS
Get Computer informations
.DESCRIPTION
Use PowerShell to get computer informations
.PARAMETER computername
computer name ex: mycomputer default:localhost
.EXAMPLE
Get-ComputerInfos
get localhost computer information
.EXAMPLE
.EXAMPLE
Get-ComputerInfos mycomputer
get mycomputer computer information
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeLine = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[String] $computername = $Env:COMPUTERNAME
)
trap [Exception] {
write-host
write-error $("TRAP: " + $_.Exception.GetType().FullName)
write-error $("TRAP: " + $_.Exception.Message)
continue;
}

$objParent = New-Object PSObject

# Get CPU speed and architectuure
$wmicpus = Get-WmiObject -Class "Win32_Processor" -ComputerName $computername -Property MaxClockSpeed
Foreach($wmicpu in $wmicpus){
$debug = "CPUspeed: " + $wmicpu.MaxClockSpeed
Write-Debug $debug
$objParent | Add-Member -MemberType noteproperty -Name "CPUspeed" -Value $wmicpu.MaxClockSpeed
break
}

# "Win32_ComputerSystem" TotalPhysicalMemory NOT report correct information -> use Win32_PhysicalMemory Capacity sum
$capacity = (Get-WmiObject Win32_PhysicalMemory -ComputerName $computername -Property Capacity | measure-object Capacity -sum).Sum/1MB
Write-Debug "RAMCapacity: $capacity"
$objParent | Add-Member -MemberType noteproperty -Name "RAMCapacity" -Value $capacity

# Get all size drives
$sizedrives = Get-WmiObject -Class "Win32_DiskDrive" -ComputerName $computername -Property Size | Select Size
Write-Debug "sizedrives: $sizedrives"
$objParent | Add-Member -MemberType noteproperty -Name "objDrive" -Value $sizedrives

# solution 1: get microsoft site compatibility list then regex html
# http://www.microsoft.com/windows/compatibility/windows-7/en-us/Search.aspx?type=Hardware&category=Graphics%20Cards%20%26%20Components&compat=all&os=32-bit&s=asus%207800&l=en-us
# solution 2: use dxdiag command with invoke-command
# start /wait dxdiag /whql:off /t -> generate dxdiag.txt -> line DirectX Version: DirectX 9.0c (4.09.0000.0904)
# solution 3: use remote registry
$Registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computername)
$RegKey = $Registry.OpenSubKey("Software\Microsoft\DirectX\" )
$directxversion = $RegKey.GetValue("Version")
$objParent | Add-Member -MemberType noteproperty -Name "directxversion" -Value $directxversion
# DirectX 9.0 "4.09.0000.0900" to test if >= 9 then test 4 first letter "4.09"
# else can use switch version but then i need exactly all directx version
if ($directxversion.substring(0,4) -ge "4.09") {
$directx = 9
} else {
$directx = 0
}
Write-Debug "DirectX: $directx"
$objParent | Add-Member -MemberType noteproperty -Name "directx" -Value $directx

return $objParent
}




# Test-IsAdministrator
# http://blogs.technet.com/b/heyscriptingguy/archive/2010/11/25/use-powershell-to-add-local-users-to-local-groups.aspx
Function Test-IsAdministrator {
    <#
    .Synopsis
        Tests if the user is an administrator
    .Description
        Returns true if a user is an administrator, false if the user is not an administrator        
    .Example
        Test-IsAdministrator
#>   
    param()
    $currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
return (New-Object Security.Principal.WindowsPrincipal $currentUser).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
} #end function Test-IsAdministrator



# Main Program
<#
.SYNOPSIS
Get Computer informations
.DESCRIPTION
Use PowerShell to get computer informations
.PARAMETER computername
computer name ex: mycomputer default:localhost
.EXAMPLE
AdvEvent5.ps1
get localhost computer information
.EXAMPLE
AdvEvent5.ps1 -debug
get localhost computer information with debug
.EXAMPLE
"computer1" | AdvEvent5.ps1
get computer1 information
.EXAMPLE
AdvEvent5.ps1 -computer computer1,computer2
get computer1 and computer2 information
#>

if (!(Test-IsAdministrator)) {
Write-Warning "Your user is not Administrator on this host"
}
$x32 = @{"cpu" = "1000"; "memory" = "1024"; "disk" = "16"; "directx" = "9"}
$x64 = @{"cpu" = "2000"; "memory" = "2048"; "disk" = "20"; "directx" = "9"}


# foreach computer
foreach ($computer in $computername) {
$obj = Get-ComputerInfos -computername $computer

# with -debug switch too lazy and no time to do a function with array of hash including debug

# 32bits
$eligibilityX32=$False
if ($obj.directx -ge $x32["directx"]) {
$debug = "DirectX Version: >= 9 (Eligibil 32bits)"
Write-Debug $debug
if ($obj.CPUspeed -ge $x32["cpu"]) {
$debug = "Computer CPU:" + $obj.CPUspeed + " >= " + $x32["cpu"] + " (Eligibil 32bits)"
Write-Debug $debug
if ($obj.RAMCapacity -ge $x32["memory"]) {
$debug = "Computer RAM:" + $obj.RAMCapacity + " >= " + $x32["memory"] + " (Eligibil 32bits)"
Write-Debug $debug
foreach ($disk in $obj.objDrive) {
if (($disk.Size/1GB) -ge $x32["disk"]) {
$eligibilityX32=$True
$debug = "Computer Disks:Disk >= " + $x32["disk"] + " (Eligibil 32bits)"
Write-Debug $debug
break
} else {
$debug = "Computer Disks:Disk < " + $x32["disk"] + " (NOT Eligibil 32bits)"
Write-Debug $debug
}
}
} else {
$debug = "Computer RAM:" + $obj.RAMCapacity + " < " + $x32["memory"] + " (NOT Eligibil 32bits)"
Write-Debug $debug
}
} else {
$debug = "Computer CPU:" + $obj.CPUspeed + "<" + $x32['cpu'] + " (NOT Eligibil 32bits)"
Write-Debug $debug
}
} else {
$debug = "DirectX Version: < 9 (NOT Eligibil 32bits)"
Write-Debug $debug
}
Write-Debug "eligibilityX32: $eligibilityX32"

# 64bits
$eligibilityX64=$False
if ($obj.directx -ge $x64["directx"]) {
$debug = "DirectX Version: >= 9 (Eligibil 64bits)"
Write-Debug $debug
if ($obj.CPUspeed -ge $x64["cpu"]) {
$debug = "Computer CPU:" + $obj.CPUspeed + " >= " + $x64["cpu"] + " (Eligibil 64bits)"
Write-Debug $debug
if ($obj.RAMCapacity -ge $x64["memory"]) {
$debug = "Computer RAM:" + $obj.RAMCapacity + " >= " + $x64["memory"] + " (Eligibil 64bits)"
Write-Debug $debug
foreach ($disk in $obj.objDrive) {
if (($disk.Size/1GB) -ge $x64["disk"]) {
$eligibilityX64=$True
$debug = "Computer Disks:Disk >= " + $x64["disk"] + " (Eligibil 64bits)"
Write-Debug $debug
break
} else {
$debug = "Computer Disks:Disk < " + $x64["disk"] + " (NOT Eligibil 64bits)"
Write-Debug $debug
}
}
} else {
$debug = "Computer RAM:" + $obj.RAMCapacity + " < " + $x64["memory"] + " (NOT Eligibil 64bits)"
Write-Debug $debug
}
} else {
$debug = "Computer CPU:" + $obj.CPUspeed + "<" + $x64['cpu'] + " (NOT Eligibil 64bits)"
Write-Debug $debug
}
} else {
$debug = "DirectX Version: < 9 (NOT Eligibil 64bits)"
Write-Debug $debug
}
Write-Debug "eligibilityX64: $eligibilityX64"

if ($eligibilityX64 -and $eligibilityX32 -and ($obj.RAMCapacity -ge 3072)) {
Write-Host "$computer best upgraded to a 64-bit (x64) Windows 7 operating system."
} else {
Write-Host "$computer best upgraded to a 32-bit (x86) Windows 7 operating system."
}
}

No comments: