Scripting Games 2010. Advanced Event 5 (Remotely getting amount of video RAM)

Пятое задание:

Event Scenario

You are in the process of planning an upgrade from Windows XP to Windows 7, and you want to ensure that your computers will have enough video memory to run Windows Aero. You have determined that you want systems to have at least 128 MB of video RAM.

You decide to write a script to obtain the video memory from local and remote computers. The script should display the amount of video RAM by using the largest possible unit of measure. For example, if the computer has 1 or more GB of video memory, you should display results in GB. If the system has 128 MB of video RAM, you should use MB.

If you write Windows PowerShell code, you can display results directly to either the Windows PowerShell console or inside the Windows PowerShell ISE. If you use VBScript, you should display the results inside the cmd prompt and not in a message box. This output is shown in the following image.

 

Design Points

· Style points given for reusable code.
· Your script should automatically determine the units to display the video memory.
· Your script should be able to run against multiple remote computers at same time.
· Your script should ensure that a remote computer is reachable before attempting the connection to retrieve the video RAM.
· If the memory is less than 128 MB, your script should state that the computer must be upgraded.
· If the memory is 128 MB or more, your script should state that the computer is ready to be upgraded.

Мое решение:

<#
Get-VideoRAM.ps1 PowerShell shs 20100504

.Synopsis
	determine amount of the VideoRAM on the remote computers

	Get-VideoRAM.ps1 <CompList>

.Parameter CompList
	list of remote computers that are required to determine the amount of memory

.Example
	Get-VideoRAM.ps1 c:\Scripts\PoSh.try\ScriptingGames\2010\5\CompList.txt

#>
param($CompList)
###############################################################################
#
# Function Show-Help. Write  first blok comment of this script to the host
#
Function Show-Help ($ScriptFullName){
   if ($ScriptFullName) {
       $IsHelpLine=$false
       switch -file $ScriptFullName {
           {$_ -match "<#"}     {Write-Host $_;$IsHelpLine=$true;continue}
           {$_ -match "#>"}     {Write-Host $_;$IsHelpLine=$false;break}
           {$IsHelpLine}        {Write-Host $_;}
       }
   }
}
###############################################################################
#
# Filter Where-Online. Return pingable computers only
#
Filter Where-Online
{
   $CompName=$_
   $ping = new-object System.Net.NetworkInformation.Ping
   try {if ($ping.send($_).Status -eq "Success" ) { $_ }}
   catch {Write-Verbose "$CompName `t-`t ping error"}
}
###############################################################################
#
# Function FormatRAMInfo
# Format the amount of video RAM by using the largest possible unit of measure
#
Function FormatRAMInfo ($RAMamount) {
	if ($RAMamount/1gb -ge 1) {$FormatedRAMamount="$($RAMamount/1gb)GB"}
	else {$FormatedRAMamount="$($RAMamount/1mb)MB"}
	$FormatedRAMamount
}
###############################################################################
#
# Function NeedUpgrade
# Return $true if computer need to upgrade
#
Function NeedUpgrade ($RAMamount) {
	if ($RAMamount/1mb -lt 128) {$true}
	else {$false}
}
###############################################################################
cls
#$VerbosePreference=Continue
#if parameter $CompList was passed to the script...
if ($Complist) {
   	#if list of computers exist...
   	if (Test-Path $CompList) {
		$CompsOnline=$null
       	#for each computer in the list which is online...
       	gc $CompList| Where-Online | foreach {
			#compose  string which contains computer names separated by comma
			$CompsOnline+=",$_"
			#remove first comma
			$CompsOnline=$CompsOnline -replace "^,"
		}
		if ($CompsOnline) {
			$VideoControllers=Invoke-Expression "Invoke-Command -ComputerName $CompsOnline -ScriptBlock {gwmi Win32_VideoController} -ErrorAction SilentlyContinue"
			$VideoControllers|ft PSComputerName , @{N="RAMInfo";E={FormatRAMInfo $_.AdapterRAM}},@{N="HaveToUpgrade";E={NeedUpgrade $_.AdapterRAM}} -AutoSize
		}
	}
	else {
		Write-Host "List of Computers $CompList not found"
	}
}
else {
	Write-Host "Parameter <CompList> required"
	#Write top block comment as help
	Show-Help $MyInvocation.MyCommand.Path
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.