Solved

Schedule Powershell command

Posted on 2014-11-19
2
194 Views
Last Modified: 2014-11-20
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?
0
Comment
Question by:garryshape
  • 2
2 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 40454130
Yes, I think your better option would be to simply dump it into a file.
Then you schedule it similarly using
Schtasks /create /tn "Give Tom access to Bob" /sc once /st 20:00 /tr "PowerShell -file c:\folder\filename.ps1"

You may still be able to accomplish the above, but you're right in that it's typically the quotes that cause the problem.  Try using the single quote:

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"
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40455870
Glad I could help - thanks for the grade. :^)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A list of top three free exchange EDB viewers that helps the user to extract a mailbox from an unmounted .edb file and get a clear preview of all emails & other items with just a single click on mailboxes.
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question