Run commands on every node in a cluster with PowerShell

Run commands on every node in a cluster with PowerShell

Q. How can I easily run commands on every node in a cluster?

A. PowerShell makes it very easy to execute commands on remote machines and remote management is enabled by default in Windows Server 2012 and above. To run a PowerShell command on every node in a cluster use the following code (replacing the cluster name and the actual PowerShell command you wish to run which in this example opens the Hyper-V Replica port).

$clusname = "savdalhv16clus"
$servers = Get-ClusterNode -Cluster $clusname
foreach ($server in $servers)
{
    Invoke-Command -ComputerName $server.Name -ScriptBlock `
    {Enable-Netfirewallrule -DisplayName `
    "Hyper-V Replica HTTP Listener (TCP-In)"}
} 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish