Remote Synchronization with PowerShell

Remote Synchronization with PowerShell

Sincronización remota con PowerShellRemote synchronization with PowerShell

Ejecución automatizada de un script en varios equipos remotosAutomated script execution across multiple remote computers

PowerShell Remoting
DescripciónOverview

Sincroniza varios equipos desde una única sesiónSynchronize multiple computers from one session

Este script lee una lista de direcciones IP, crea una sesión remota para cada equipo y ejecuta el archivo SynchThis.ps1 en el destino.This script reads a list of IP addresses, creates a remote session for each computer and runs the SynchThis.ps1 file on the target.

PowerShell remote synchronization
Requisito previo importanteImportant prerequisite

Ejecuta Enable-PSRemoting con permisos de administrador en cada equipo remoto antes de utilizar el script.Run Enable-PSRemoting with administrator privileges on every remote computer before using the script.

PowerShell
$Username = 'domain\user' # Domain admin user
$Password = '123456789'
# Create credential object

$SecurePassWord = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $SecurePassWord

foreach ($line in [System.IO.File]::ReadLines("local path\hosts.txt")) # IP addresses list of the remote machines
{
  $line
  Set-Item WSMan:\localhost\Client\TrustedHosts -Value $line -Force

  # Create session object
  $Session = New-PSSession -ComputerName $line -Credential $Cred

  # Invoke command
  $Script_Block = "remote path\SynchThis.ps1" # This file is needed on every remote machine
  $Script = $ExecutionContext.InvokeCommand.NewScriptBlock($Script_Block)
  $Job = Invoke-Command -Session $Session -ScriptBlock $Script

  # Close session
  Remove-PSSession -Session $Session
}
Seguridad:Security: sustituye las credenciales de ejemplo y evita guardar contraseñas reales en texto plano. En producción, utiliza un almacén seguro de credenciales. replace the example credentials and avoid storing real passwords as plain text. Use a secure credential store in production.
ArchivosFiles
Microsoft PowerShell · Administración remotaMicrosoft PowerShell · Remote administration

Comentarios