我想執行我的Powershell腳本,每5分鐘運行一次。我嘗試使用Windows 7的任務計劃程序來運行它,但它會在記事本中打開我的腳本,並且不會在我的數據庫中插入任何東西如何在Task Scheduler上執行PowerShell腳本?
以下是我的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
問題是腳本無法按計劃正確執行,或者即使手動運行腳本也不按預期運行?如果是後者,請儘可能將文字提煉出來以證明問題。 – alroc 2014-10-28 13:26:39
如果我手動運行腳本,它將按預期工作。它只是不運行在任務計劃程序上,它每5分鐘只在記事本中打開腳本。 – mario 2014-10-28 13:28:58
好的,然後在這裏顯示腳本沒有任何貢獻。顯示您如何在Task Scheduler中安排腳本,因爲**是問題的根源。 – alroc 2014-10-28 13:30:25