Skip to main content

Posts

Showing posts from July, 2012

Add-PSTypeAccelerator

This is one of my most favorite PS hacks. It allows you to add a type alias, similar to [PSObject], [ADSI], or [WMI].
param(      [Parameter(Mandatory = $true, ValueFromPipeline = $true)]      [ValidateNotNull()]      [Type] $Type,      [Parameter(ValueFromPipelineByPropertyName = $true)]      [ValidateNotNullOrEmpty()]      [string] $Name = $Type.Name  )  BEGIN {  $ErrorActionPreference = 'Stop'$PSTypeAccelerators = [Type]::GetType("System.Management.Automation.TypeAccelerators, $([PSObject].Assembly.FullName)")  }  PROCESS {  if ($PSTypeAccelerators::Add) {  $PSTypeAccelerators::Add($Name, $Type)      } elseif ($PSTypeAccelerators::AddReplace) {  $PSTypeAccelerators::AddReplace($Name, $Type)      }  }   You can add a single alias:
Add-PSTypeAccelerator System.Management.Automation.PSCredential   Or an entire namespace or assembly (by removing the namespace filter):
[System.Reflection.Assembly]::LoadWithPartialName('System.Messaging').GetTypes() |? { $_.N…

Failover Cluster Node Maintenance Automation

I have been using the Exchange 2010 scripts to start/stop DAG member maintenance as well as redistribution of databases to preferred owners after maintenance is complete.

I was doing some work on several failover cluster nodes, and thought, it'd be nice to have the same functionality. The ability to pause a node, move all the cluster resources off it, stop the maintenance, and then to redistribution the cluster groups to the preferred nodes.

So, I present the below three scripts to perform these tasks on Windows Server 2008 R2 failover clusters.

[Start-ClusterNodeMaintenance.ps1]
param([string] $Node = $ENV:COMPUTERNAME, [string] $Cluster = '.')  Import-Module FailoverClusters  Suspend-ClusterNode -Name $Node -Cluster $ClusterGet-ClusterNode -Name $Node -Cluster $Cluster | Get-ClusterGroup -Cluster $Cluster | Move-ClusterGroup -Cluster $Cluster [Stop-ClusterNodeMaintenance.ps1]
param([string] $Node = $ENV:COMPUTERNAME, [string] $Cluster = '.')  Import-Module Failover…

Synchronize Windows Features and Roles

I didn't see anything specific to this with a quick Google, so I thought I'd post this. Perhaps, it will help other administrators replicate/copy Windows features and roles to multiple servers from a source server. In my particular case I needed to copy the features and roles from a test environment server to a certification server.

This will add new features and roles as well as remove any not in use from the source to the localhost.

param([string] $Source)  Import-Module ServerManager  $features = Invoke-Command -Computer $Source -Script {      Import-Module ServerManager      Get-WindowsFeature  }  $features |? { $_.Installed -and $_.SubFeatures.Count -eq 0} | Add-WindowsFeature  $features |? { !$_.Installed } | Remove-WindowsFeature  
Note: The Remove-WindowsFeature may fail if a reboot is required after the Add-WindowsFeature completes. Just run the command again after the server is rebooted.

Note: The script had to select only features that have no subfeatures to install,…