?
Solved

Powershell script not doing anything when run from task scheduler

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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 anti-spam), the admin…
Loops Section Overview

840 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