locked
Powershell script not running in scheduled task RRS feed

  • Question

  • Im attempting to run the following script in sched task

    it works perfectly fine outside of it but fails when running from an account that isnt logged in at the time

    I have tried the following

    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noprofile -nologo -file E:\~Scripts\check_service_script\Check_Service.ps1

    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noprofile -nologo -command E:\~Scripts\check_service_script\Check_Service.ps1

    and every combination of both.

    with highest privileges , without....any ideas?

    Ive tried it on a 2k8 and 2k3 server with like results

    The script imports a csv file , which contains server names and the domain they are on

    it uses get-wmiobject to see if a service is running on the server ,it then sends an email out with results


    #REMOVED ANY IDENTIFYING INFO

    $dataSource=import-csv "E:\~scripts\check_service_script\Servers2.csv"
    
    $ServOff = @()
    $ServOn = @()
    
    $password = cat E:\~Scripts\securepass.txt | convertto-securestring 
    $userprod = "prod\user"
    $usercorp = "corp\user"
    $credprod = new-object -typename System.Management.Automation.PSCredential -argumentlist $userprod,$password
    $credcorp = new-object -typename System.Management.Automation.PSCredential -argumentlist $usercorp,$password
    
    
    
    
    	function Check-Service($server, $name , $Dname) 
    	{
    	#$check = Get-Service $server -Name $name | ? { $_.Status -ne "Running" }
    	if ( $Dname -eq "prod")
    	{
    	$check = Get-WmiObject -computer $server -credential $credprod Win32_Service -Filter "Name='EventSentry'" | ? { $_.Status -ne "Running" }
    	}
    		if ( $Dname -eq "corp")
    	{
    	$check = Get-WmiObject -computer $server -credential $credcorp Win32_Service -Filter "Name='EventSentry'" | ? { $_.Status -ne "Running" }
    	}
    
    		if ( $check.State -eq "Running" ) 
    		{
    
    		$global:ServOn += "$server On <BR>"
    
    		}
    		else 
    		{ 
    
    		$global:ServOff += "$server Off <BR>"
    
    		}
    	}
    	
    
    #_____________
      function sendMail{
    
         Write-Host "Sending Email"
    
         #SMTP server name
         $smtpServer = "****.****.local"
    
         #Creating a Mail object
         $msg = new-object Net.Mail.MailMessage
    
         #Creating SMTP server object
         $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    
         #Email structure
         $msg.From = "Service_Monitor@*******.com"
         $msg.ReplyTo = "NoReply@*******.com"
         $msg.To.Add("*****@*******.com")
         $msg.subject = "Service Monitor"
    	 $msg.IsBodyHtml = $True
         $msg.body = @"
      <html><head></head><body>
    This is a notification of the Event Sentry service state.<BR>
    Service being Monitored: $($Servicea)<BR><BR><BR>
    
    The service is off for the following Servers<BR>
    $($ServOFF)<BR><BR><BR>
    
    The service is running on for the following Servers<BR>
    $($ServOn)<BR><BR><BR>
    
    
    <FONT face=Courier>$($html)</FONT>
    <BR>
    
          </body>
      </html>
    "@
    
         #Sending email
         $smtp.Send($msg)
     
    }
    
    
    
    foreach($dataRecord in $dataSource)
    {
    
    
    
    	$servera = $dataRecord.Server
    	$Servicea = 'EventSentry'
    	$Domaina = $dataRecord.Domain
    
    Check-Service $servera $Servicea $Domaina
    
    }
    sendMail
    
    
    write $ServOn
    write $ServOff
    
    
    



    • Edited by RobMDD Wednesday, July 31, 2013 6:40 PM
    Wednesday, July 31, 2013 6:35 PM

Answers

  • figured it out permissions issue with

    $password = cat E:\~Scripts\securepass.txt | convertto-securestring 

    • Marked as answer by RobMDD Thursday, August 1, 2013 12:14 AM
    Thursday, August 1, 2013 12:07 AM

All replies

  • Try this simpler version. It still needs error management to be useful as a scheduled task.

    function Check-Service($server, $name , $Dname){
        #$check = Get-Service $server -Name $name | ? { $_.Status -ne "Running" }
        if($Dname -eq "prod"){
            $check = Get-WmiObject -computer $server -credential $credprod Win32_Service -Filter "Name='EventSentry'" | ? { $_.Status -ne "Running" }
        }
        if($Dname -eq "corp"){
            $check = Get-WmiObject -computer $server -credential $credcorp Win32_Service -Filter "Name='EventSentry'" | ? { $_.Status -ne "Running" }
        }
    
        if($check.State -eq "Running"){ 
            [array]$global:ServOn+="$server On <BR>"
        }else{ 
            [array]$global:ServOff+="$server Off <BR>"
        }
    }
    	
    $password = cat E:\~Scripts\securepass.txt | convertto-securestring 
    $userprod = "prod\user"
    $usercorp = "corp\user"
    $credprod = new-object -typename System.Management.Automation.PSCredential -argumentlist $userprod,$password
    $credcorp = new-object -typename System.Management.Automation.PSCredential -argumentlist $usercorp,$password
    
    $dataSource=import-csv "E:\~scripts\check_service_script\Servers2.csv"
    foreach($dataRecord in $dataSource){
    	$servera = $dataRecord.Server
    	$Servicea = 'EventSentry'
    	$Domaina = $dataRecord.Domain
        Check-Service $servera $Servicea $Domaina
    }
    
    # splat and use Send-MailMessage for reliability
    Write-Host 'Sending Email'
    $mail=@{
        SMTPServer='****.****.local'
        From='Service_Monitor@*******.com'
        ReplyTo='NoReply@*******.com'
        To='*****@*******.com'
        Subject='Service Monitor'
        BodyAsHtml=$true
    }
    
    $Body = @"
    <html><head></head><body>
    This is a notification of the Event Sentry service state.<BR>
    Service being Monitored: $($Servicea)<BR><BR><BR>
    
    The service is off for the following Servers<BR>
    $global:ServOFF <BR><BR><BR>
    
    The service is running on for the following Servers<BR>
    $global:ServOn<BR><BR><BR>
    <FONT face=Courier>$($html)</FONT>
    </body></html>
    "@
    
    Send-MailMessage @mail -Body $Body
    

    You may have issues with authentication when running as a task because only Domain Controllers have the right to 'Delegate'. Re-authentication to a third machine may require delegation.

    PLace some error checks to a file on each of your remote calls.


    ¯\_(ツ)_/¯

    Wednesday, July 31, 2013 7:12 PM
  • Here is a function that will trap errors.  I also fixed some logic errors.

        Try{
            $splatter=@{
                ComputerName=$server
                Class='Win32_Service'
                Filter="Name='EventSentry'"
                Credential=$null
                ErrorAction='Stop'
            }
            if($Dname -eq "prod"){
                $splatter.Credential=$credprod
            }else{
                $splatter.Credential=$credcorp
            }
            $check=Get-WmiObject @splatter
            if($check.State -eq "Running"){ 
                [array]$global:ServOn+="$server On <BR>"
            }else{ 
                [array]$global:ServOff+="$server Off <BR>"
            }
        }
        Catch{
            $_ | Out-File c:\temp\logfile.log -append
        }
    }
    	


    ¯\_(ツ)_/¯

    Wednesday, July 31, 2013 7:23 PM
  • hey tanks for the resp .

    the script works fine outside of task sched , execution policy is set , i have put the triggers in.... the acc running it is a Domain admin. logged in it works fine

    logged out it hangs in a running state and does nothing , even with try catch statements... thanks for your help but still in the same boat

    Wednesday, July 31, 2013 10:11 PM
  • Add " -windowstyle hidden "


    ¯\_(ツ)_/¯

    Wednesday, July 31, 2013 10:18 PM
  • figured it out permissions issue with

    $password = cat E:\~Scripts\securepass.txt | convertto-securestring 

    • Marked as answer by RobMDD Thursday, August 1, 2013 12:14 AM
    Thursday, August 1, 2013 12:07 AM