Solved

Powershell: Script to run on all DC

Posted on 2016-10-20
8
53 Views
Last Modified: 2016-10-25
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
Comment
Question by:Parity123
  • 4
  • 4
8 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 41852707
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
 

Author Comment

by:Parity123
ID: 41852814
The DC's are running 2008r2.  I need to run a command del c:\temp\*.* on all DC
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41852862
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
 

Author Comment

by:Parity123
ID: 41852874
Thanks sub sun. Like the first example you showed, if I want to run multiple commands using invoke-command, how would I modify..
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 41852880
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
 

Author Comment

by:Parity123
ID: 41853087
Thanks.  If I want to include a few lines of Powershell script, can I include it in invoke-command section.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41853319
Yes you can include it in the script block as I showed in example...
0
 

Author Closing Comment

by:Parity123
ID: 41858856
Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Set OWA language and time zone in Exchange for individuals, all users or per database.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now