简体   繁体   English

查找所有带有特定文本文件的计算机

[英]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.

相关问题 查找不是特定GG组成员的计算机 - Find computers that are not members of a specific GG group 在本地管理员组中查找具有特定用户的计算机 - Find computers with a specific user in local admin group Powershell - 查找登录了特定用户名的计算机 - Powershell - Find computers with a specific username logged in 如何查找用户登录的所有计算机 - How to find all computers a user is logged into 如何使用 PowerShell 在文件中查找特定文本? - How to find specific text in a file with PowerShell? get-wmiobject并行处理以在所有域计算机上查找设备 - get-wmiobject parallel processing to find device on all domain computers Powershell:查找AD中的所有计算机,但排除某些OU - Powershell: Find all computers in AD but exclude certain OU's Powershell:在文件中查找一组文本,然后提取该组文本中的特定行 - Powershell : Find a group of text in a file then extract a specific line in that group of text Powershell脚本,用于将AD计算机与文本文件进行比较,并更改这些计算机上的注册表服务,然后编写脱机的计算机 - Powershell script to compare AD computers to text file and change registry service on those computer and then write the computers that were offline 使用ForEach在计算机列表中搜索以在文件中查找字符串-Powershell - Search through a list of computers to find a string in a file with ForEach- Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM