尝试获取此Powershell脚本以检查我域中所有PC上文件中的特定条目,并将具有指定OLD服务器名称的PC写入文件,然后仅在具有找到值的计算机上运行替换。我可以通过在每台PC上执行此操作来获取它,因为我知道这仅适用于具有匹配数据的PC,但是我必须先运行Stop服务,然后在每台PC上进行启动服务,然后再进行更改,而我不想这样做。在域中的每台PC上停止/启动服务。我已经将所有PC的输出都输出到了文件中,但是不确定如何将其组合到IF语句中。
$path = "C:\myfile.txt"
$find = "OldServerName"
$replace = "NewServerName"
$adcomputers = "C:\computers.txt"
$changes = "C:\changes.txt"
Get-ADComputer -Filter * | Select -Expand Name | Out-File -FilePath .\computers.txt
#For only computers that need the change
Stop-Service -name myservice
(get-content $path) | foreach-object {$_ -replace $find , $replace} | out-file $path
Start-Service -name myservice
您可以先检查计算机上的文件是否有任何行与给定单词匹配。然后仅在找到一行的情况下处理该文件,即可以在所有计算机上运行这样的文件:
# Check if the computer needs the change - Find any line with the $find word
$LinesMatched = $null
$LinesMatched = Get-Content $path | Where { $_ -match $find }
# If there is one or more lines in the file that needs to be changed
If($LinesMatched -ne $null) {
# Stop service and replace words in file.
Stop-Service -name myservice
(Get-Content $path) -replace $find , $replace | Out-File $path
Start-Service -name myservice
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句