Tuesday, September 29, 2009

Here is a script I pasted together that modifies the machine.config in the .Net framework using Powershell. This script basically allows you to add and remove domains and specify what connection limit you would like to set it to by adding/modifying system.net/connectionmanagement elements, which you can find out more here. Hope this helps someone out. One sight I relied heavily on was http://www.pluralsight.com/community/blogs/dan/archive/2006/10/30/41434.aspx.
-Jarrett

# Global Vars
param(
    [string]$command = $(throw "Please specify action.`nUSAGE: `n>maxuserport.ps1 (read|add|delete) [[int]value]"),
    [string]$domain,
    [int]$value
    )
$usage = "Please specify action.`nUSAGE: `n>maxuserport.ps1 (read|add|delete) [[int]value]"
#write-host "command is " $command "domain is " $domain "and value is " $value
if ($command -notmatch "read|add|delete") {$usage; return}
$bitness = (get-wmiobject win32_OperatingSystem).OSArchitecture
$working_path = "C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG"
$working_path64 = "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG"
$machine_config = "machine.config"
$date = get-date -uformat "%Y%m%d-%H%M"
$paths = @{}
$paths.Add(32,(join-path $working_path $machine_config))
if ($bitness -eq "64-bit") {
  $paths.Add(64, (join-path $working_path64 $machine_config))
}

# ===================================================
# Functions
# ===================================================

function add ([string]$address, [int]$maxconnection) {
  if (!($address)) {"Must specify a domain to add`n"; $usage; return}
  # Loading config
  foreach ($fullpath in $paths.keys) {
    $xml = new-object System.Xml.XmlDocument
    $xml.Load($paths[$fullpath])
    $configuration = $xml.configuration
    # Creating Elements
    if (!($configuration."system.net")) {
      $systemNet = $xml.CreateElement("system.net")
      # If "system.net" does not exist then it is safe to say that
      #  "connectionManagement" does not either.
      $connectionManagement = $xml.CreateElement("connectionManagement")
    }
    else {
      $systemNet = $configuration."system.net"
      $connectionManagement = $systemNet.connectionManagement
    }
    # Add the "add" element
    $add_address = $xml.CreateElement("add")
    $add_address.SetAttribute("address", $address)
    $add_address.SetAttribute("maxconnection", $maxconnection)
    # Connecting Elements
    $null = $connectionManagement.AppendChild($add_address)
    $null = $systemNet.AppendChild($connectionManagement)
    $null = $xml.configuration.AppendChild($systemNet)
    $xml.Save($paths[$fullpath])
    read
  }
}

function read () {
  $OFS = ", "
  $domains = @()
  foreach ($fullpath in $paths.keys) {
    $xml = new-object System.Xml.XmlDocument
    $xml.Load($paths[$fullpath])
    $configuration = $xml.configuration
    # Seeing if "system.net" exists
    if (!($configuration."system.net")) {
      "system.net does not exist"
      return
    }
    $systemNet = $configuration."system.net"
    $connectionManagement = $systemNet.connectionManagement
    $connectionManagement.SelectNodes("add") | % {
      $tmp = '' | select address, maxconnection
      $tmp.address = [string]$_.address
      $tmp.maxconnection = [int]$_.maxconnection
      $domains += $tmp
    }
   "$fullpath(" + "$($domains | % {$_.address + ":" + $_.maxconnection})" + ")"
   }
}

function delete ([string]$address, [int]$maxconnection) {
  if (!($address)) {"Must specify a domain to add\n"; $usage; return}
  $target = @()
  foreach ($fullpath in $paths.keys) {
    # Loading config
    $xml = new-object System.Xml.XmlDocument
    $xml.Load($paths[$fullpath])
    $configuration = $xml.configuration
    # Checking Elements
    if (!($configuration."system.net")) { "system.net Elemnet does not exist.  Exiting"; return }
    $systemNet = $configuration."system.net"
    $connectionManagement = $systemNet.connectionManagement
    # Targeting and deleting Element
    $target = $connectionManagement.selectNodes("add") | ? {$_.address -eq $address -and $_.maxconnection -eq $maxconnection}
    if ($target -eq $null) {"Could not find domain to delete!"; return}
    foreach ($missle in $target) {
      $null = $connectionManagement.RemoveChild($missle)
    }
    $xml.Save($paths[$fullpath])
    read 
  }
}

function backup_config () {
    if (test-path ($fullpath + ".orig") ) {
      copy $fullpath ($fullpath + "." + $date)
    }
    else { copy $fullpath ($fullpath + ".orig") }
}

# ===================================================
# MAIN 
# ===================================================
&($command) $domain $value


0 comments:

Labels