Invoke-Command
Aller à la navigation
Aller à la recherche
| Fiche express | |
|---|---|
| Domaine | Exécution distante |
| Cmdlet | Invoke-Command
|
| Voir aussi | New-PSSession · Get-wmiobject |
Invoke-Command exécute une commande ou un bloc de script sur une ou plusieurs machines distantes, via WS-Man (PowerShell Remoting).
Commande ponctuelle
$cmd = { Get-Service | Where { $_.Status -eq 'Paused' } }
Invoke-Command -ComputerName serveur, localhost -ScriptBlock $cmd
Invoke-Command -ComputerName serveur -ScriptBlock { $env:PROCESSOR_IDENTIFIER }
Session permanente
$session = New-PSSession -ComputerName 192.168.0.10
Invoke-Command -Session $session -ScriptBlock { $s }
Invoke-Command -Session $session -ScriptBlock { $s.Continue() }
Exécuter un script
Invoke-Command -ComputerName serveur -FilePath C:\temp\get-freespace.ps1
Exemple get-freespace.ps1 — espace disque libre de chaque disque local :
param($computer = '.')
Get-WmiObject -Computer $computer Win32_LogicalDisk |
Where { $_.DriveType -eq 3 } |
Select @{e={$_.SystemName}; n='Système'},
@{e={$_.Name}; n='Disque'},
@{e={[math]::Round($_.FreeSpace/1GB,1)}; n='Disponible (Go)'},
@{e={[math]::Round(([int64]$_.FreeSpace/[int64]$_.Size*100),0)}; n='% restant'}