Link to home
Start Free TrialLog in
Avatar of Parity123
Parity123Flag for United States of America

asked on

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.
Avatar of SubSun
SubSun
Flag of India image

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

Avatar of Parity123

ASKER

The DC's are running 2008r2.  I need to run a command del c:\temp\*.* on all DC
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

Thanks sub sun. Like the first example you showed, if I want to run multiple commands using invoke-command, how would I modify..
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.  If I want to include a few lines of Powershell script, can I include it in invoke-command section.
Yes you can include it in the script block as I showed in example...
Thanks.