我想执行我的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