Created
December 2, 2015 14:56
-
-
Save BenNeise/b1df2065b21dde4b94aa to your computer and use it in GitHub Desktop.
Creates a new vRO naming prefix using the IaaS oData interface via REST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function New-IaaSHostNamePrefix { | |
<# | |
.SYNOPSIS | |
Creates a new vRO naming prefix using the IaaS oData interface via REST | |
.DESCRIPTION | |
Creates a new vRO naming prefix using the IaaS oData interface via REST. Tested on vCO 6.2.1 with PowerShell 4 | |
.PARAMETER Server | |
The name of the IaaS server on which the operation shouldbe performed. | |
.PARAMETER Prefix | |
The Machine Prefix to use | |
.PARAMETER NextMachineNumber | |
The Next Number to use | |
.PARAMETER MachineNumberLength | |
Number of digits to be used | |
.PARAMETER Credential | |
A PowerShell PSCredential object | |
.EXAMPLE | |
Create a prefix called "TST" with a next machine number of 100 and a machine length of 4 on the IaaS server IAAS01. | |
The first machine to use this prefix will be TST0124. | |
New-IaaSHostNamePrefix -Server "IAAS01" -Prefix "TST" -nextMachineNumber 123 -machineNumberLength 4 -Credential $cred | |
.NOTES | |
Ben Neise | |
#> | |
[cmdletbinding()] | |
param ( | |
[parameter(Mandatory = $true, | |
Position = 0 | |
)] | |
[String] | |
$Server, | |
[parameter(Mandatory = $true, | |
Position = 1 | |
)] | |
[String] | |
$Prefix, | |
[parameter(Mandatory = $true, | |
Position = 2 | |
)] | |
[int32] | |
$NextMachineNumber, | |
[parameter(Mandatory = $true, | |
Position = 3 | |
)] | |
[int32] | |
$MachineNumberLength, | |
[parameter(Mandatory = $true, | |
Position = 4 | |
)] | |
[PSCredential] | |
$Credential | |
) | |
begin { | |
$baseUri = "https://" + $Server + "/Repository/data/ManagementModelEntities.svc/" | |
$uri = $baseUri + "HostNamePrefixes" | |
$method = "POST" | |
$xmlContentType = "application/atom+xml" | |
$strDate = Get-Date -Format s | |
$guid = [guid]::NewGuid() | |
} | |
process { | |
$xml = [xml]'<?xml version="1.0" encoding="utf-8"?><entry></entry>' | |
$xmlTitle = $xml.CreateElement("title") | |
$xml.get_ChildNodes().Item(1).AppendChild($xmlTitle) | Out-Null | |
$xml.entry.SetAttribute('xmlns','http://www.w3.org/2005/Atom') | |
$xml.entry.SetAttribute('xmlns:d','http://schemas.microsoft.com/ado/2007/08/dataservices') | |
$xml.entry.SetAttribute('xmlns:m','http://schemas.microsoft.com/ado/2007/08/dataservices/metadata') | |
$xmlNamespaceManager = New-Object System.Xml.XmlNamespaceManager($xml.nametable) | |
$xmlNamespaceManager.addnamespace("d", $xml.entry.GetNamespaceOfPrefix("d")) | |
$xmlNamespaceManager.addnamespace("m", $xml.entry.GetNamespaceOfPrefix("m")) | |
$xmlUpdated = $xml.CreateElement("updated") | |
$xmlUpdatedNode = $xml.entry.AppendChild($xmlUpdated) | |
$xmlText = $xml.CreateTextNode($strDate) | |
$xmlUpdatedNode.AppendChild($xmlText) | Out-Null | |
$xmlAuthor = $xml.CreateElement("author") | |
$xml.entry.AppendChild($xmlAuthor) | Out-Null | |
$xmlName = $xml.CreateElement("name") | |
$xmlAuthor.AppendChild($xmlName) | Out-Null | |
$xmlCategory = $xml.CreateElement("category") | |
$xmlCategory.SetAttribute("scheme", "http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"); | |
$xmlCategory.SetAttribute("term", "DynamicOps.ManagementModel.HostNamePrefix"); | |
$xml.entry.AppendChild($xmlCategory) | Out-Null | |
$xmlContent = $xml.CreateElement("content") | |
$xmlContent.SetAttribute('type','application/xml') | Out-Null | |
$xml.entry.AppendChild($xmlContent) | Out-Null | |
$xmlProperties = $xml.CreateElement('m:properties',$xmlNamespaceManager.LookupNamespace("m")) | |
$xmlContent.AppendChild($xmlProperties) | Out-Null | |
$xmlHostnamePrefixID = $xml.CreateElement('d:HostnamePrefixID',$xmlNamespaceManager.LookupNamespace("d")) | |
$xmlProperties.AppendChild($xmlHostnamePrefixID) | Out-Null | |
$xmlHostnamePrefixID.SetAttribute('type',$xmlNamespaceManager.LookupNamespace("m"),'Edm.Guid') | Out-Null | |
$xmlHostnamePrefix = $xml.CreateTextNode($guid) | |
$xmlHostnamePrefixID.AppendChild($xmlHostnamePrefix) | Out-Null | |
$xmlMachinePrefix = $xml.CreateElement('d:MachinePrefix',$xmlNamespaceManager.LookupNamespace("d")) | |
$xmlProperties.AppendChild($xmlMachinePrefix) | Out-Null | |
$xmlMachinePrefixText = $xml.CreateTextNode($prefix) | |
$xmlMachinePrefix.AppendChild($xmlMachinePrefixText) | Out-Null | |
$xmlNextMachineNo = $xml.CreateElement('d:NextMachineNo',$xmlNamespaceManager.LookupNamespace("d")) | |
$xmlProperties.AppendChild($xmlNextMachineNo) | Out-Null | |
$xmlNextMachineNo.SetAttribute('type',$xmlNamespaceManager.LookupNamespace("m"),'Edm.Int32') | Out-Null | |
$xmlNextMachineNoText = $xml.CreateTextNode($nextMachineNumber) | |
$xmlNextMachineNo.AppendChild($xmlNextMachineNoText) | Out-Null | |
$xmlMachineNumberLength = $xml.CreateElement('d:MachineNumberLength',$xmlNamespaceManager.LookupNamespace("d")) | |
$xmlProperties.AppendChild($xmlMachineNumberLength) | Out-Null | |
$xmlMachineNumberLength.SetAttribute('type',$xmlNamespaceManager.LookupNamespace("m"),'Edm.Int32') | Out-Null | |
$xmlMachineNumberLengthText = $xml.CreateTextNode($MachineNumberLength) | |
$xmlMachineNumberLength.AppendChild($xmlMachineNumberLengthText) | Out-Null | |
$body = $xml.outerXML | |
$result = try { | |
Invoke-WebRequest -Uri $uri -Method $method -ContentType $xmlContentType -Credential $Credential -Body $body | |
} | |
catch { | |
Write-Error -Message ([xml]$_.ErrorDetails.Message).error.innererror.internalException.internalException.message | |
return | |
} | |
} | |
end { | |
return $result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment