Creating a ScheduledTask in PowerShell to run when any network is available

42 Views Asked by At

I'm trying to use New-ScheduledTaskSettingsSet and Register-ScheduledTask to create a Scheduled Task using PowerShell that will run an app when any network connection is available after startup or log on.

So far I have something like this:

$taskAction =  New-ScheduledTaskAction -Execute "MyApp.exe" -Argument "/someparams"
$taskTriggers = @()
$taskTriggers += New-ScheduledTaskTrigger -AtLogOn
$taskTriggers += New-ScheduledTaskTrigger -AtStartup
$taskSettings = New-ScheduledTaskSettingsSet -RunOnlyIfNetworkAvailable -MultipleInstances IgnoreNew -RestartCount 3 -RestartInterval (New-Timespan -Minutes 2)

Register-ScheduledTask -TaskName "MyTask" -TaskPath "\MyTasks" -Description "A test task" -Action $taskAction -Settings $taskSettings -Trigger $taskTriggers

This appears to work and a new task is visible in the Task Scheduler app, but if I try to look at the Conditions tab of that task then Task Scheduler crashes, which can't be good!

I think this might be because the Microsoft documentation for New-ScheduledTaskSettingsSet says that I need to also specify -NetworkId and -NetworkName when using -RunOnlyIfNetworkAvailable

Unfortunately I can't find any documentation that says what the valid values are for NetworkId or where I can find them. It's also not clear if there is a way to specify "any network": the Task Scheduler app seems to have that as an option, but it causes the same crash!

0

There are 0 best solutions below