Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell, Scheduled Task, Exchange

Posted on 2012-04-13
11
Medium Priority
?
792 Views
Last Modified: 2012-04-13
Hello Experts,

I have a somewhat bizarre problem which I cant figure out. I have a windows 2008 server r2, which acts as a script runner. In other words it's only purpose is to run scheduled scripts.
Everything and all of the scripts works fine, except one of the scripts. This script bulks out mailbox end user information from exchange 2003 and outputes it to a csv file.

My problem:
When I run the script manually I get all the headers + information out in the csv file
When the script is run from scheduled tasks, it does not output all the information required.

Script code:
$Date = Get-Date
$Date = $Date.ToString("ddMMyyyy")

Get-WmiObject -Class Exchange_Mailbox -NameSpace root\microsoftexchangev2 -ComputerName ExchangeServerName1,ExchangeServerName2 | where {$_.DateDiscoveredAbsentInDS -eq $null} | select @{Label='SamAccountName';Expression={(Get-QADUser -Identity $_.LegacyDN).SamAccountName}}, @{Label='mail';Expression={(Get-QADUser -includedproperties "mail" -Identity $_.LegacyDN).mail}}, ServerName, StorageGroupName, Storename, Size, mailboxdisplayname | Export-Csv C:\Management\MailboxUserInfo\Exchange2003MUI$date.csv -NoTypeInformation

When i run the script manually heres what the output looks like:
SamAccountName,"mail","ServerName","StorageGroupName","Storename","Size","mailboxdisplayname"
Firma,"Firstname.Lastname@domain.se","SMCR090V","First Storage Group","Std2","184319","FullName"
EM0403,"Firstname.Lastname@domain.se","SMCR090V","First Storage Group","Std2","567","FullName"

But when ran from scheduled tasks, the output looks like this:
SamAccountName,"mail","ServerName","StorageGroupName","Storename","Size","mailboxdisplayname"
,,"SMCR090V","First Storage Group","Std2","184319","Fullname"
,,"SMCR090V","First Storage Group","Std2","567","Fullname"


Conclusion:
As you can see when the script is ran from scheduled tasks it will not output the SamAccountName and Mail... Any ideas experts ?

cheer,s

//Juuso
0
Comment
Question by:JuusoConnecta
[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
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37842136
in the Schedule task have you entered the username and password with account that you running manually? it may be the permissions issue.
0
 
LVL 11

Author Comment

by:JuusoConnecta
ID: 37842165
Thanks for the input. I've taken that into consideration and the result is the same.
I have scheduled the script to be run with the same user account and password than when I run it manually..
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 37842177
Have you set script to run with highest privileges?
0
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 11

Author Comment

by:JuusoConnecta
ID: 37842187
Yes
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 37842435
how did you schedule the script? In a batch file?
0
 
LVL 11

Author Comment

by:JuusoConnecta
ID: 37842474
No, its directly a powershell script.

Program script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add argument: C:\Management\Scripts\WorkingScripts\Exchange2003UserInfo.ps1
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37842543
can you check below post and see if this can be the reason

http://www.jamssupport.com/KnowledgebaseArticle50027.aspx
0
 
LVL 11

Author Comment

by:JuusoConnecta
ID: 37842588
Have tried that as well, that isnt the issue. The script is fully functional when running it manually, but not when running thru scheduled tasks, regardless if you use the parameter set-executionpolicy.

I can also add that all other scripts, for example a script that basically does the same thing runs against Exchange 2010 works perfectly, but not the one that goes against exchange 2003..

regards
0
 
LVL 6

Accepted Solution

by:
slidingfox earned 2000 total points
ID: 37842623
Looks like the SAMAccountName and Mail items are pulled using the Quest tools (Get-QADUser). Could you try adding the following to the top of the script and see if it makes a difference?

Note: This is assuming that you've installed the Quest AD CmdLets in the default location.

Add-PSSnapin Quest.ActiveRoles.ADManagement
& 'C:\Program Files\Quest Software\Management Shell for AD\qsft.ps1'

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 37842628
Did you issue exact the same commandline? Get-QADUser is Quest cmdlet not loaded as part of the default modules. Your command for testing it manually should say
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Management\Scripts\WorkingScripts\Exchange2003UserInfo.ps1

Open in new window

, started in e.g. a Cmd window.
0
 
LVL 11

Author Comment

by:JuusoConnecta
ID: 37842739
Cheers slidingfox, been working so many scripts lately in the same powershell session (probably havent closed it for 3 weeks), so I did not even notice that the quest modules were already in the session, of course I didnt have the add-pssnapin parameter in the scheduled script...

Thanks for this, it was the last of my 10 scripts =), can now relax with a good conscious. Cheers!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
On September 18, Experts Exchange launched the first installment of the Help Bell, a new feature for Premium Members, Team Accounts, and Qualified Experts. The Help Bell will serve as an additional tool to help teams increase question visibility.
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

719 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