Sunday, 14 December 2014

An Introduction to Converting PowerShell Scripts into WFA Commands

I’ve been regularly writing scripts in PowerShell, testing them in PowerShell, and then porting them into WFA. So, it seems like a good idea to document this process rather than having to tax my memory every time I need to convert something...

Code differences of Native PowerShell script v PowerShell Code in WFA

In the below example - looking at a CDOT Unix Groups Synchronization Script - the Native PowerShell specific code is highlighted in orange, and PowerShell code specific to WFA is highlighted in blue.

$Username = "USERNAME"
$Password = "PASSWORD"
$Cred = New-Object System.Management.Automation.PsCredential($Username,($Password | ConvertTo-SecureString  -AsPlainText -Force))

$SourceCluster = "NCA1"
$SourceSVM = "NCA1V1"
$DestinationCluster = "NCB1"
$DestinationSVM = "NCB1V1"

[Void](Connect-NcController -Name $SourceCluster -Credential $Cred)

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

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

  [parameter(Mandatory=$true, HelpMessage="Destination Vserver name")]
  [string]$DestinationSVM 
)

Connect-WfaCluster $SourceCluster

[Void](Connect-NcController -Name $DestinationCluster -Credential $Cred)

Connect-WfaCluster $DestinationCluster

"Unix Group $GroupName does not exist - CREATING!"

Get-WFALogger -Info -message $("Unix Group $GroupName does not exist - CREATING!")

"ADDING user $User to the Unix Group $GroupName!"

Get-WFALogger -Info -message $("ADDING user $User to the Unix Group $GroupName!")

"GroupId for $GroupName is not correct, SETTING!"

Get-WFALogger -Info -message $("GroupId for $GroupName is not correct, SETTING!")

"REMOVING user $DstUser from the Unix Group $GroupName!"

Get-WFALogger -Info -message $("REMOVING user $DstUser from the Unix Group $GroupName!")

"ADDING user $SrcUser to the Unix Group $GroupName!"

Get-WFALogger -Info -message $("ADDING user $SrcUser to the Unix Group $GroupName!")

"Group $GroupName does not exist on the source - REMOVING!"

Get-WFALogger -Info -message $("Group $GroupName does not exist on the source - REMOVING!")

"END of UNIX GROUPS SYNCH!"

Get-WFALogger -Info -message $("END of UNIX GROUPS SYNCH!")

Conversions from Native PoSH Code to WFA PoSH Code

Using the very simple example from above, essentially the required conversions can be broken down as below:

1) Credentials

Native Code:

$Username = "USERNAME"
$Password = "PASSWORD"
$Cred = New-Object System.Management.Automation.PsCredential($Username,($Password | ConvertTo-SecureString  -AsPlainText -Force))

WFA Code:

No requirement for any code since this is contained in the WFA database!

2) Input Variables

Native Code:

$SourceCluster = "NCA1"
$SourceSVM = "NCA1V1"
$DestinationCluster = "NCB1"
$DestinationSVM = "NCB1V1"

WFA Code:

For each input variable, the parameter syntax is required. Then when the command is run in WFA, it will expect these parameters to be fed in as inputs either via the GUI or other means.

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

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

  [parameter(Mandatory=$true, HelpMessage="Destination Vserver name")]
  [string]$DestinationSVM 
)

3) Connecting to CDOT Clusters

Native Code:

[Void](Connect-NcController -Name $SourceCluster -Credential $Cred)

WFA Code:

Every Connect-NcController is converted into a Connect-WfaCluster.

Connect-WfaCluster $SourceCluster

4) Outputs

Native Code:

"Unix Group $GroupName does not exist - CREATING!"

WFA Code:

Every screen output that’s contained in “”, is encapsulated with:

Get-WFALogger -Info -message $("OUTPUT GOES HERE")

Get-WFALogger -Info -message $("Unix Group $GroupName does not exist - CREATING!")

Final Comment and Future Developments

Hopefully the above gives an idea of how to convert your Native PowerShell scripts to work in WFA. The example above is very simplistic but hopefully you get the gist. One idea for a future mini project is to automate converting PowerShell scripts into WFA commands - potentially, if keeping to some standards when writing PowerShell scripts, this is do-able, especially generating the adjusted code to put into WFA.

Image: Where the adjusted PoSH code goes in WFA (2.2)

No comments:

Post a Comment