2014-10-28 49 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