本帖最后由 flashercs 于 2019-8-4 20:36 编辑
- @echo off
- Powershell -Command "$flag = $false; $sb = $ExecutionContext.InvokeCommand.NewScriptBlock((Get-Content -LiteralPath \"%~0\" | ForEach-Object { if ($flag) { $_ }else { if ($_ -match '#+Powershell#+') { $Script:flag = $true } } }) -join \"`r`n\"); & $sb "
- pause
- exit /b
- ########Powershell########
- # XML文件路径
- $xmlpath = "E:\test\new folder\zuma\1.xml"
- # 计算机名前缀
- $prefix = "PCZF"
- function MakeDOM {
- param (
-
- )
- $dom = try {
- New-Object -ComObject 'Msxml2.DOMDocument.6.0'
- }
- catch {
- try {
- New-Object -ComObject 'Msxml2.DOMDocument.3.0'
- }
- catch {
- try {
- New-Object -ComObject 'Msxml2.DOMDocument'
- }
- catch {
- throw
- }
- }
- }
- $dom.async = $false
- $dom.validateOnParse = $false
- $dom.resolveExternals = $false
- $dom.setProperty('AllowDocumentFunction', $false)
- $dom.setProperty('AllowXsltScript', $false)
- $dom.setProperty('MaxElementDepth', 256)
- $dom.setProperty('ProhibitDTD', $true)
- $dom.setProperty('SelectionLanguage', 'XPath')
- return $dom
- }
-
- function parseError {
- param(
- $dom
- )
- $myErr = $dom.parseError
- if ($myErr.errorCode -ne 0) {
- Write-Host -InputObject "XML parse error" -ForegroundColor Red
- Out-Host -InputObject @{
- 'reason' = myErr.reason;
- 'code' = myErr.errorCode.toUIString(16);
- 'filepos' = myErr.filepos;
- 'line' = myErr.line;
- 'linepos' = myErr.linepos;
- 'srcText' = myErr.srcText;
- 'url' = myErr.url
- }
- return $true
- }
- return $false
- }
- $xmldoc = MakeDOM
- try {
- $xmldoc.load($xmlpath) | Out-Null
- if (parseError $xmldoc) {
- throw
- }
- do {
- $dic = $host.UI.Prompt('Modify XML', '请输入替换前和替换后的字符串:', @('From', 'To'))
- try {
- $xmldoc.SelectSingleNode("//ComputerName[text()=`"$prefix$($dic.From)`"]/text()").nodeValue = $prefix + $dic.To
- Write-Host "Success" -ForegroundColor Green
- }
- catch {
- Write-Host "Failed" -ForegroundColor Green
- }
- $choice = $host.UI.PromptForChoice('继续修改', '是否继续?', @([System.Management.Automation.Host.ChoiceDescription]::new('Y', 'Yes'), [System.Management.Automation.Host.ChoiceDescription]::new('N', 'No')), 0)
- } until ($choice)
-
- $xmldoc.save($xmlpath)
- }
- catch {
-
- }
- finally {
- [System.Runtime.InteropServices.Marshal]::ReleaseComObject($xmldoc) | Out-Null
- Remove-Variable -Name xmldoc
- [gc]::collect()
- [gc]::WaitForPendingFinalizers()
- }
复制代码
|