Windows 2008 – Tasks on a cluster node

by luca
3 comments

Working with a Windows cluster, I needed a way to run scheduled tasks only on the cluster node that is owner of a given cluster group.

In Windows 2003 it was possible to configure Task Scheduler as a cluster resource; I wasn’t able to find a way to do the same in Windows 2008.

I created a simple PowerShell script that checks if the node is cluster group’s owner and, only if it is, runs the command you pass to the script:

# Cluster management cmdlets
Import-Module FailoverClusters
 
Write-Host "runTask_Cluster 1.0"
Write-Host
 
# Check if command was passed as parameter
If($args.Length -ne 1) 
{
  Write-Host "Usage: runTask_Cluster.ps1 <command>"
  Exit 1
}
$cmd = $args[0]
 
# Cluster group name
$groupName = "PRODSASCMFE"
Write-Host "Cluster group:`t$groupName"
 
# Get server name
$serverName = gc env:computername
Write-Host "Server name:`t$serverName"
 
# Get cluster group's owner
$groupOwner = (Get-ClusterGroup -name $groupName).OwnerNode.Name.ToUpper()
Write-Host "Group owner:`t$groupOwner"
Write-Host
 
# Check if actual server is cluster group's owner
If($groupOwner -ieq $serverName) 
{
  Write-Host "Server is cluster group's owner, running the command:"
  Write-Host $cmd
  & $cmd
  Write-Host
  Write-Host "Done!"
}
 
Else 
{
  Write-Host "Server is not cluster group's owner, stop."
}

For example, I wrote a simple cmd that prints Hello World:

and I ran it using my script on a cluster node, when this is cluster group’s owner and when it is not:

Related Posts

3 comments

Thomas Thursday March 28th, 2013 - 12:29 AM

I like this script but I can’t get it to work. I get the following error. Got any ideas?

Cluster group: JAGUAR2
Server name: CFSPRODSQL1
Get-ClusterGroup : An error occurred opening the clustered service or application ‘JAGUAR2’.
At C:\scripts\runTask_Cluster.ps1:24 char:16
+ $groupOwner = (Get-ClusterGroup -name $groupName).OwnerNode.Name.ToUpper()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ClusterGroup], ClusterCmdletException
+ FullyQualifiedErrorId : Get-ClusterGroup,Microsoft.FailoverClusters.PowerShell.GetClusterGroupCommand

You cannot call a method on a null-valued expression.
At C:\scripts\runTask_Cluster.ps1:24 char:1
+ $groupOwner = (Get-ClusterGroup -name $groupName).OwnerNode.Name.ToUpper()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Group owner:

Server is not cluster group’s owner, stop.
PS C:\Users\snapdrive.COOPFS.000>

Reply
Thomas Thursday March 28th, 2013 - 05:54 PM

I fixed it. the name of the cluster group was incorrect, once I fixed that the script works.

Reply
luca Thursday March 28th, 2013 - 08:53 PM

great, thanks for sharing your experience!

Reply

Leave a Reply to Thomas Cancel Reply

three × three =