Solved

Schedule Powershell command

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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A couple of months ago we ran into an issue that necessitated re-creating our Edge Subscriptions. However, when we attempted to execute the command: New-EdgeSubscription -filename C:\NewEdgeSub_01.xml we received an error indicating that the LDAP se…
In this video we show how to create an email address policy 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 Mail Flow…
In this video we show how to create a mailbox database 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 Servers >> Data…

717 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