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
}
}
}