2011-03-09 30 views
1

這是我的XML文件:我寧願讀XML,然後修改計算機名說newcompname並把它寫回文件需要幫助的更新(窗戶的unattend.xml)使用PowerShell XML文件

<?xml version="1.0" encoding="utf-8"?> 
<unattend xmlns="urn:schemas-microsoft-com:unattend"> 
<servicing> 
</servicing> 
<settings pass="specialize"> 
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" 
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<ComputerName>testserver007</ComputerName> 
<RegisteredOrganization>MyOrganization</RegisteredOrganization> 
<RegisteredOwner>Admin</RegisteredOwner> 
<ShowWindowsLive>false</ShowWindowsLive> 
</component> 
<component name="Microsoft-Windows-TerminalServices-LocalSessionManager" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<fDenyTSConnections>false</fDenyTSConnections> 
</component> 
<component name="Microsoft-Windows-TerminalServices-RDP-WinStationExtensions" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<UserAuthentication>1</UserAuthentication> 
</component> 
<component name="Microsoft-Windows-TCPIP" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" 
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<Interfaces> 
<Interface wcm:action="add"> 
<Ipv4Settings> 
<DhcpEnabled>false</DhcpEnabled> 
<RouterDiscoveryEnabled>false</RouterDiscoveryEnabled> 
<Metric>10</Metric> 
</Ipv4Settings> 
<Identifier>Local Area Connection</Identifier> 
<Routes> 
<Route wcm:action="add"> 
<Identifier>1</Identifier> 
<Metric>10</Metric> 
<NextHopAddress>192.168.2.247</NextHopAddress> 
<Prefix>0.0.0.0/0</Prefix> 
</Route> 
</Routes> 
<UnicastIpAddresses> 
<IpAddress wcm:action="add" wcm:keyValue="1">192.168.2.101/24</IpAddress> 
</UnicastIpAddresses> 
</Interface> 
</Interfaces> 
</component> 
<component name="Microsoft-Windows-DNS-Client" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" 
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<Interfaces> 
<Interface wcm:action="add"> 
<DNSServerSearchOrder> 
<IpAddress wcm:action="add" wcm:keyValue="1">192.168.2.75</IpAddress> 
<IpAddress wcm:action="add" wcm:keyValue="2">192.168.2.76</IpAddress> 
</DNSServerSearchOrder> 
<Identifier>Local Area Connection</Identifier> 
<EnableAdapterDomainNameRegistration>true</EnableAdapterDomainNameRegistration> 
<DisableDynamicUpdate>false</DisableDynamicUpdate> 
</Interface> 
</Interfaces> 
</component> 
<component name="Microsoft-Windows-OutOfBoxExperience" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" 
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<DoNotOpenInitialConfigurationTasksAtLogon>true</DoNotOpenInitialConfigurationTasksAtLogon> 
</component> 
<component name="Microsoft-Windows-ServerManager-SvrMgrNc" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" 
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<DoNotOpenServerManagerAtLogon>true</DoNotOpenServerManagerAtLogon> 
</component> 
</settings> 
<settings pass="oobeSystem"> 
<component name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" 
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<InputLocale>0809:00000809</InputLocale> 
<SystemLocale>en-GB</SystemLocale> 
<UILanguage>en-US</UILanguage> 
<UserLocale>en-GB</UserLocale> 
</component> 
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" 
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<AutoLogon> 
<Password> 
<Value>RgBvAG4ANgBIADAAbQBlAFAAYQBzAHMAdwBvAHIAZAA=</Value> 
<PlainText>false</PlainText> 
</Password> 
<Domain>testserver007</Domain> 
<Enabled>true</Enabled> 
<LogonCount>5</LogonCount> 
<Username>Administrator</Username> 
</AutoLogon> 
<FirstLogonCommands> 
<SynchronousCommand> 
<Order>1</Order> 
<!-- Post Build Script 1 --> 
<CommandLine>cmd.exe /C C:\BuildScripts\firsttest.cmd</CommandLine> 
</SynchronousCommand> 
</FirstLogonCommands> 
<Display> 
<ColorDepth>32</ColorDepth> 
<HorizontalResolution>1024</HorizontalResolution> 
<VerticalResolution>768</VerticalResolution> 
</Display> 
<OOBE> 
<HideEULAPage>true</HideEULAPage> 
<ProtectYourPC>3</ProtectYourPC> 
<NetworkLocation>Other</NetworkLocation> 
</OOBE> 
<UserAccounts> 
<AdministratorPassword> 
<Value>RgBvAG4ANgBIADAAbQBlAEEAZABtAGkAbgBpAHMAdAByAGEAdABvAHIAUABhAHMAcwB3AG8AcgBkAA==</Value> 
<PlainText>false</PlainText> 
</AdministratorPassword> 
</UserAccounts> 
<RegisteredOrganization>MyOrganization</RegisteredOrganization> 
<RegisteredOwner>Admin</RegisteredOwner> 
<TimeZone>GMT Standard Time</TimeZone> 
<LogonCommands> 
<AsynchronousCommand wcm:action="add"> 
<CommandLine>cmd.exe /C C:\Scripts\test.cmd</CommandLine> 
<Description>test command</Description> 
<Order>2</Order> 
<RequiresUserInput>true</RequiresUserInput> 
</AsynchronousCommand> 
</LogonCommands> 
</component> 
</settings> 
<cpi:offlineImage cpi:source="catalog:d:/win2008r2instfiles/sources/install_windows server 2008 r2 serverstandard.clg" xmlns:cpi="urn:schemas-microsoft-com:cpi" /> 
</unattend> 
+1

您可以編輯XML(把它放在一個代碼塊),最簡單的方法。尖括號被剝離。 – 2011-03-09 17:28:07

+0

對不起,mods不能做你所問的。如果您仍然有問題,請在[meta](http://meta.stackoverflow.com)提問 – Will 2011-03-10 14:37:37

回答

2

您可以使用各種技術,像以下示例一樣迭代節點,使用XPATH查詢調用select-xml以查找節點,然後更改值或$ xml.selectNodes(...)。通常我會去的XPath的路線,但包括名稱空間,它是簡單的XML這是證明它在PowerShell中

$xml = [xml]@" 
    <?xml version="1.0" encoding="utf-8"?> 
    <unattend xmlns="urn:schemas-microsoft-com:unattend"># 
     <servicing></servicing> 
     <settings pass="specialize"> 
      <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
       <ComputerName>testserver007</ComputerName> 
       <RegisteredOrganization>MyOrg</RegisteredOrganization> 
       <RegisteredOwner>Admin</RegisteredOwner> 
       <ShowWindowsLive>false</ShowWindowsLive> 
      </component> 
    </settings> 
    </unattend> 
"@ 

#presuming multiple settings tags, with multiplecomponents 
$xml.unattend | foreach { $_.settings } | foreach { $_.component.computername = "newname" } 
$xml.save("c:\temp\test.xml") 
+0

您好,感謝您的回覆,但我無法使用您的解決方案。當我運行上面的PS文件時,出現以下錯誤: - **開始的字符串:在D:\ Dumps \ PSScripts \ xmlupdate2.ps1:1 char:13 + $ xml = [xml] <<<< @「錯過終結者:「@。在D:\ Dumps \ PSScripts \ xmlupdate2.ps1:18 char:1 + <<<< + CategoryInfo:ParserError:(<?xml versi ... p \ test.xml'「):String)[],ParseException + FullyQualifiedErrorId:TerminatorExpectedAtEndOfString ** – user652002 2011-03-10 09:41:58

+0

你需要@之前」刪除空格。 – JasonMArcher 2011-03-10 20:17:59

+0

hmm這裏的字符串不能很好地粘貼在stackoverflow上。任何建議,也許使用PRE標籤而不是自動? – klumsy 2011-03-10 23:16:20