Avatar of Garry Shape
Garry Shape
Flag for United States of America

asked on 

Schedule Powershell command

Hi,

I'm trying to run a command that will add a scheduled task that will run a PowerShell command.

Schtasks /create /tn "Give Tom access to Bob" /sc once /st 20:00 /tr "PowerShell -command Add-ADPermission -Identity "Bob Jones" -User "Tom Smith" -AccessRights ExtendedRight -ExtendedRights "send as" -ErrorAction Stop; Add-MailboxPermission "Bob Jones" -User "Tom Smith" -Access "FullAccess" -confirm: $false -ErrorAction Stop"

Open in new window


This is what I'm trying to do up there:
Run 2 commands at the specified time (once, 8:00)

But I'm running into an issue, I think, with the quotes.

I'd get the error:
ERROR: Invalid argument/option - 'Bob'.

Is this a quote issue?
Also will the ; help with running both commands within the task?
Or is the best way here to generate the commands to run into a ps1 file and then have the ps1 file run? And if so, could I add a command to delete the ps1 (itself) after its run?
PowershellExchange

Avatar of undefined
Last Comment
sirbounty

8/22/2022 - Mon