Solved

Scheduling a powershell script

Posted on 2009-07-01
3
801 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DFS issue 3 26
Need help Creating PowerShell Script 5 57
Need output in powershell ( Vertical format) 2 22
Active Directory Automation 7 30
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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