0

VMWare의 vcloud REST API를 사용하여 NIC (VMWare 자체의 powerCLI가 허용하지 않는 것)를 추가하는 powershell cmdlet을 작성하려고합니다. 그렇게하기 위해서는,이 XML을해야합니다Powershell : 네임 스페이스로 XML 문서에 요소 추가

<?xml version="1.0" encoding="UTF-8"?><vcloud:RasdItemsList 
    xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" 
    xmlns:vcloud="http://www.vmware.com/vcloud/v1.5" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    href="https://vcloud.example.com/api/vApp/vm-89c84bd6-c6f2-4e4c-8a7d-c44a3489e2e4/virtualHardwareSection/networkCards" 
    type="application/vnd.vmware.vcloud.rasdItemsList+xml"> 
    <vcloud:Link 
     href="https://vcloud.example.com/api/vApp/vm-89c84bd6-c6f2-4e4c-8a7d-c44a3489e2e4/virtualHardwareSection/networkCards" 
     rel="edit" 
     type="application/vnd.vmware.vcloud.rasdItemsList+xml"/> 
</vcloud:RasdItemsList> 

을 그리고 마지막 vcloud 아래에 다음과 같은 요소에 추가 링크. 이제

<ovf:Item> 
     <rasd:Address></rasd:Address> 
     <rasd:AddressOnParent>0</rasd:AddressOnParent> 
     <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation> 
     <rasd:Connection 
      vcloud:ipAddressingMode="none" 
      vcloud:primaryNetworkConnection="true">VM Network</rasd:Connection> 
     <rasd:Description>E1000 ethernet adapter on "VM Network"</rasd:Description> 
     <rasd:ElementName>Network adapter 0</rasd:ElementName> 
     <rasd:InstanceID>1</rasd:InstanceID> 
     <rasd:ResourceSubType>E1000</rasd:ResourceSubType> 
     <rasd:ResourceType>10</rasd:ResourceType> 
    </ovf:Item> 

, 나는 $에 해당 xmldoc으로 PS에 원래의 XML있어, 나는 코드 아래 시도 : 여기

$xmlElt = $xmlDoc.CreateElement("ovf:Item") 
$xmlSubElt = $xmlDoc.CreateElement("rasd:Address") 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:AddressOnParent") 
$xmlSubText = $xmlDoc.CreateTextNode("0") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:AutomaticAllocation") 
$xmlSubText = $xmlDoc.CreateTextNode("false") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:Connection") 
$xmlSubText = $xmlDoc.CreateTextNode("none") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlAtt = $xmlDoc.CreateAttribute("vcloud:primaryNetworkConnection") 
$xmlAtt.Value = "True" 
$xmlSubElt.Attributes.Append($xmlAtt) 
$xmlAtt = $xmlDoc.CreateAttribute("vcloud:ipAddressingMode") 
$xmlAtt.Value = "NONE" 
$xmlSubElt.Attributes.Append($xmlAtt) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:Description") 
$xmlSubText = $xmlDoc.CreateTextNode("Network Adapter 0") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:ElementName") 
$xmlSubText = $xmlDoc.CreateTextNode("Network Adapter 0") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:InstanceID") 
$xmlSubText = $xmlDoc.CreateTextNode("0") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:ResourceSubType") 
$xmlSubText = $xmlDoc.CreateTextNode("E1000") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:ResourceType") 
$xmlSubText = $xmlDoc.CreateTextNode("10") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlDoc.LastChild.AppendChild($xmlElt); 

이 (가) 작성하는 XML 관련 비트입니다 비록 :

<Item> 
    <Address /> 
    <AddressOnParent>0</AddressOnParent> 
    <AutomaticAllocation>false</AutomaticAllocation> 
    <Connection primaryNetworkConnection="True" ipAddressingMode="NONE">none</Connection> 
    <Description>Network Adapter 0</Description> 
    <ElementName>Network Adapter 0</ElementName> 
    <InstanceID>0</InstanceID> 
    <ResourceSubType>E1000</ResourceSubType> 
    <ResourceType>10</ResourceType> 
</Item> 

당신이 볼 수 있듯이, 그것은 대부분은 작동하지만, 불행히도 XML 이름 공간 접두사를 제거하는 것 같습니다. 여전히 작동 할 수도 있지만 형식과 일치 시키려합니다. 나는 XML과 Powershell 인터랙션에 익숙하지 않기 때문에 나의 질문은 다음과 같다 :

  1. 어떻게 네임 스페이스를 출력하게 하는가?
  2. 내가 이렇게 바보처럼 내려다보고있는 것처럼 각 요소를 수동으로 만드는 것이 더 쉬운 방법이 있습니까?
+0

[TechNet의 상기 네임 스페이스의 세번째 파라미터를 사용하여 설정한다 (http://msdn.microsoft.com/en-us/library/c22k3d47 (V = vs.110) .ASPX)

+0

작동하지 않습니다. 해당 Cmdlet을 사용하려면 vcloud 디렉터를 통하지 않고 vcenter를 통해 액세스하고 변경해야합니다. 이 스크립트의 목적은 vCenter에 전혀 액세스하지 않고 이러한 변경을 수행하는 것입니다. –

답변

2

해결되었습니다. "rasdItemsList"xpath에서 System.Xml.XmlNamespaceManager로 네임 스페이스를로드 한 다음 요소를 만들 때 각 URI를 조회해야했습니다. 항

$nsm = New-Object System.Xml.XmlNamespaceManager($xmldoc.nametable) 
$nsm.addnamespace("xsi", $xmldoc.rasdItemsList.GetNamespaceOfPrefix("xsi")) 
$nsm.addnamespace("rasd", $xmldoc.rasdItemsList.GetNamespaceOfPrefix("rasd")) 
$nsm.addnamespace("vcloud", $xmldoc.rasdItemsList.GetNamespaceOfPrefix("vcloud")) 

$xmlElt = $xmlDoc.CreateElement("vcloud:Item", $nsm.LookupNamespace("vcloud")) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:Address", $nsm.LookupNamespace("rasd")) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:AddressOnParent", $nsm.LookupNamespace("rasd")) 
$xmlSubText = $xmlDoc.CreateTextNode("0") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:AutomaticAllocation", $nsm.LookupNamespace("rasd")) 
$xmlSubText = $xmlDoc.CreateTextNode("false") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:Connection", $nsm.LookupNamespace("rasd")) 
$xmlSubText = $xmlDoc.CreateTextNode("none") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlAtt = $xmlDoc.CreateAttribute("vcloud:primaryNetworkConnection", $nsm.LookupNamespace("vcloud")) 
$xmlAtt.Value = "True" 
$xmlSubElt.Attributes.Append($xmlAtt) 
$xmlAtt = $xmlDoc.CreateAttribute("vcloud:ipAddressingMode", $nsm.LookupNamespace("vcloud")) 
$xmlAtt.Value = "NONE" 
$xmlSubElt.Attributes.Append($xmlAtt) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:Description", $nsm.LookupNamespace("rasd")) 
$xmlSubText = $xmlDoc.CreateTextNode("Network Adapter 0") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:ElementName", $nsm.LookupNamespace("rasd")) 
$xmlSubText = $xmlDoc.CreateTextNode("Network Adapter 0") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:InstanceID", $nsm.LookupNamespace("rasd")) 
$xmlSubText = $xmlDoc.CreateTextNode("0") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:ResourceSubType", $nsm.LookupNamespace("rasd")) 
$xmlSubText = $xmlDoc.CreateTextNode("E1000") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlSubElt = $xmlDoc.CreateElement("rasd:ResourceType", $nsm.LookupNamespace("rasd")) 
$xmlSubText = $xmlDoc.CreateTextNode("10") 
$xmlSubElt.AppendChild($xmlSubText) 
$xmlElt.AppendChild($xmlSubElt) 
$xmlDoc.LastChild.AppendChild($xmlElt); 
0

왜 PowerCLI를 사용하지 않습니까?

New-NetworkAdapter -VM $vm -NetworkName "VM Network" -MacAddress 'aa:bb:cc:dd:ee:ff' -WakeOnLan -StartConnected 
+0

왜냐하면, 다음과 같은 이유 때문입니다 : createElement 메소드 (예 :'$ xmlElt = $ xmlDoc.CreateElement ('', 'Item', "rasd")') –