Script for failing over Windows Clusters

by dmittel at 2013-01-15 08:28:15

I hope this is getting posted in the right section. The issue I’m having is I’m trying to write a script that I can use to fail over a group of servers and I want to make sure that if the server is already on the preferred node that it will not get failed over. Here is the script that I have:

Thanks, but I have actually changed up my script a little bit, and now it is breaking at a different place. Maybe you could explain why I am getting the following error. Here is my script:

import-module failoverclusters

$i=0
$clustername = get-content “c:\ClusterName.txt”
$Oddservers = get-content @(“c:\Odds.txt”)


Foreach ($cname in $clustername)
{
$PreferredOwner = Get-clusterownernode -cluster $cname -group $oddservers[$i] | select-object -ExpandProperty OwnerNodes -Unique
$CurrentOwner = get-clustergroup -cluster $cname -name $oddservers[$i] | select-object -ExpandProperty OwnerNode -Unique
if ($preferredOwner -eq $Currentowner)
{$i++}
elseif ($PreferredOwner -ne $CurrentOwner)
{
move-clustergroup -cluster $cname -name $Oddservers[$i]
$i++
}
}


When it runs through the line $CurrentOwner = get-clustergroup -cluster $cname -name $oddservers[$i] | select-object -ExpandProperty OwnerNode -Unique I get the following error on my screen:


[DBG]>>> Hit Line breakpoint on c:\test_script.ps1:10’
[DBG]>>> Stopped at: if ($.FullyQualifiedErrorId -ne “NativeCommandErrorMessage” -and $ErrorView -ne “CategoryView”) {
[DBG]>>> Stopped at: $myinv = $
.InvocationInfo
[DBG]>>> Stopped at: switch -regex ( & { Set-StrictMode -Version 1; $myinv.MyCommand.CommandType } )
[DBG]>>> Stopped at: switch -regex ( & { Set-StrictMode -Version 1; $myinv.MyCommand.CommandType } )
[DBG]>>> Stopped at: switch -regex ( & { Set-StrictMode -Version 1; $myinv.MyCommand.CommandType } )
[DBG]>>> Stopped at: if ( & { Set-StrictMode -Version 1; $myinv.MyCommand.Name } )
[DBG]>>> Stopped at: if ( & { Set-StrictMode -Version 1; $myinv.MyCommand.Name } )
[DBG]>>> Stopped at: if ( & { Set-StrictMode -Version 1; $myinv.MyCommand.Name } )
[DBG]>>> Stopped at: $myinv.MyCommand.Name + " : "; break;
[DBG]>>> Stopped at: $myinv.MyCommand.Name + " : "; break;
[DBG]>>> Stopped at: if ($.FullyQualifiedErrorId -eq “NativeCommandErrorMessage”) {
[DBG]>>> Stopped at: if ($
.InvocationInfo) {
[DBG]>>> Stopped at: $posmsg = $.InvocationInfo.PositionMessage
[DBG]>>> Stopped at: if ( & { Set-StrictMode -Version 1; $
.PSMessageDetails } ) {
[DBG]>>> Stopped at: if ( & { Set-StrictMode -Version 1; $.PSMessageDetails } ) {
[DBG]>>> Stopped at: if ( & { Set-StrictMode -Version 1; $
.PSMessageDetails } ) {
[DBG]>>> Stopped at: & { Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails }
[DBG]>>> Stopped at: & { Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails }
[DBG]>>> Stopped at: & { Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails }
[DBG]>>> Stopped at: $indent = 4
[DBG]>>> Stopped at: $width = $host.UI.RawUI.BufferSize.Width - $indent - 2
[DBG]>>> Stopped at: $indentString = "+ CategoryInfo : " + $.CategoryInfo
[DBG]>>> Stopped at: $posmsg += “n&quot;<br>&#91;DBG&#93;&gt;&gt;&gt; Stopped at&#58; foreach($line in @($indentString -split &quot;(&#46;{$width})&quot;)) { if($line) { $posmsg += (&quot; &quot; * $indent + $line) } }<br>&#91;DBG&#93;&gt;&gt;&gt; Stopped at&#58; foreach($line in @($indentString -split &quot;(&#46;{$width})&quot;)) { if($line) { $posmsg += (&quot; &quot; * $indent + $line) } }<br>&#91;DBG&#93;&gt;&gt;&gt; Stopped at&#58; foreach($line in @($indentString -split &quot;(&#46;{$width})&quot;)) { if($line) { $posmsg += (&quot; &quot; * $indent + $line) } }<br>&#91;DBG&#93;&gt;&gt;&gt; Stopped at&#58; foreach($line in @($indentString -split &quot;(&#46;{$width})&quot;)) { if($line) { $posmsg += (&quot; &quot; * $indent + $line) } }<br>&#91;DBG&#93;&gt;&gt;&gt; Stopped at&#58; $indentString = &quot;+ FullyQualifiedErrorId &#58; &quot; + $_&#46;FullyQualifiedErrorId<br>&#91;DBG&#93;&gt;&gt;&gt; Stopped at&#58; $posmsg += &quot;n”
[DBG]>>> Stopped at: foreach($line in @($indentString -split “(.{$width})”)) { if($line) { $posmsg += (" " * $indent + $line) } }
[DBG]>>> Stopped at: foreach($line in @($indentString -split “(.{$width})”)) { if($line) { $posmsg += (" " * $indent + $line) } }
[DBG]>>> Stopped at: foreach($line in @($indentString -split “(.{$width})”)) { if($line) { $posmsg += (" " * $indent + $line) } }
[DBG]>>> Stopped at: foreach($line in @($indentString -split “(.{$width})”)) { if($line) { $posmsg += (" " * $indent + $line) } }
[DBG]>>> Stopped at: if ($ErrorView -eq “CategoryView”) {
[DBG]>>> Stopped at: elseif (! $
.ErrorDetails -or ! $.ErrorDetails.Message) {
[DBG]>>> Stopped at: $
.Exception.Message + $posmsg + "`n "
Get-ClusterGroup : Cannot convert ‘test-vps-2008-b’ to the type ‘System.Collections.Specialized.StringCollection’ required by parameter ‘Name’. Unable to cast object of type ‘System.Management.Automation.PSObject’ to type ‘System.String’.
At c:\test_script.ps1:13 char:63
+ $CurrentOwner = get-clustergroup -cluster $cname -name <<<< $oddservers[$i] | select-object -ExpandProperty OwnerNode -Unique
+ CategoryInfo : InvalidArgument: (:slight_smile: [Get-ClusterGroup], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.FailoverClusters.PowerShell.GetClusterGroupCommand


Any help would be much appreciated.

Thanks
by DonJ at 2013-01-15 14:43:20
It looks like you have a breakpoint defined. Run Get-PSBreakpoint to check. If it’s the [DBG] output you’re concerned about.

The error is because you’re passing a PSObject to -Name, but it wants a String collection. In other words, the object in $oddserver[$i] isn’t what -Name wants.

The problem may be how you’re wrapping the command in @(). Get-Content already returns an array of strings; you’ve put that array into another array. Try removing the @().
by dmittel at 2013-01-15 16:08:28
Thanks Don, I tried that, but it still did not work. I even tried setting the array into another variable and that is where I see another problem.

When I tried
Thanks, but I have actually changed up my script a little bit, and now it is breaking at a different place. Maybe you could explain why I am getting the following error. Here is my script:

import-module failoverclusters

$i=0
$clustername = get-content “c:\ClusterName.txt”
$Oddservers = get-content "c:\Odds.txt"


Foreach ($cname in $clustername)
{
$Oddservers[$i] = $oserver
$PreferredOwner = Get-clusterownernode -cluster $cname -group $oserver | select-object -ExpandProperty OwnerNodes -Unique
$CurrentOwner = get-clustergroup -cluster $cname -name $oserver | select-object -ExpandProperty OwnerNode -Unique
if ($preferredOwner -eq $Currentowner)
{$i++}
elseif ($PreferredOwner -ne $CurrentOwner)
{
move-clustergroup -cluster $cname -name $Oddservers[$i]
$i++
}
}


It wasnt passing the value to the new variable. It didnt matter if wrapped the Get-Content in the @() or not. What else might I be missing?
by DonJ at 2013-01-15 16:41:31
What does that text file contain?
by dmittel at 2013-01-15 22:24:26
The odds.txt file just contains a list of the nodes:

test-vps-2008-b
test-vps-2008-d

The clusternames.txt file contains a list of the cluters

c-test-ps-ab
c-test-ps-dc