Solved

Powershell: Script to run on all DC

Posted on 2016-10-20
8
80 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
Let's recap what we learned from yesterday's Skyport Systems webinar.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

624 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