[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 107
  • Last Modified:

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.
0
Parity123
Asked:
Parity123
  • 4
  • 4
1 Solution
 
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
 
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
Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

 
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
 
SubsunCommented:
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
 
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

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now