2009-06-15

Powershell Scripting games - Day 5

Registry keys. Sorry for the silly humor - don't know how and why Tonto got in there.

Thanks to Rob Rohr - for helping me out here.

   1: # first we define some variables
   2: $ourpath = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings"
   3: $Maxdl1_0 = "MaxConnectionsPer1_0Server"
   4: $Maxdl = "MaxConnectionsPerServer"
   5:  
   6: #for IE8
   7:  
   8: $ie8path = "HKLM:\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_MAXCONNECTIONSPERSERVER"
   9: $ie8path1_0 = "HKLM:\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_MAXCONNECTIONSPER1_0SERVER"
  10: $ie8Max = "iexplore.exe"
  11:  
  12:  
  13: function upsertReg($path, [string]$name, [int]$value, [string]$propType) {
  14:     "Path: $path"
  15:     "Name: $name"
  16:     "Target Value:  $value"
  17:     
  18:     # verify path exists
  19:     $keyExists = Test-Path $path
  20:     
  21:     if ($keyExists -eq $true) {
  22:         
  23:         # check if named registry item exists at path
  24:         $reg = get-itemproperty $path
  25:         if ($reg.$($name) -eq $null) {
  26:             # value doesn't exist... Create.
  27:             Write-Host " Key is not there Kimosabi. Let's make some magic."
  28:             New-ItemProperty $path -name $name -value $value -propertytype ` 
  29:                 $propType
  30:         } 
  31:         else {
  32:             # value exists... Update...
  33:             $curr = Get-ItemProperty -path $path -name $name
  34:             Write-Host "Hold you horses Kimosabi. It is already there!"
  35:             $confirm = read-host " You have ($curr) as your current value and ` 
  36:                 we are going to change it to ==>$value. Are you sure? (Y/N)"
  37:             if ($confirm -eq "Y") {
  38:                 Set-ItemProperty -path $path -name $name -value $value
  39:                 Write-Host "Entry has been changed Kimosabi."
  40:             }
  41:             else {
  42:             Write-Host "You chose not to change it - so it stays there."
  43:             }
  44:         }
  45:     } else {
  46:         "Path doesn't exist.  Registry entry not created."
  47:     }
  48: }
  49:  
  50: # create/update registry items using the function
  51: upsertReg $ourpath $Maxdl 10 DWord
  52: upsertReg $ourpath $Maxdl1_0 10 DWord
  53: upsertReg $ie8path $ie8Max 10 DWord
  54: upsertReg $ie8path1_0 $ie8Max 10 DWord