需要一些帮助。无法从批处理或计划任务中执行脚本 [英] Need some help. cant execute script from a batch or scheduled task

查看:77
本文介绍了需要一些帮助。无法从批处理或计划任务中执行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过任务调度程序执行此代码,但它无法正常工作。  如果我只是从PS窗口执行它就可以了。这是我用来调用脚本的命令行。  我已经厌倦了许多不同的方式。

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'"

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

这里是脚本:

<#	
	.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. 

谢谢Mike

推荐答案

嗨Marten,

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:

程序:

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

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

开始于:C:\Scripts\\ \\

Start in: C:\Scripts\

常规选项卡,选择最高权限等。

General Tab, select highest priviledges etc..

您也可以尝试将Start-Job更改为Start-Process。

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

您似乎正在开始工作,但没有监控或清理它们。使用Get-Job |其中{...} | 删除 - 清理已完成的作业\失败的。

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


这篇关于需要一些帮助。无法从批处理或计划任务中执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆