none
Need some help. cant execute script from a batch or scheduled task RRS feed

  • Question

  • I am trying to execute this code via task scheduler and it will not work.  if I just execute it from a PS windows it works just fine. Here is the command line that I am using to call the script.  I have tired a number of different ways.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Scripts\rebootExtend.ps1'"

    here is the script:

    <#	
    	.NOTES
    	===========================================================================
    	 Created with: 	SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.93
    	 Created on:   	9/26/2015 7:39 AM
    	 Created by:   	Mike Snow
    	 Organization: 	Marten Transport LTD
    	 Filename:     	
    	===========================================================================
    	.DESCRIPTION
    		reboots the citrix servers listed in a file
    #>
    
    
    
    
    #main reboot code
    
    $code = {
    	param ($name)
    
    	$computerNames = Get-Content "c:\scripts\ListofComputers.txt"
    
    	
    	Import-Module PSTerminalServices
    	$a = 20
    	Get-TSSession -State Active -ComputerName $name | Send-TSMessage -text "Server is going down for a daily reboot in 30 minutes"
    	do
    	{
    		
    		
    		Start-Sleep (1 * 600)
    		
    		Get-TSSession -State Active -ComputerName $name | Send-TSMessage -text "Server is going down for a daily reboot in $a minutes"
    		$a -= 10
    	}
    	while ($a -ge 0)
    	
    	Get-TSSession -State Active -ComputerName $name | Send-TSMessage -text "I am rebooting now"
    	
    	#Restart-Computer -ComputerName $name -Force
    	
    }
    
    
    # starts the reboot of all listed computers in a threaded manner.
    
    foreach ($computerName in $computerNames)
    {
    	
    	start-job -ScriptBlock $code -ArgumentList $computerName
    	
    }
    
    

    I feel its something that its something about the start-job.  Any suggestions would be great.  I need all the servers to execute the code at one time. 

    Thanks Mike

    • Moved by Bill_Stewart Monday, November 23, 2015 9:23 PM Unanswerable; problem description too vague; this is not "magically figure out what I am doing wrong somehow" forum
    Monday, September 28, 2015 11:56 AM

All replies

  • Hi Marten,

    See if you can get a simple script running first as a scheduled task.

    Get-Services | Out-File Out.txt

    If its not running try this in ACtion of Task:

    Program:

    Powershell.exe -file 'C:\Scripts\rebootExtend.ps1'

    Start in: C:\Scripts\

    General Tab, select highest priviledges etc..

    You can also try changing the Start-Job to Start-Process.

    You seem to be starting jobs but not monitoring or cleaning them. Use Get-Job | where{...} | Remove-Job to cleanup completed\Failed ones.


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    • Edited by Satyajit321 Monday, September 28, 2015 12:07 PM
    • Proposed as answer by Josh Lavely Monday, September 28, 2015 12:17 PM
    Monday, September 28, 2015 12:07 PM
  • so the basic get-service to an out file works just fine. I have tried the start-process as well and it still does not work.  Is there a better way to do this?  Start a chunk of code on multiple systems at once?
    Monday, September 28, 2015 12:36 PM
  • i have tried to call the PS script from a simple batch file with the same issue.. just wanted to add this to the issue.

    Mike

    Monday, September 28, 2015 1:16 PM
  • a little more.. if I package it with sapien PS studio is does not work either.. even with domain admin creds
    Monday, September 28, 2015 2:18 PM
  • What is meant by "it doesn't work"? 


    \_(ツ)_/

    Monday, September 28, 2015 2:22 PM
  • Hi,

    I generally use -File instead of -Command.

    Have you tested the script with the account you're running it under via the task scheduler?


    Monday, September 28, 2015 2:22 PM
  • same results as the first time
    Monday, September 28, 2015 2:23 PM
  • same results as the first time

    ?


    Monday, September 28, 2015 2:24 PM
  • Try it this way:

    $code = {
    	param ($name)	
    	$computerNames = Get-Content "c:\scripts\ListofComputers.txt"
    	Import-Module PSTerminalServices
    	$a = 20
    	Get-TSSession -State Active -ComputerName $name | Send-TSMessage -text "Server is going down for a daily reboot in 30 minutes"
    	do {
    		Start-Sleep (1 * 600)
    		Get-TSSession -State Active -ComputerName $name | Send-TSMessage -text "Server is going down for a daily reboot in $a minutes"
    		$a -= 10
    	} while ($a -ge 0)
    	Get-TSSession -State Active -ComputerName $name | Send-TSMessage -text "I am rebooting now"
    	Restart-Computer -ComputerName $name -Force
    }
    
    
    foreach ($computerName in $computerNames) {	
    	start-job -ScriptBlock $code -ArgumentList $computerName
    }
    Get-Job | Wait-Job | Receive-Job
    


    \_(ツ)_/

    Monday, September 28, 2015 2:25 PM
  • i have tried that as well.  I am logged on to the box as the same user as i execute with the TS. 

    Monday, September 28, 2015 2:25 PM
  • will try this.. be back in a moment.
    Monday, September 28, 2015 2:26 PM
  • i have tried that as well.  I am logged on to the box as the same user as i execute with the TS. 

    And you've set up the task how exactly?


    Monday, September 28, 2015 2:26 PM
  • no joy still will not execute from a batch flie or TS
    Monday, September 28, 2015 2:28 PM
  • i have tried that as well.  I am logged on to the box as the same user as i execute with the TS. 

    And you've set up the task how exactly?


    Program:

    Powershell.exe -file 'C:\Scripts\rebootExtend.ps1'

    Start in: C:\Scripts\

    General Tab, select highest priviledges etc..


    Monday, September 28, 2015 2:29 PM
  • Try adding Start-Transcript and see if the output sheds any light.

    Monday, September 28, 2015 2:34 PM
  • ok i think I got it..  i think it was a couple things.. it was not pulling the computer name from the txt file and the get-job|wait-job| receive-job part.  here is the finished working code.

    <#	
    	.NOTES
    	===========================================================================
    	 Created with: 	SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.93
    	 Created on:   	9/26/2015 7:39 AM
    	 Created by:   	Mike Snow
    	 Organization: 	Marten Transport LTD
    	 Filename:     	
    	===========================================================================
    	.DESCRIPTION
    		reboots the citrix servers listed in a file
    #>
    $computerNames = Get-Content c:\scripts\ListofComputers.txt
    
    $code = {
    	param ($name)
    	
    	Import-Module PSTerminalServices
    	$a = 20
    	Get-TSSession -State Active -ComputerName $name | Send-TSMessage -text "Server is going down for a daily reboot in 30 minutes"
    	do
    	{
    		Start-Sleep (1 * 600)
    		Get-TSSession -State Active -ComputerName $name | Send-TSMessage -text "Server is going down for a daily reboot in $a minutes"
    		$a -= 10
    	}
    	while ($a -ge 0)
    	Get-TSSession -State Active -ComputerName $name | Send-TSMessage -text "I am rebooting now"
    	Restart-Computer -ComputerName $name -Force
    }
    
    
    foreach ($computerName in $computerNames)
    {
    	
    	start-job -ScriptBlock $code -ArgumentList $computerName
    }
    Get-Job | Wait-Job | Receive-Job
    

    Monday, September 28, 2015 2:55 PM
  • Hi Marten,

    Alternately you might what to try this in the loop, otherwise you need to wait for all the jobs to complete.

    Get-Job | ?{$_.State -ne 'Running'} | Receive-Job

    }



    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Tuesday, September 29, 2015 5:54 AM