Monday, 15 December 2014

Running WFA PowerShell Code in Native PowerShell


I’ve written a load of CDOT Synchronization commands that run in WFA, and form complete synchronization workflows. But, I want to be able to test, tinker, and perfect every CDOT Synchronization command, outside of WFA, before they are let loose on production systems. And I don’t want to have to maintain two pieces of code, one for inside WFA, and one for outside.

Here’s a super simple solution! Copy the below into a text editor and save as ConvertWfaPS.ps1

## ConvertWfaPS.ps1 ##

Function Connect-WfaCluster {
       Param([String]$Cluster)
       $Username = "YOUR-ADMIN-USER-HERE"
       $Password = "YOUR-ADMIN-PASS-HERE"
       $Cred = New-Object System.Management.Automation.PsCredential($Username,($Password | ConvertTo-SecureString  -AsPlainText -Force))
       [Void](Connect-NcController -Name $Cluster -Credential $Cred)
}

Function Get-WFALogger {
       Param([String]$Message)
       Write-Host $Message
}

## End of Script ##

Replace the contents of $Username and $Password with an account and password that has sufficient permissions to your CDOT clusters.

Then load the function into PowerShell with (there’s a space between the . .):

. .\ConvertWfaPS.ps1

Then grab the code from WFA, paste into a text editor and save as WHATEVER.ps1.
Then simply run that script including all mandatory parameters.

For example:
PS C:\WFA SYNCH> .\01-synch-cron.ps1 -SourceCluster PRCLU01 -DestinationCluster DRCLU01
<<<<< START: SYNCHRONIZE CRON SCHEDULES >>>>>
This task is TOTALLY SAFE for the Source Cluster. Tasks:
1) Connecting to Source Cluster = PRCLU01!
2) ONLY READ the Cron Schedules from PRCLU01!
3) Connecting to Destination Cluster = DRCLU01!
4) Add Cron Schedules that don't already exist and/or set them correctly.
Note: It does not delete Cron Schedules that exist on the destination but not the source!
===== STARTING TASKS =====
1) Connecting to Source Cluster = PRCLU01!
2) ONLY READ the Cron Schedules from PRCLU01!
3) Connecting to Destination Cluster = DRCLU01!
4) Add Cron Schedules that don't already exist and/or set them correctly.
>>>>> END: SYNCHRONIZE CRON SCHEDULES <<<<<

And this is the code from WFA which we just ran in Native PowerShell (it’s about the simplest CDOT Synchronization command I have - for synchronizing Cron Schedules):

param (
  [parameter(Mandatory=$true, HelpMessage="Source Cluster Name or IP address")]
  [string]$SourceCluster,
 
  [parameter(Mandatory=$true, HelpMessage="Destination Cluster Name or IP address")]
  [string]$DestinationCluster
)

## START ##
Get-WFALogger -Info -message $("<<<<< START: SYNCHRONIZE CRON SCHEDULES >>>>>")
Get-WFALogger -Info -message $("This task is TOTALLY SAFE for the Source Cluster. Tasks:")
Get-WFALogger -Info -message $("1) Connecting to Source Cluster = $SourceCluster!")
Get-WFALogger -Info -message $("2) ONLY READ the Cron Schedules from $SourceCluster!")
Get-WFALogger -Info -message $("3) Connecting to Destination Cluster = $DestinationCluster!")
Get-WFALogger -Info -message $("4) Add Cron Schedules that don't already exist and/or set them correctly.")
Get-WFALogger -Info -message $("Note: It does not delete Cron Schedules that exist on the destination but not the source!")
Get-WFALogger -Info -message $("===== STARTING TASKS =====")

## 1 ##
Get-WFALogger -Info -message $("1) Connecting to Source Cluster = $SourceCluster!")
Connect-WfaCluster $SourceCluster

## 2 ##
Get-WFALogger -Info -message $("2) ONLY READ the Cron Schedules from $SourceCluster!")
$SourceCronJobs = Get-NcJobCronSchedule

## 3 ##
Get-WFALogger -Info -message $("3) Connecting to Destination Cluster = $DestinationCluster!")
Connect-WfaCluster $DestinationCluster

## 4 ##
Get-WFALogger -Info -message $("4) Add Cron Schedules that don't already exist and/or set them correctly.")
$SourceCronJobs | foreach { $_.NcController = $null; [Void]( $_ | Add-NcJobCronSchedule -ErrorAction SilentlyContinue ) }
$SourceCronJobs | foreach { $_.NcController = $null; [Void]( $_ | Set-NcJobCronSchedule -ErrorAction SilentlyContinue ) }

## END ##
Get-WFALogger -Info -message $(">>>>> END: SYNCHRONIZE CRON SCHEDULES <<<<<")

And to prove the command ‘SYNCHRONIZE CRON SCHEDULES’ really does exist in WFA (and for reference also), here’s all the tabs from the Command Definition in WFA:

Image: Command Definition - Properties

Image: Command Definition - Code

Image: Command Definition - Parameters Definition

Image: Command Definition - Parameters Mapping

THE END!

No comments:

Post a Comment