我正在尝试执行我的 Powershell 脚本,每 5 分钟运行一次。我尝试使用 Windows 7 的任务计划程序来运行它,但它在记事本上打开我的脚本,并且不会在我的数据库中插入任何内容
下面是我的 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 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
执行脚本的用户需要拥有执行脚本中所有操作所需的权限。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)