Solved

Schedule Powershell command

Posted on 2014-11-19
2
193 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
In this video we show how to create a User Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Mailb…
how to add IIS SMTP to handle application/Scanner relays into office 365.

825 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