npb consultants

IT Consultancy Services

  • Home
  • Downloads
  • Blog
  • Contact
  • Portal

Archive for the ‘Scripting’ Category

List Number of Mailboxes per Exchange Server

Thursday, April 23rd, 2009

Another powershell script. This time making use of the .NET DirectorySearcher class. The title pretty much says it all – the output from the script lists each Exchange server in the organization with the number of mailboxes hosted.

function GetExchangeServers()
{
	$RootPath = "LDAP://" + $RootDseObj.configurationNamingContext;
	$RootObj = New-Object DirectoryServices.DirectoryEntry $RootPath;
	$Selector = New-Object DirectoryServices.DirectorySearcher;
	$Selector.SearchRoot = $RootObj;
	$Selector.pagesize = 1000;
	$Selector.Sort.PropertyName = "name";
	$Selector.filter = "(objectClass=msExchExchangeServer)";
	$Results = $Selector.findall();
	foreach($AdObj in $Results)
	{
		$AdObj.properties | Write-Output;
	}
}
 
function GetMailboxesOnServer($server)
{
	$RootPath = "GC://" + $RootDseObj.rootDomainNamingContext;
	$RootObj = New-Object DirectoryServices.DirectoryEntry $RootPath;
	$Selector = New-Object DirectoryServices.DirectorySearcher;
	$Selector.SearchRoot = $RootObj;
	$Selector.pagesize = 1000;
	$Selector.filter = "(msExchHomeServerName="+$server["legacyexchangedn"]+")";
	$Results = $Selector.findall();
	$server["name"][0] + ": " + $Results.Count;
}
 
cls;
$RootDseObj = New-Object DirectoryServices.DirectoryEntry "LDAP://RootDSE";
GetExchangeServers | foreach {GetMailboxesOnServer($_)};

Posted in Powershell, Scripting | No Comments »

Querying WSUS with Powershell

Wednesday, February 11th, 2009

Out of the box, WSUS is capable of reporting update status in a number of permutations, but I always seem to need some combination that I can’t easily derive using the GUI.

I recently needed to determine the status of a particular update (KB958644) for a number of computers that spanned different WSUS groups. I knew that there was a .NET API for WSUS management and so Powershell seemed to the most appropriate tool to use. I found this script example which got me started.

After a bit of head scratching I came up with the script below. It reads a list of computer names from a plain text file (Computers.txt) and calls the GetUpdateStatus function for each computer listed, passing the computer name and update string to the function.

The GetUpdateStatus function pulls the computer object from WSUS and then attempts to locate the update specified by the string. The status of the update is then returned.

[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null
 
if (!$wsus) {$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer();}
 
function GetUpdateStatus([string]$a, [string]$b)
{
  $ComputerScope = new-object Microsoft.UpdateServices.Administration.ComputerTargetScope;
  $ComputerScope.NameIncludes = $a;
 
  $UpdateScope = new-object Microsoft.UpdateServices.Administration.UpdateScope;
  $UpdateScope.ExcludedInstallationStates = [Microsoft.UpdateServices.Administration.UpdateInstallationStates]::NotApplicable;
  $UpdateScope.TextIncludes  = $b;
 
  $Computers = $wsus.GetComputerTargets($ComputerScope);
 
  $Computers | foreach-object{
    $_.FullDomainName + " (Status @ " + $_.LastReportedStatusTime + ")" | write-host;
 
    $Updates = $_.GetUpdateInstallationInfoPerUpdate($UpdateScope);
    $Updates | foreach-object{
      $Update = $wsus.GetUpdate($_.UpdateId);
      "   "+$Update.Title+"   {"+$_.UpdateInstallationState +"}" | write-host;
    }
  }
}
 
cls
Get-Content Computers.txt | Foreach-Object {GetUpdateStatus $_ "KB958644"}

Posted in Powershell, Scripting | No Comments »

  • Search

  • You are currently browsing the archives for the Scripting category.

  • Pages

    • Dad’s Army
    • Invoices
    • Nick Brown
  • Archives

    • November 2009
    • April 2009
    • February 2009
    • January 2009
  • Categories

    • Open Audit (1)
    • Scripting (2)
      • Powershell (2)
    • Windows (2)
      • Deployment (1)
      • Vista (1)
 

©2007 All Rights Reserved.  •  Design by Free CSS Templates  •  Icons by FAMFAMFAM.