?
Solved

Powershell script not doing anything when run from task scheduler

Posted on 2011-09-15
3
Medium Priority
?
945 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

770 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