Scheduldtask script question

Guys, you helped me so much this week. Last question to complete everything

I’ve got on every server (±150) my reboot schedule task. but every month i would like to restart the servers after patching.

I created the following:

$servers = Get-Content C:\temp\servers.txt
$DateTime = Read-Host “Date and Time (MM/DD/YY)”
$User = Read-host “Admin username”
$Password = Read-Host “Enter Password” -AsSecureString

foreach ($server in $servers)
{
Enter-PSSession $server
$Time = New-ScheduledTaskTrigger -At $DateTime -Once
Set-ScheduledTask -TaskName “Rebootserver” -Trigger $Time -User nbdbiblion$user -Password $password
Exit-PSSession
}

I works, but i creates only locally on my computer but i would like to create it on the servers. I think inovke-command wil not work because of the variables that are needed.

Thanks again.

Have a look at how to schedule server reboot task on multiple servers

The bottom script does exactly what you require (I think ) :slight_smile:

Thanks simon but that not what i am looking for because i have already deployment other scripts on my severs. There has to be a way to do it like i posted above?

Are your servers in the same OU ?

Yes they are, but i would like to manage which servers are about to restart so not all servers in the OU

Enter-PSSession for interactive use only

you should use Invoke-Command with $using: variable prefix

look at example 9 for Invoke-Command