Solved

Powershell: Script to run on all DC

Posted on 2016-10-20
8
59 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
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

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
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.

785 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