PowerShell Plugin

From Now Micro
Jump to: navigation, search

This Wiki is out of date. Please view the current documentation at https://wiki.recastsoftware.com

PowerShell Plugin

Allows administrators to share custom PowerShell scripts.

Directory Structure

The default server installation folder is under C:\Program Files (x86)\Now Micro\Recast RCT Server\Server\.

<ServerInstallFolder>\App_Data\PowerShell - The root location for all PowerShell scripts

<ServerInstallFolder>\App_Data\PowerShell\ComputerActions - Location to add scripts to make actions against computers available.

<ServerInstallFolder>\App_Data\PowerShell\UserActions - Location to add scripts to make actions against users available.

<ServerInstallFolder>\App_Data\PowerShell\Scripts - Scripts in this folder will be loaded before any ComputerAction / UserAction scripts are run. Place custom cmdlets here, or other scripts that will be called by the ComputerAction / UserAction scripts.


Script Format

Any PowerShell function in the ComputerActions / UserActions folder that takes a single string as a parameter will be added to the list of actions for a single computer / user. Any function in those folders that takes an array of strings as a parameter will be added to the list of actions for a list of computers / users. If the function has a Synopsis field, this field will be used to display the action on the phone. If the function is missing the Synopsis field, the PowerShell plugin will display the function name to the phone. If the function has a Description field, this will be displayed on the phone as subtext. The return value of the function should be either a single PowerShell command string or an array of PowerShell command strings to be executed in parallel. The output of running the PowerShell command will be returned to the mobile clients and displayed below the computer / user name that the action is being run on. Below is an example script for pinging a single computer:

Function PingComputerAction
{
	<#
	.SYNOPSIS
	PowerShell Ping Computer
	.DESCRIPTION
	Send a ping packet to the computer
	#>
	Param([string]$ComputerName)

	$command = "PingComputer -ComputerName $ComputerName"
	$command
}

To allow the command to be available to multiple computers, we need to change the parameter to be an array of strings, and the return value needs to be an array of PowerShell commands:

Function PingComputersAction
{
<#
	.SYNOPSIS
	PowerShell Ping Computers
	.DESCRIPTION
	Send a ping packet to the computer
	#>
	Param([string[]]$ComputerNames)
	
	$returnValue = @()
	foreach($computerName in $computernames){
		$returnValue += "PingComputer -ComputerName $computerName"
	}
	
	$returnValue
}

To display a confirmation prompt to the user, the function name needs to start with "dangerous":

Function DangerousPingComputersAction
{
<#
	.SYNOPSIS
	PowerShell Ping Computers
	.DESCRIPTION
	Send a ping packet to the computer
	#>
	Param([string[]]$ComputerNames)
	
	$returnValue = @()
	foreach($computerName in $computernames){
		$returnValue += "PingComputer -ComputerName $computerName"
	}
	
	$returnValue
}

For each of these functions, we are calling the PingComputer function stored in the Scripts folder. Recall that the subtext is updated on the phone according to the command returned by the action functions. In the case of ping, it displays the text "Success" if it succeeds and the error message returned by Test-Connection if it fails:

Function PingComputer
{
	Param
	(
		$ComputerName
	)
	
	$error.Clear()
	try{
		$results = Test-Connection -computername $ComputerName -count 1
		if($error){
			$retVal = $error.Exception.InnerException.Message.ToString()
		}
		else{
			if($results.ResponseTime -ne $null){
				$retVal = "Success"
			}
			else{
				$retVal = "Failed to ping $ComputerName"
			}
		}
	}
	catch {
		$retVal = $error.Exception.InnerException.ToString()
	}
	$retVal
}

The following screenshot shows the result of running the PowerShell Ping Computers action on an OU.

PowerShellPingScreenshot.png

Personal tools
Namespaces

Variants
Actions
Navigation
Tools