Follow

Example Powershell Script

Here's an example Powershell script that gives a flavour of this functions available.

Specifically, it:

  1. Gets the Target Environment details
  2. Writes details to the emdash Job Log
  3. Gets parameters passed from the workflow
  4. Gets System Wide Settings
  5. Gets Environment Wide Settings
  6. Loops through the Servers, Services, Users, Groups and Applications allocated to an environment.

Here's the Powershell Script in text form which you can copy/paste into your environment as a basis for more interesting steps!

#################################################################################
#
# emdash - Custom Powershell Script (Overview).
#
# Colin Jones, 2017-04-19
#
# -------------------------------------------------------------------------------
#
# This script gives an overview of some of the key concepts that are used in
# Custom Powershell scripts in emdash.
#
# It is an example script that shows how you can derive environment data from
# emdash and use that in a custom step.
#################################################################################

[CmdletBinding()]
Param(

[Parameter(Mandatory=$True)]
[emDash.Workflow.Step.Interface.PowershellContext]$ctx,

[Parameter(Mandatory=$True,HelpMessage="An integer value.")]
[int]$intParam,

[Parameter(Mandatory=$True,HelpMessage="A string value.")]
[string]$stringParam,

[Parameter(Mandatory=$True,HelpMessage="A boolean value.")]
[boolean]$booleanParam,

[Parameter(Mandatory=$True,HelpMessage="A date time value.")]
[DateTime]$dateTimeParam

)

try
{
$emdashStepName="SampleCustomPowerShellScript-Overview"
$emdashStepVersion="0.1-InitialVersion"

Get-Module -ListAvailable | Import-Module -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null

# Get the Target ENVIRONMENT object
$targetEnv=$ctx.TargetEnvironment

# Log out some JOB DETAILS
$ctx.LogProgress("Running Sample Job Step in workflow: " +$ctx.WorkflowName)
$ctx.LogProgress("Job Step Id: " +$ctx.JobStepId)
$ctx.LogProgress("Environment Name: " +$targetEnv.Name)

# Log out the JOB PARAMETERS
$ctx.LogProgress("Parameters Passed into the Job....")
$ctx.LogProgress("intParam - " + $intParam)

$ctx.LogProgress("initParam - " + $intParam)
$ctx.LogProgress("stringParam - " + $stringParam)
$ctx.LogProgress("booleanParam - " + $booleanParam)
$ctx.LogProgress("dateTimeParam - " + $dateTimeParam)

# LOOP THROUGH ENVIRONMENT ALLOCATIONS....
# DATABASES
$ctx.LogProgress("Looping through all Databases...")
Foreach ($dbAlloc in $targetEnv.DatabaseAllocations)
{
$ctx.LogProgress("Database Name: " +$dbAlloc.Databases.Name)
}

# SERVERS
$ctx.LogProgress("Looping through all Servers...")
Foreach ($serverAlloc in $targetEnv.ServerAllocations)
{
$ctx.LogProgress("Server Name: " +$serverAlloc.Servers.Name)
}

# USERS
$ctx.LogProgress("Looping through all Users...")
Foreach ($userAlloc in $targetEnv.UserAllocations)
{
$ctx.LogProgress("User Name: " +$userAlloc.User.DisplayName)
}

# GROUPS
$ctx.LogProgress("Looping through all Groups...")
Foreach ($groupAlloc in $targetEnv.GroupAllocations)
{
$ctx.LogProgress("Group Name: " +$groupAlloc.Groups.Name)
}

# APPS
$ctx.LogProgress("Looping through all Applications and showing Code Version...")
Foreach ($appAlloc in $targetEnv.ApplicationAllocations)
{
$ctx.LogProgress("Application Name: " +$appAlloc.Applications.Name +" Code Version: " +$appAlloc.CodeVersions.Name)
}

# SYSTEM WIDE SETTINGS
$devOU = $ctx.GetSystemWideSettingByName("DEVOU","defaultValue")
$ctx.LogProgress("System Wide Setting DEVOU is " +$devOU)

# ENVIRONMENT WIDE SETTING (Mapped against the environment)
$envSpreadPort = $ctx.GetEnvironmentWideSettingByName("EnvSpreadPort","defaultValue","true")
$ctx.LogProgress("Getting Env Wide Setting mapped to environment: EnvSpreadPort... " +$envSpreadPort)

# ENVIRONMENT WIDE SETTING (Not mapped to Environment - go to Env Wide Setting Option)
$endurLicenceFile = $ctx.GetEnvironmentWideSettingByName("EndurLicenceFile","defaultValue","true")
$ctx.LogProgress("Getting Env Wide Setting where it is not mapped to the env: EndurLicenceFile... " +$endurLicenceFile)

Return $True

}
catch [Exception]
{
Write-Error ("Error: {0} {1}" -f $_.Exception.Message, $_.Exception.StackTrace)
Return $False
}

 

(Note: Don't forget to run this command in Powershell ISE on an emdash Server first to enable Intellisense)

[reflection.assembly]::LoadFrom("C:/emDash/workflow.service/emDash.Workflow.Step.Interface.dll")
[reflection.assembly]::LoadFrom("C:/emDash/workflow.service/emDash.Models.dll")

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

0 Comments

Article is closed for comments.
Powered by Zendesk