Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfch/301d330d961b93ffc37e to your computer and use it in GitHub Desktop.
Save dfch/301d330d961b93ffc37e to your computer and use it in GitHub Desktop.
Create vCAC ManagementModelEntites objects from ODATA REST call with PowerShell
# http://d-fens.ch/2014/01/27/create-vcac-modelmanagemententites-objects-from-odata-rest-call-with-powershell/
function Get-VcacEntity {
[CmdletBinding(
SupportsShouldProcess = $false
,
ConfirmImpact = 'Low'
,
DefaultParameterSetName = 'guid'
,
HelpURI='http://dfch.biz/PS/Vcac/Utilities/Get-VcacEntity/'
)]
PARAM (
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'guid')]
[guid] $Guid
,
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'id')]
[int] $id
,
[Parameter(Mandatory = $false, ParameterSetName = 'filter')]
[string] $FilterProperty
,
[Parameter(Mandatory = $true, ParameterSetName = 'filter')]
[Alias('Name')]
[string] $FilterValue
,
[ValidateSet('eq', 'ne', 'gt', 'ge', 'lt', 'le')]
[Parameter(Mandatory = $false, ParameterSetName = 'filter')]
[string] $FilterOperand = 'eq'
,
[Parameter(Mandatory = $false)]
[String] $Type = 'DynamicOps.ManagementModel.VirtualMachine'
,
[Parameter(Mandatory = $false)]
[Switch] $ReturnExistingEntity = $true
,
[Parameter(Mandatory = $false, ParameterSetName = 'list')]
[Switch] $ListAvailable = $true
,
[Parameter(Mandatory = $false, ParameterSetName = 'ListEntityType')]
[String] $ListEntityType = 'DynamicOps.ManagementModel.VirtualMachine'
,
[DynamicOps.ManagementModel.ManagementModelEntities] $m = $biz_dfch_PS_vCAC_Utilities.mgmtContext
)
BEGIN {
Set-Variable gotoSuccess -Option 'Constant' -Value 'biz.dfch.System.Exception.gotoSuccess' -Confirm:$false -WhatIf:$false;
Set-Variable gotoError -Option 'Constant' -Value 'biz.dfch.System.Exception.gotoError' -Confirm:$false -WhatIf:$false;
Set-Variable gotoFailure -Option 'Constant' -Value 'biz.dfch.System.Exception.gotoFailure' -Confirm:$false -WhatIf:$false;
Set-Variable gotoNotFound -Option 'Constant' -Value 'biz.dfch.System.Exception.gotoNotFound' -Confirm:$false -WhatIf:$false;
$datBegin = [datetime]::Now;
[string] $fn = $MyInvocation.MyCommand.Name;
Write-Host ("CALL. MgmtContext '{0}'. Guid '{1}'. id '{2}'. FilterProperty '{3}'. FilterValue '{4}'. Type '{5}'. ParameterSetName: '{6}'." -f ($m -is [Object]), $Guid, $id, $FilterProperty, $FilterValue, $Type, $PsCmdlet.ParameterSetName) -fac 1;
} # BEGIN
PROCESS {
# Default test variable for checking function response codes.
[Boolean] $fReturn = $false;
# Return values are always and only returned via OutputParameter.
$OutputParameter = $null;
try {
if($PSCmdlet.ParameterSetName -eq 'list') {
$aDefinition = ($m | gm -Type Properties).Definition;
foreach($Definition in $aDefinition) {
$fReturn = $Definition -match 'DynamicOps\.Repository\.RepositoryServiceQuery\[(?<Type>[^\]]+)\]';
if(!$fReturn) { continue; }
$OutputParameter += ("{0}`n" -f $Matches.Type);
$OutputParameter = $OutputParameter | Sort;
} # foreach
throw($gotoSuccess);
} elseif($PSCmdlet.ParameterSetName -eq 'ListEntityType') {
$entity = New-Object $ListEntityType;
$fReturn = $true;
$OutputParameter = $entity;
throw($gotoSuccess);
} # if
# Create new entity
$NewVcacEntity = New-Object $Type;
if(!$NewVcacEntity) { Write-Error ("Invalid object type: '{0}'." -f $Type) }
# Define default property name if not specified
if(!$FilterProperty) { $FilterProperty = '{0}Name' -f $NewVcacEntity.GetType().Name; }
# Construct Uri
$ServerUri = $biz_dfch_PS_vCAC_Utilities.ServerUri;
switch($PSCmdlet.ParameterSetName) {
'id' { [Uri] $Uri = "{0}/{1}({2})" -f $ServerUri, $m.GetEntitySetFromType($Type), $id; }
'guid' { [Uri] $Uri = "{0}/{1}(guid'{2}')" -f $ServerUri, $m.GetEntitySetFromType($Type), $Guid; }
'filter' { [Uri] $Uri = "{0}/{1}?`$filter={2} {3} '{4}'" -f $ServerUri, $m.GetEntitySetFromType($Type), $FilterProperty, $FilterOperand, $FilterValue; }
default { Write-Error 'Invalid ParameterSetName.' }
} # switch
Write-Host ("Invoking Uri '{0}' ..." -f $Uri);
if($m.Credentials.UserName) {
$r = Invoke-RestMethod $Uri -Credential $m.Credentials;
} else {
$r = Invoke-RestMethod $Uri -UseDefaultCredentials;
} # if
# Convert from XmlDocument or XmlLinkedNode
if($r.entry) { $x = $r.entry; } else { $x = $r; }
foreach($Entity in $m.Entities) {
if($x.id -ne $Entity.Identity) { continue; }
if($ReturnExistingEntity) {
Write-Host ("Entity '{0}' [{1}] found in entity tracker. Returning existing entity ..." -f $Entity.Identity, $NewVcacEntity.GetType().Name);
$NewVcacEntity = $Entity.Entity;
$m.AttachIfNeeded($m.GetEntitySetFromType($NewVcacEntity.GetType()), $NewVcacEntity);
$OutputParameter = $NewVcacEntity;
throw($gotoSuccess);
} else {
Write-Host ("Entity '{0}' [{1}] found in entity tracker. Removing existing entity ..." -f $Entity.Identity, $NewVcacEntity.GetType().Name);
$fReturn = $m.Detach($Entity.Entity);
break;
} # if
} # foreach
# Convert returned EF xml entities to .NET data types
foreach($p in $x.content.properties.GetEnumerator()) {
if($p.localname -eq '#whitespace') { continue; }
switch($p.type) {
'Edm.Int32' { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
'Edm.Int64' { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
'Edm.Decimal' { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
'Edm.Boolean' { if($p.null -ne 'true') { if($p.'#text' -eq 'true') { $NewVcacEntity.($p.localname) = $true; } else { $NewVcacEntity.($p.localname) = $false; } } }
'Edm.Guid' { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
'Edm.DateTime' { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
'Edm.Byte' { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
default { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
} # switch
} # foreach
# Attach object to entity tracker
$m.AttachIfNeeded($m.GetEntitySetFromType($NewVcacEntity.GetType()), $NewVcacEntity);
# Optional: if you had used an expand expression you could also load the returned links to that entity
# foreach($l in $r.entry.link.GetEnumerator()) {
# if(!$l.inline) { continue; }
# $null = $m.LoadProperty($NewVcacEntity, $l.title);
# } # foreach
$fReturn = $true;
$OutputParameter = $NewVcacEntity
} # try
catch {
# ... do sth useful
} # catch
finally {
# Clean up
} # finally
} # PROCESS
END {
$datEnd = [datetime]::Now;
Write-Host ("RET. fReturn: [{0}]. Execution time: [{1}]ms. Started: [{2}]." -f $fReturn, ($datEnd - $datBegin).TotalMilliseconds, $datBegin.ToString('yyyy-MM-dd HH:mm:ss.fffzzz')) -fac 2;
return $OutputParameter;
} # END
} # function
Export-ModuleMember -Function Get-VcacEntity;
<#
Copyright 2014-2015 d-fens GmbH
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#>
d-fens Create vCAC ManagementModelEntites objects from ODATA REST call with PowerShell
Copyright 2014 d-fens GmbH
This product includes software developed at
d-fens GmbH (http://d-fens.ch/)
d-fens GmbH
General-Guisan-Strasse 6
CH-6300 Zug
Switzerland
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment