Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Assistance with PowerShell Scripting - Windows Scheduled Tasks

Avatar of Brad Sims, CCNA
Brad Sims, CCNAFlag for United States of America asked on
Windows OSPowershellWindows 10
5 Comments1 Solution130 ViewsLast Modified:
I am trying to get PowerShell to run a Scheduled Task if it exists, or create a scheduled task if it doesn't.  I have verified the locations of the files and that the computer names are online and reachable (I have one here at my desk as a test).  The script looks like it runs, and I don't get any errors, but the task isn't created on the remote machine like it should be.  

Did I miss something simple?  Or is my code just wrong.  I am still trying to learn PowerShell, so either scenario is very possible.

$computers = Get-Content -Path C:\Users\****\Desktop\Computers.txt


$taskname = "McAfee Agent Update"
$taskdescription = "Update McAfee Agent Using Service Account"
$action = New-ScheduledTaskAction -Execute '\\share\McAfee.bat' `
  -Argument '-NoProfile -WindowStyle Hidden -command "& Restart-Service -displayname \"McUpdates\""'

foreach ($computer in $computers) {

$taskexists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskname }

if($taskexists) {

Start-ScheduledTask -TaskName $taskname

} else {


$ts = New-TimeSpan -Days 0 -Hours 0 -Minutes 1
$trigger =  New-ScheduledTaskTrigger -Once -At ((Get-Date) + $ts) 
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 2) -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1)


Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskname -Description $taskdescription -Settings $settings -User "System"

}

}
ASKER CERTIFIED SOLUTION
Avatar of ITguy565
ITguy565Flag of United States of America image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers