Solved

Scheduling a powershell script

Posted on 2009-07-01
3
790 Views
Last Modified: 2012-05-07
Hello All,
I'm having trouble with using the Scheduled Tasks utility to run a powershell script.  I have read the posts here and on different sites and they all have indicated that I should:
1.  Create my powershell script.
2.  Create a batch file to run that script.
3.  Use the Scheduled Task utility to execute the batch file.

I have created my powershell script.  Here is the code:

$OS = "Windows*server*"
$Servers = Get-QADComputer -OSName $OS | foreach{$_.name} | Sort-Object
$results = ""
Foreach ($item in $Servers) {
$PingResult = Get-WmiObject -Query "Select * From win32_PingStatus Where address='$item'"
      If ($PingResult.statuscode -ne 0) {
      $results = $results+ "$item is not pingable and the server is off-line. `n"
      }
}

$mail = New-Object System.Net.Mail.MailMessage("Server@test.com","nick@test.com","Offline Servers", $results)
$smtp = New-Object System.Net.Mail.SmtpClient("mail01.summitlan.states", "25");
$smtp.Send($mail);

I have created a batch file to execute the powershell script.  Here is that code:
Powershell -command "$ {c:\sascripts\OfflineServers.ps1}"

When I run the powershell script from PowerShell or PowerGUI it works fine.  When try to schedule the script to run, I get a blank email.

I thought this could be a security issue so I set the executionpolicy to unrestricted and I verified that the scheduled task was configured to run with my credentials.

As always, and and all help is extremely appreciated.

Regards,
Nick
0
Comment
Question by:ndalmolin_13
3 Comments
 
LVL 1

Accepted Solution

by:
tojo2k earned 500 total points
ID: 24759574
I wonder if your profile is not being loaded.  Try adding this line to the top of your script:

add-PSSnapin quest.activeroles.admanagement -ea SilentlyContinue

That will make sure that the Quest cmdlets are loaded.
0
 
LVL 1

Author Comment

by:ndalmolin_13
ID: 24762981
Here was the problem:
Powershell -command "$ {c:\sascripts\OfflineServers.ps1}"

The "$" in the above line should have been a "&".

I made this change and it worked as expected.

0
 
LVL 1

Expert Comment

by:jjoz
ID: 27261946
still it didn't work out man, see the attached code.

G:\>Powershell -command "& {G:\Print.ps1}"

File G:\Print.ps1 cannot be loaded. The file G:\Print.ps1 is not digitally sign

ed. The script will not execute on the system. Please see "get-help about_signi

ng" for more details..

At line:1 char:16

+ & {G:\Print.ps1 <<<< }

    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException

    + FullyQualifiedErrorId : RuntimeException

Open in new window

0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

This article will help you understand what HashTables are and how to use them in PowerShell.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

947 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

24 Experts available now in Live!

Get 1:1 Help Now