Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Scheduling a powershell script

Posted on 2009-07-01
3
Medium Priority
?
827 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
[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
3 Comments
 
LVL 1

Accepted Solution

by:
tojo2k earned 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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 antispam), the admini…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 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