Solved

Powershell script not doing anything when run from task scheduler

Posted on 2011-09-15
3
916 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now