![](/img/trans.png)
[英]PowerShell MySQL Backup Script Error in Task Scheduler 0x00041301
[英]How to execute PowerShell script on Task Scheduler?
我試圖執行我的Powershell腳本以每5分鍾運行一次。 我嘗試使用Windows 7的Task Scheduler來運行它,但是它在記事本上打開了我的腳本,並且沒有在數據庫中插入任何內容
以下是我的Powershell V-2.0腳本。 我不確定代碼中是否有錯誤(對此表示懷疑),或者是否有辦法將其包含在腳本中以每5分鍾運行一次,可以說從上午9點到晚上9點。
這是我安排的時間:
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
計划任務的操作必須調用powershell.exe
,並將腳本文件作為參數傳遞。 參見http://blogs.technet.com/b/heyscriptingguy/archive/2012/08/11/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script.aspx
powershell.exe -file c:\path_to_your_script\script.ps1
執行腳本的用戶需要具有執行腳本中所有動作所需的權限。
沒有任務描述,這全都是猜測。 無論如何,聽起來您只指定了要運行的腳本,而不是執行引擎。 也就是說,任務計划程序將能夠運行.cmd
和.bat
腳本而無需任何額外的配置。 Powershell並非如此。 您必須指定任務以運行powershell.exe
並將腳本文件的路徑作為參數傳遞。 一個簡單的示例在Technet中。
由於Powershell的.ps1
與批處理不同的原因是安全性。 快速轉儲makse Powershell腳本並沒有使Powershell腳本易於運行,反而沒有吸引攻擊性。
嘗試使用此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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.