Mysql 如何在任务计划程序上执行PowerShell脚本?
我正在尝试执行Powershell脚本以每5分钟运行一次。我尝试使用Windows7的任务调度程序来运行它,但它在记事本上打开了我的脚本,并且没有在我的数据库中插入任何内容 下面是我的Powershell V-2.0脚本。我不确定我的代码中是否有错误(我对此表示怀疑),或者是否有办法将其包含在脚本中,每5分钟运行一次,比如说从上午9点到晚上9点 我是这样安排的:Mysql 如何在任务计划程序上执行PowerShell脚本?,mysql,powershell,powershell-2.0,powershell-ise,Mysql,Powershell,Powershell 2.0,Powershell Ise,我正在尝试执行Powershell脚本以每5分钟运行一次。我尝试使用Windows7的任务调度程序来运行它,但它在记事本上打开了我的脚本,并且没有在我的数据库中插入任何内容 下面是我的Powershell V-2.0脚本。我不确定我的代码中是否有错误(我对此表示怀疑),或者是否有办法将其包含在脚本中,每5分钟运行一次,比如说从上午9点到晚上9点 我是这样安排的: General -------- Run only when user is logged on Triggers --------
General
--------
Run only when user is logged on
Triggers
--------
Trigger: at 8h56am every day-
Details: After triggered, repeat every 5 minutes indefinitely
Status: Enabled
Actions
-------
Action: Start a program
Details: Path to the script
提前谢谢
POWERSHELL(不包括连接):
function ping_getUser{
#ping each computer if online, obtain information about the local hard drives only
TRY{
foreach($workstation in $workstation_list){
$command = $connection.CreateCommand();
$command.Connection = $connection;
$command.CommandText = "INSERT INTO workstation_userlogged
(username,hostname,online)
VALUES (@username,@hostname,@status)";
### =============================================================
### values to be assigned as a parameters
### =============================================================
$hostname = $workstation;
$test_ping = Test-Connection -ComputerName $hostname -Count 1 -ErrorAction SilentlyContinue;
$status = [bool](Test-Connection $hostname -Quiet -Count 1 -ErrorAction SilentlyContinue);
#if status is TRUE get username
if($test_ping)
{
TRY{
$username =( Get-WMIObject -ComputerName $hostname -Class Win32_ComputerSystem -ErrorAction Stop | Select-Object -ExpandProperty Username);
}CATCH{
Write-Host "Caught the exception" -ForegroundColor Red;
Write-Host "$_" -ForegroundColor Red;
}
TRY{
if( $test_ping -and $username )
{
$username = $username.split("\");
$username = $username[1];
echo $username;
}
}CATCH{
Write-Host "Caught the exception" -ForegroundColor Red;
Write-Host "$_" -ForegroundColor Red;
}
#if ping succeeds but no user is logged
if($test_ping -and -not $username ) # -eq ""
{
$username = "No User";
}
} #end if
#if ping DOES NOT succeed set username value to NULL and status to FALSE
elseif(!$test_ping)
{
$username = [DBNull]::Value;
}#end elseif
### =============================================================
### Assign parameters with appropriate values
### =============================================================
$command.Parameters.Add("@username", $username) | Out-Null
$command.Parameters.Add("@hostname", $hostname) | Out-Null
$command.Parameters.Add("@status", $status) | Out-null
### =============================================================
### Execute command query
### =============================================================
TRY{
$command.ExecuteNonQuery() | out-null;
Write-Host "Successfully inserted in Database";
}
CATCH{
Write-Host "Caught the exception" -ForegroundColor Red;
Write-Host "$_" -ForegroundColor Red;
}
### =============================================================
### clear parameter values
### =============================================================
$command.Dispose()
$hostname = "";
$username = "";
$status = "";
$test_ping = "";
}#end for each loop
}#end try
CATCH{
Write-Host "Caught the exception" -ForegroundColor Red;
Write-Host "$_" -ForegroundColor Red;
}#end catch
$connection.Close(); #close connection
}#end ping_test function
ping_getUser #execute function
没有任务描述,一切都是猜测。无论如何,听起来您只指定了要运行的脚本,而没有指定执行引擎。也就是说,任务调度器将能够运行
.cmd
和.bat
脚本,而无需任何额外配置。Powershell的情况并非如此。您必须指定要运行的任务powershell.exe
,并将脚本文件的路径作为参数传递。一个是在Technet
至于为什么Powershell的
.ps1
与batch不同,原因在于安全性。而不是让PuxScript脚本易于运行,速度转储MAKSE PopS壳脚本不太吸引攻击向量。 < P>对于你的计划任务的动作必须调用脚本>文件>文件。看
执行脚本的用户需要具有执行脚本中所有操作所需的权限。尝试使用此CMD包装文件从任务计划程序启动,然后命名它
runYour-PS1-filename.cmd
。该文件将创建错误日志和标准输出日志:
@echo off
SetLocal
REM
REM This cmd file will launch it's corresponding PowerShell-script.
REM
REM We need to change std character set to support international characters when calling
REM a PowerShell script. It's ohh soo 2017
REM
chcp 1252 > nul
Set MyName=%~n0
Set ErrLog="%~dp0ERR_%MyName:~3%.log"
Set LogFile="%~dp0%MyName:~3%.log"
REM This line will launch with a separate ERROR-log
PowerShell -File "%~dp0%MyName:~3%.ps1" %* >> %LogFile% 2>&1 2>> %ErrLog%
REM Remove log if empty
findstr "^" %ErrLog% || del %ErrLog% >nul
chcp 850 > nul
EndLocal
问题是脚本没有按计划正确执行,或者即使手动运行脚本,它的行为也不符合要求?如果是后者,请将脚本提取到尽可能小的程度,以演示问题。如果我手动运行脚本,它将按预期工作。它只是没有在任务调度器上运行,它只会每5分钟在记事本上打开一次脚本。好的,在这里显示脚本没有任何作用。显示您是如何在任务计划程序中计划脚本的,因为这是问题的根源。可能的重复我更新了说明并提到了我是如何计划的
@echo off
SetLocal
REM
REM This cmd file will launch it's corresponding PowerShell-script.
REM
REM We need to change std character set to support international characters when calling
REM a PowerShell script. It's ohh soo 2017
REM
chcp 1252 > nul
Set MyName=%~n0
Set ErrLog="%~dp0ERR_%MyName:~3%.log"
Set LogFile="%~dp0%MyName:~3%.log"
REM This line will launch with a separate ERROR-log
PowerShell -File "%~dp0%MyName:~3%.ps1" %* >> %LogFile% 2>&1 2>> %ErrLog%
REM Remove log if empty
findstr "^" %ErrLog% || del %ErrLog% >nul
chcp 850 > nul
EndLocal