SysJam Powershell RightClick Tool – Part 5 – By-Passing User Logon Requirement for a Program

Most of the time when deploying software I’ll set the program to run only with the user logged off. This is to avoid situations when the user may have an older version of the application open when they receive the advertisement. For testing however, this can be a pain….which is why I included the “ByPass User Logon Requirement (Temporary)” button in the Sysjam Powershell RightClick tool. This button sets the requirement to “None” temporarily within WMI. The next time the system does a Machine Policy refresh this setting gets overwritten.

Setting it is quite simple:

Param (($strComputer, $AdvID)
				
$SBUserLogonReq = {
	Param ([string]$AdvID)
	$strQuery = "Select * from CCM_SoftwareDistribution where ADV_AdvertisementID='" + $AdvID + "'"
        Try{
		Get-WmiObject -Namespace "root\CCM\Policy\Machine\ActualConfig" -Query $strQuery | ForEach-Object {
			$_.PRG_PRF_UserLogonRequirement = "None"
			[Void]$_.Put()
		}
		return $true
	}
	Catch [Exception]{
		return $false
	}

}
Invoke-Command -ComputerName $strComputer -ArgumentList $AdvID -ScriptBlock $SBUserLogonReq

Lets break this down.

First, our function or scriptblock takes a computer and an the advertisement ID as input.

Param (($strComputer, $AdvID)

Then, we’ll get all programs associated with the advertisement from WMI:

$strQuery = "Select * from CCM_SoftwareDistribution where ADV_AdvertisementID='" + $AdvID + "'"
Get-WmiObject -Namespace "root\CCM\Policy\Machine\ActualConfig" -Query $strQuery

Take each program associated with the advertisement ID provided and set the UserLogonRequirement:

ForEach-Object {
	$_.PRG_PRF_UserLogonRequirement = "None"
	[Void]$_.Put()
}

You’ll want to make this code a script block or a function so you can easily re-use it. If you use a script block, you can use a powershell runspace or a powershell job to execute it leaving your main thread available to update the gui of your form. For details on powershell runspaces, see here. For details on powershell jobs, see here.

-Matthew

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s