这是我的XML文件:我更喜欢读取xml,然后修改计算机名称以说出newcompname并将其写回文件
<?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>
答案 0 :(得分: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")