2014-10-28 17 views
1

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

问题是脚本无法按计划正确执行,或者即使手动运行脚本也不按预期运行?如果是后者,请尽可能将文字提炼出来以证明问题。 – alroc 2014-10-28 13:26:39

+0

如果我手动运行脚本,它将按预期工作。它只是不运行在任务计划程序上,它每5分钟只在记事本中打开脚本。 – mario 2014-10-28 13:28:58

+0

好的,然后在这里显示脚本没有任何贡献。显示您如何在Task Scheduler中安排脚本,因为**是问题的根源。 – alroc 2014-10-28 13:30:25

回答

1

没有任务描述,它的所有猜测。无论如何,这听起来像你只指定了要运行的脚本,而不是执行引擎。也就是说,任务计划程序将能够运行.cmd.bat脚本,而不需要任何额外的配置。与Powershell不同。您必须指定任务以运行powershell.exe并将脚本文件的路径作为参数传递。 A trivial example在Technet。

由于为什么Powershell的.ps1不同于批次,原因是安全。而不是让Powershell脚本轻松运行,速度转储makse Powershell脚本的攻击媒介吸引力较小。

+0

中的脚本,谢谢它的工作!它帮助我理解! – mario 2014-10-28 13:57:21

0

尝试使用此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