?
Solved

Schedule Powershell command

Posted on 2014-11-19
2
Medium Priority
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
In this video we show how to create a Shared 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 >> Sha…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…
Suggested Courses

762 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