Author Posts

August 1, 2017 at 9:51 am

Hi Guys,

I've created the following script for like ~250 server

$servers = Import-csv "C:\temp\servers.csv"
ForEach ($server in $servers){
#createfolder
New-Item "\\$($Server.Servername)\c$\Test1" -type directory
Start-Sleep 1
#copyfile
Copy-Item D:\Scripting\Files\bla1.txt -Destination \\$($Server.Servername)\c$\Test1
}

This works. Now i would like to create a scedule task on the servers remotly.

When i do it directly to one server i use:

$Action = New-ScheduledTaskAction -Execute "Cmd"
$Trigger = New-ScheduledTaskTrigger -Once -At 17:00
Register-ScheduledTask Bodhi -Action $Action -Trigger $Trigger

This works. But i would like to do it on all the servers but with foreach i cannot fix it.

Some help please 🙂

August 1, 2017 at 10:35 am

why not use group policy and push the task that way ?

https://technet.microsoft.com/en-us/library/cc725745.aspx

August 1, 2017 at 10:43 am

or if you wanted to do it via Powershell have a look at the bottom answer here using SCHTASKS.EXE

https://serverfault.com/questions/586655/scheduling-tasksjobs-across-servers-from-central-location-windows-server

August 1, 2017 at 12:34 pm

You might put your scheduled task commands into a script file, and then use the Invoke-Command cmdlet to run that script on each remote server. See the help for the Invoke-Command cmdlet for details.

August 2, 2017 at 6:56 am

Thanks Kevyn, i will look into Invoke-Command