To iterate over a list of machine names or IP addresses from a text file and execute the provided PowerShell command on each, I crafted a loop structure within the PowerShell script. Assuming each line in your text file represents a different machine name or IP address, here’s how you can structure the loop:
# Path to the text file containing machine names or IP addresses $textFilePath = "C:\path\to\your\file.txt" # Read each line in the text file, where each line contains a machine name or IP address $machines = Get-Content -Path $textFilePath foreach ($machine in $machines) { try { # Attempt to set the RemoteRegistry service startup type to Automatic and start the service on each machine Get-Service -ComputerName $machine -Name RemoteRegistry | Set-Service -StartupType Automatic -PassThru | Start-Service Write-Host "Service updated successfully on machine: $machine" } catch { Write-Host "An error occurred on machine: $machine. Error: $_" } }
In this script:
$textFilePath
should be replaced with the actual path to your text file.Get-Content
cmdlet reads the list of machine names or IP addresses from the file.- A
foreach
loop iterates over each machine name or IP address. - Within the loop, the
Get-Service
cmdlet fetches theRemoteRegistry
service,Set-Service
changes its startup type to Automatic, andStart-Service
initiates the service. - Basic error handling is implemented with a
try-catch
block to gracefully manage any issues that occur for individual machines, such as connectivity problems or access permissions, without halting the entire script.
VÕ TÌNH THƯƠNG