It's not as straightfoward as you might think
I recently had to run a .cmd script on a cluster to determine the size of the cluster resource. Running the script remotely took more than two hours and produced a lot of network traffic, so I decided to run the script locally on each cluster node.
Running a script on a cluster might seem like it should take the same amount of effort as running a script on a standalone server, but that's not the case. There are extra steps you need to take when scheduling a script or another process to run on a cluster. Because there's the possibility of a cluster resource failing over to another cluster node, you need to make sure that the script exists on all the nodes that the resource could possibly fail over to. However, you also need to make sure that the script executes only on the active cluster node. You can accomplish both objectives using a second script like the FindClusterResource.vbs script in Listing 1.
Listing 1: FindClusterResource.vbs
FindClusterResource.vbs checks for the existence of the resource drive. When the resource drive exists, it runs the primary script SpaceReport.cmd. When the resource drive doesn't exist, FindClusterResource.vbs ends and SpaceReport.cmd doesn't run. Because the cluster resource can reside on only one node at any given time, SpaceReport.cmd executes only on the node where the cluster resource resides.
Callout A in Listing 1 highlights the test used to determine whether the resource drive exists. In this test, FindClusterResource.vbs calls the GetDrive method of the FileSystemObject object, which is part of the Microsoft Scripting Runtime Library in Windows Script Host (WSH). The GetDrive method returns a Drive object for the drive in a specified path. When the drive doesn't exist, an error occurs, which is indicated by a value of 0. So, after the GetDrive call, FindClusterResource.vbs tests for a value of 0. When this value is present (i.e., the resource drive doesn't exist), FindClusterResource.vbs ends. When this value isn't present (i.e., the resource drive exists), FindClusterResource.vbs uses the WshShell object's Run method to run SpaceReport.cmd as a new process on that node.
You can download FindClusterResource.vbs by clicking the Download the Code Here button at the top of the page. To use this script, you need to customize it in two spots. First, in the code
TaskName1 = "C:\Scripts\SpaceReport\SpaceReport.cmd"
replace C:\Scripts\SpaceReport\SpaceReport.cmd with the pathname to the primary script. Second, in the code
ResourceDrv = "X:"
replace X with your resource drive letter.
After you make these two changes, place FindClusterResource.vbs and the primary script in the same folder on each node in the cluster. Then use the Task Scheduler to schedule FindClusterResource.vbs. When FindClusterResource.vbs runs, it will execute your primary script, no matter where the cluster resource resides. Note that if a failover occurs during the scheduled task, the primary script will fail. However, this scenario is unlikely and there's little you can do to compensate for it.