[英]Find all computers with specific text file
Trying to get this powershell script to check for a specific entry in a file on all PC's in my domain and the ones with the designated OLD server name write out to a file then run a replace on only the computers with the found value. 尝试获取此Powershell脚本以检查我域中所有PC上文件中的特定条目,并将具有指定OLD服务器名称的PC写入文件,然后仅在具有找到值的计算机上运行替换。 I can get it by doing this to every PC as i know that will only apply to the ones with matching data however i have to run a stop service then a start service on each PC where i make the change and i don't want to stop/start the service on every PC in the domain.
我可以通过在每台PC上执行此操作来获取它,因为我知道这仅适用于具有匹配数据的PC,但是我必须先运行Stop服务,然后在每台PC上进行启动服务,然后再进行更改,而我不想在域中的每台PC上停止/启动服务。 I've gotten as far as getting all PC's outputted to a file but not sure how to combine that into the IF Statement.
我已经将所有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
You could check if the file on the computer has any lines that match the given word first. 您可以先检查计算机上的文件是否有任何行与给定单词匹配。 Then only process the file if a line is found ie something like this could be run on all computers:
然后仅在找到一行后才处理该文件,即可以在所有计算机上运行这样的文件:
# 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.