Task Name | Godd Powershell coding example with try and catch powershell concept |
Description | |
Prerequisites | Tested on UCSD 6.0.1.1 |
Category | Workflow |
Components | vSphere 5.x |
User Inputs | |
Instructions for Regular Workflow Use:
- Download the attached .ZIP file below to your computer. *Remember the location of the saved file on your computer.
- Unzip the file on your computer. Should end up with a .WFD file.
- Log in to UCS Director as a user that has "system-admin" privileges.
- Navigate to "Policies-->Orchestration" and click on "Import".
- Click "Browse" and navigate to the location on your computer where the .WFD file resides. Choose the .WFD file and click "Open".
- Click "Upload" and then "OK" once the file upload is completed. Then click "Next".
- Click the "Select" button next to "Import Workflows". Click the "Check All" button to check all checkboxes and then the "Select" button.
- Click "Submit".
- A new folder should appear in "Policies-->Orchestration" that contains the imported workflow. You will now need to update the included tasks with information about the specific environment.
This was inspired by Jeremiah! Thank you.
The workflow:

The task content:

The code in the Powershell task as ASCII:
Try {
echo "do something good" > c:\\myGoodFile.txt
echo "do something good" > c:\\myGoodFile.txt
echo "do something good" > c:\\myGoodFile.txt
badcommand
}
Catch {
$reportFile = "c:\\Error_ErrorLog.txt"
$report = @("####################")
$report += "Error Date: $(get-date -format G)"
$report += "_________________________"
$report += $error
$report += "###################"
$report | out-file -FilePath $reportFile -Append
#throw $error[0]
type $reportFile
}
The workflow execution:

The resulting log files on the Windows server:



Workflow execution if there is no error:

The If statement:


Custom Logger Code:
//Sometimes if the string may contain passwords then it does not write to the logger
logger.addInfo(">----------------------------------------------------------------<");
var a = input.LogFileEntry;
logger.addInfo(">"+ a +"<");
logger.addInfo(">----------------------------------------------------------------<");
// Created this section to write to log file anywhay
// Disable thei section when not debugging
var b = a.split("<");
logger.addInfo("b : " + b.length);
for ( var i=0; i < b.length; i++ ) {
logger.addInfo(">"+ b[i] +"<");
}
logger.addInfo(">----------------------------------------------------------------<");