Solved

Powershell script not doing anything when run from task scheduler

Posted on 2011-09-15
3
930 Views
Last Modified: 2012-06-22
Hello,

I've had a similar problem in the past with a powershell script not running correctly from within task scheduler (http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_26895190.html). However, this time around is a bit different...the script is running but there are no results. The script basically queries AD, finds user accounts that match a criteria, and adds them a distribution group (see code). The script runs perfectly when running it at the console.

Within task scheduler, here's what I've tried:

** Option #1 **
Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add arguments (optional): & 'C:\scripts\MyScript.ps1'
Start in (optional): C:\scripts\

** Option #2 **
Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add arguments (optional): -nologo -command "& {C:\scripts\MyScript.ps1}"
Start in (optional): <null>

** Option #3 **
Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add arguments (optional): C:\scripts\MyBatchFile.bat
Start in (optional): C:\scripts\

* MyBatchFile.bat *
Powershell -command "& { C:\scripts\MyScript.ps1 }"

None of the above options seem to work...when I run the task, I get no errors and it says the task completed successfully. I've also tried various accounts to run the task...including the domain administrator...'run with highest privileges' is checked..but still nothing.

Any ideas?
# Params
$filter = "(title=*test*)"
$scope = 'dc=domain,dc=local'
$Group = Get-QADGroup -Identity "MyGroup"
$password = Get-Content C:\scripts\password.txt | ConvertTo-SecureString

# Connect to Active Directory
Connect-QADService -ConnectionAccount 'domain\administrator' -ConnectionPassword $password

# Clear group
[void](Set-QADGroup -Identity $Group.DN -Member $NULL)

# Get all enabled Active Directory accounts and add them to the specified group
Get-QADUser -Enabled -SearchRoot $scope -IncludedProperties title -LdapFilter $filter -SizeLimit 0 | `
	ForEach-Object {
		[void](Add-QADGroupMember -Identity $Group.DN -Member $_.DN )
		}

# Disconnect from Active Directory provider.
Disconnect-QADService

Open in new window

0
Comment
Question by:bndit
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 500 total points
ID: 36546770
One thing I noticed is you are not adding the quest snapin. Try to add this line to the beginning of your script and test.

add-pssnapin *Quest*
0
 
LVL 2

Author Comment

by:bndit
ID: 36546779
@KenMcF - You're the man...that did it. Thanks a bunch!
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 36546806
Glad that helped.
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 project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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