?
Solved

Powershell script not doing anything when run from task scheduler

Posted on 2011-09-15
3
Medium Priority
?
953 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
[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
3 Comments
 
LVL 27

Accepted Solution

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

649 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