Solved

Scheduling a powershell script

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
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…

757 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

20 Experts available now in Live!

Get 1:1 Help Now