MyITGuy
8/14/2016 - 3:35 PM

PowerShell: Get policy status of currently running program from SMP agent

PowerShell: Get policy status of currently running program from SMP agent

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