Powershell: Script to run on all DC

Hello Experts,

I need to run a command on all DCs remotely. Could you please help me with a PowerShell/WinRm script.

Your assistance is much appreciated.
Parity123Asked:
Who is Participating?
 
SubsunConnect With a Mentor Commented:
Here is a simple example..
Import-Module Activedirectory
$DCLict = Get-ADDomainController -Filter * | Select -exp name
Invoke-Command -Comp $DCLict { 
Ipconfig /Flushdns 
DCDiag
repadmin /showrepl
}

Open in new window

1
 
SubsunCommented:
If remote PowerShell is enabled on DC's then, you can use Invoke-Command to run commands remotely. But details like what you trying to achieve and the OS on DC's will help you to get a clear answer!
http://ss64.com/ps/invoke-command.html
Example, following code will run Ipconfig /Flushdns command on all DC's
Import-Module Activedirectory
$DCLict = Get-ADDomainController -Filter * | Select -exp name
Invoke-Command -Comp $DCLict { Ipconfig /Flushdns }

Open in new window

1
 
Parity123Author Commented:
The DC's are running 2008r2.  I need to run a command del c:\temp\*.* on all DC
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
SubsunCommented:
Does remote PowerShell enabled or can be enabled on all DC's?

Or if admin shares are enabled, then you can try..
Import-Module Activedirectory
Get-ADDomainController -Filter * | %{Remove-Item "\\$($_.Name)\c$\temp\*" -recurse}

Open in new window

1
 
Parity123Author Commented:
Thanks sub sun. Like the first example you showed, if I want to run multiple commands using invoke-command, how would I modify..
0
 
Parity123Author Commented:
Thanks.  If I want to include a few lines of Powershell script, can I include it in invoke-command section.
0
 
SubsunCommented:
Yes you can include it in the script block as I showed in example...
0
 
Parity123Author Commented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.