Appendix A. Windows PowerShell source code for core component runbooks

Get Runbook Path

$Url = "{Orchestrator Web Service URL from "Initialize Data"}"
$ActivityID = "{Runbook Activity ID from "Initialize Data"}"
$RootPath = "{Root Path from "Initialize Data"}"

$ErrorState = 0
$ErrorMessage = ""
$Action = "Get Runbook Path"
$Trace = "Begin '$Action' `r`n"

try
{
    $Trace += "Calling Get-ActivityRunbookPath with URL '$Url' and ActivityID '$ActivityID'. `r`n"
    if(!($Url.EndsWith('/') -Or $Url.EndsWith(''))) {
        $Url += "/"
    }
    $Url = $Url + "Activities(guid'$ActivityID')/Runbook"
    $Trace += "Completed URL: $Url `r`n"
    $URI = New-Object System.Uri($Url,$true)

    $counter = 0
    $max = 6
    $completed = $false
    while ($counter -le $max -and !$completed)
    {
        try
        {
            $counter ++;
            #Create a request object using the URI
            $request = [System.Net.HttpWebRequest]::Create($URI)
            #Build up a nice User Agent
            $request.UserAgent = $(
                "{0} (PowerShell {1}; .NET CLR {2}; {3})" -f $UserAgent, $(if($Host.Version){$Host.Version}else{"1.0"}),
                [Environment]::Version,
                [Environment]::OSVersion.ToString().Replace("Microsoft Windows ", "Win")
                )

            $request.UseDefaultCredentials = $true

            [System.Net.HttpWebResponse] $response = [System.Net.HttpWebResponse] $request.GetResponse()
            $completed = $true
        }
        catch
        {
            if ($counter -eq $max)
            {
                Throw "$($_.Exception.Message)"
            }
            else
            {
                $Trace += [DateTime]::Now.ToString() + " $($_.Exception.Message)... Trying again.`r`n"
                sleep 10
            }
        }
    }

    $reader = [IO.StreamReader] $response.GetResponseStream()

    [xml]$output = $reader.ReadToEnd()
    $reader.Close()

    $response.Close()
    $path = $RootPath + $output.Entry.Content.properties.Path
    $Trace += "Path to return: $path `r`n"
    $Trace += "Completed $Action `r`n"
}
catch
{
   $ErrorState = 2;
   $ErrorMessage = $error[0].Exception.tostring()
   $Trace += "Error running '$Action'. `r`n"
}
finally
{
   $Trace += "Exiting  '$Action' `r`n"
   $Trace += "ErrorState:   $ErrorState`r`n"
   $Trace += "ErrorMessage: $ErrorMessage`r`n"
}

Get Relative Folder

$startPath = "{Start Folder from "Initialize Data"}"
$ancestors = {Ancestor Count from "Initialize Data"}
$childPath = "{Child Folder Path from "Initialize Data"}"

$Action = "Get Relative Folder"
$ErrorState = 0
$ErrorMessage = ""
$Trace = "Starting $Action`n`n"

try
{
    if (!$childPath.StartsWith(""))
    {
       $childPath = "" + $childPath
    }

    $Trace += "Validating $startPath exists `r`n"
    $directory = [System.IO.DirectoryInfo]$startPath

    $Trace += "Getting $ancestors directory parents`r`n"
    while ($ancestors -gt 0)
    {
        $directory = $directory.Parent
        $ancestors--
    }

    $Trace += "Appending $childPath to new path and validating existance`r`n"
    $newPath = $directory.FullName + $childPath
    $newDirectory = [System.IO.DirectoryInfo]$newPath
    if (!$newDirectory.Exists)
    {
        throw "New path not found! $newPath"
    }

    $Trace += "Completing $Action`r`n"
    $DirectoryPath = $newDirectory.FullName
}
catch
{
    $ErrorState = 2
    $ErrorMessage = $error[0].Exception.tostring()
    $Trace += "Error caught in $Action`r`n"
}
finally
{
    $Trace += "Exiting $Action `r`n"
    $Trace += "ErrorState:   $ErrorState`r`n"
    $Trace += "ErrorMessage: $ErrorMessage`r`n"
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset