PowerShell: Execute Managed Software Delivery task with policy check
[CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName="None")]
PARAM()
function Get-PolicyStatus {
[CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName="None")]
PARAM(
[Alias("Guid")]
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ParameterSetName="p1")]
[guid]
$PolicyGuid
)
try {
Write-Verbose "Policy Guid: $($PolicyGuid.ToString('B').ToUpper())"
# Get the current execution user context
$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
Write-Verbose "Running as: $($CurrentUser)"
# Connect to the local base agent
$IAeXClient = New-Object -ComObject Altiris.AeXClient -ErrorAction Stop
# Get the Web service for the default server
$DefaultWebSerivceUri = $IAeXClient.ServerWeb
Write-Verbose "Default Web Service URI: $($DefaultWebSerivceUri)"
if ($PolicyGuid -eq '{00000000-0000-0000-0000-000000000000}') {
$PolicyEnabled = $false
} else {
$Stopwatch = [Diagnostics.Stopwatch]::StartNew()
$ItemManagementService = "$($DefaultWebSerivceUri)/ASDK.NS/ItemManagementService.asmx"
$ItemManagementServiceProxy = New-WebServiceProxy -Uri $ItemManagementService Namespace WebServiceProxy -UseDefaultCredential
$ItemManagementServiceProxy.EnableDecompression = $true
Write-Verbose "Connected to: $($ItemManagementServiceProxy.Url)"
$ItemDetails = $ItemManagementServiceProxy.GetItemByGuid($PolicyGuid)
Write-Verbose "ItemManagementService call: $($Stopwatch.Elapsed)"
$PolicyEnabled = $ItemDetails.Enabled
Write-Verbose "Policy Name: $($ItemDetails.Name)"
}
Write-Verbose "Policy Enabled: $($PolicyEnabled)"
$PolicyEnabled
} catch {
$false
} finally {
if ($Stopwatch) {
$Stopwatch.Stop()
$Stopwatch = $null
}
}
}
function Get-RunningPolicyGuid {
[CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName="None")]
PARAM()
$parentPolicyGuid = "{00000000-0000-0000-0000-000000000000}"
try {
# Connect to the local base agent
$IAeXClient = New-Object -ComObject Altiris.AeXClient -ErrorAction Stop
# Connect to the local software delivery agent
$IAeXSWDAgent = $IAeXClient.Agent("Altiris.SWD")
# Get the advertisementId Guid of the currently running policy
$advertisementId = ([Guid]$IAeXSWDAgent.RunningProgram.Id).ToString('B').ToUpper()
Write-Verbose "advertisementId: $($advertisementId)"
if ($advertisementId) {
# Get Software Delivery directories (they can span multiple drives
$SWDInstallDirs = $IAeXSWDAgent.Packages | ? {$_.CacheFolder} | Select -ExpandProperty CacheFolder | Select @{Name="SWDInstallDir";Expression={$_.Substring(0,$_.indexof('{'))}} -Unique | Select -ExpandProperty SWDInstallDir
foreach ($SWDInstallDir In $SWDInstallDirs) {
$AeXSWDPolicyXmlPath = "$($SWDInstallDir)AeXSWDPolicy.xml"
Write-Verbose "Testing: $($AeXSWDPolicyXmlPath)"
# Test that the path exists
if (Test-Path -Path $AeXSWDPolicyXmlPath -PathType Leaf) {
Write-Verbose "Getting contents"
# Get the contents of the XML file as XML
$AeXSWDPolicyXml = [xml](Get-Content -Path $AeXSWDPolicyXmlPath)
# Locate the policy guid for the advertisement
$parentPolicyGuid = $AeXSWDPolicyXml.SWD.Advertisements.Advertisement | ? {$_.Id -eq $advertisementId} | Select -ExpandProperty "parentPolicyGuid"
if ($parentPolicyGuid) {
Write-Verbose "Policy Guid found. ($($parentPolicyGuid))"
break
}
}
}
}
$parentPolicyGuid
} catch {
$parentPolicyGuid
} finally {
if ($IAeXClient) {
# Destroy the COM Object if it exists
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($IAeXClient) | Out-Null
}
}
}
function Set-ExitCodeAndExit {
[CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName="None")]
PARAM(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ParameterSetName="p1")]
[int]
$ExitCode
)
Write-Verbose "Setting ExitCode to $($ExitCode) and exiting..."
$host.SetShouldExit($ExitCode)
exit $ExitCode
}
$RunningPolicyGuid = Get-RunningPolicyGuid -Verbose:$VerbosePreference
$TranscriptFilePath = "$($env:WINDIR)\System32\LogFiles\MSDPolicyEnabled-$((Get-Date -format MMM).ToUpper()).log"
try {
if ($RunningPolicyGuid) {
try {
Start-Transcript -Path $TranscriptFilePath -Force -Verbose -Append
$PolicyEnabled = Get-PolicyStatus -PolicyGuid $RunningPolicyGuid -Verbose:$VerbosePreference
if ($PolicyEnabled) {
$Process = Start-Process -FilePath "$(Split-Path $Script:MyInvocation.MyCommand.Path)\MSDPolicyEnabled.cmd" -Wait -NoNewWindow -PassThru
Set-ExitCodeAndExit -ExitCode $Process.ExitCode -Verbose:$VerbosePreference
} else {
Set-ExitCodeAndExit -ExitCode 1001 -Verbose:$VerbosePreference
}
} catch {
} finally {
try {
Stop-Transcript
} catch {}
}
} else {
Set-ExitCodeAndExit -ExitCode 1001 -Verbose:$VerbosePreference
}
} catch {
} finally {
# Write-Host "Press any key to continue ..."
# $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}