CTERA Code Snipplets

Here are some code fragments that might help when dealing with CTERA REST responses. For a complete PowerShell module check this post Module: biz.dfch.PS.Storebox.Api.

1. Selecting an Attribute value based on its ‘id’:

function ConvertFrom-CteraRestObjAtt {
[CmdletBinding(
	HelpURI='http://dfch.biz/PS/Storebox/Utilities/ConvertFrom-CteraRestObjAtt'
)]
[OutputType([string])]
Param (
    [Parameter(Mandatory = $true, Position = 0)]
    [alias("o")]
    $InputObject
    ,
    [Parameter(Mandatory = $true, Position = 1)]
    [alias("id")]
    [string] $idAttribute
    ,
    [ValidateSet('Value', 'List')]
    [Parameter(Mandatory = $false, Position = 2)]
    [alias("t")]
    [string] $Type = 'Value'
)
BEGIN {
	$datBegin = [datetime]::Now;
	[string] $fn = $MyInvocation.MyCommand.Name;
} # BEGIN
PROCESS {
	if([string]::IsNullOrEmpty($idAttribute) -or [string]::IsNullOrWhiteSpace($idAttribute)) {
        $e = New-CustomErrorRecord -m "Invalid argument 'idAttribute' specified. Object is empty." -cat InvalidArgument -o $idAttribute;
		Log-Debug $fn $e.Exception.Message;
		$OutputParameter = $null;
        $PSCmdlet.ThrowTerminatingError($e);
	} # if
	if($InputObject -is [string]) {
		[xml] $xml = $InputObject;
	} elseif($InputObject -is [System.Xml.XmlElement]) {
		$xml = $InputObject;
	} else {
        $e = New-CustomErrorRecord -m ("Invalid argument 'InputObject' specified. Object has invalid type: '{0}'." -f $InputObject.GetType()) -cat InvalidType -o $InputObject;
		Log-Debug $fn $e.Exception.Message;
		$OutputParameter = $null;
        $PSCmdlet.ThrowTerminatingError($e);
	} # if
	$fReturn = $false;
	$OutputParameter = $null;
	foreach($a in $xml.att) {
		if($a.id -eq $idAttribute) {
			if($Type -eq 'List') {
				$OutputParameter = $a.list.OuterXml;
			} else {
				$OutputParameter = '{0}' -f $a.val;
			} # if
			$fReturn = $true;
			break;
		} # if
	} # foreach
	if(!$fReturn) { $OutputParameter = $null; }
	return $OutputParameter;
} # PROCESS
END {
} # END

} # ConvertFrom-CteraRestObjAtt
Export-ModuleMember -Function ConvertFrom-CteraRestObjAtt;

2. Converting a list ov ‘val’ into an array

[xml] $l = ConvertFrom-CteraRestObjAtt $r.list.obj[1] users -Type list;
 $l

list
----
list

 $l.list

val
---
{objs/17/portal/PortalUser/usera1, objs/42/portal/PortalUser/usera2}

3. Loading XML template from ‘defaults’ and populate them:

$r = Invoke-CteraCommand -Api 'defaults/ProjectACLRule';
$y = $r.obj.SelectSingleNode("//att[@id = 'name']");
$y.set_InnerXml('<val>tralala</val>');
$y = $r.obj.SelectSingleNode("//att[@id = 'uid']");
$y.set_InnerXml('<val>42</val>');
$y = $r.obj.SelectSingleNode("//att[@id = 'permissions']");
$y.set_InnerXml('<val>ReadOnly</val>');
$y = $r.obj.SelectSingleNode("//att[@id = 'type']");
$y.set_InnerXml('<val>LocalUser</val>');
$r.obj.OuterXml | Format-Xml;

<obj class="ProjectACLRule">
  <att id="expiration" />
  <att id="name">
    <val>EdgarSchnittenfittich</val>
  </att>
  <att id="permissions">
    <val>ReadOnly</val>
  </att>
  <att id="type">
    <val>LocalUser</val>
  </att>
  <att id="uid">
    <val>42</val>
  </att>
</obj>

$list = $r.obj.OuterXml;

$r = Invoke-CteraCommand -Api 'defaults/ProjectCreateParams';
$y = $r.obj.SelectSingleNode("//att[@id = 'acl']");
$y.set_InnerXML( ("<list>{0}</list>" -f $list) );

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

%d bloggers like this: