[英]How to run a scheduled task to stop and start SSRS service with elevated permissions?
我的網站上存在此SSRS延遲問題。 因此,我在Google上進行了搜索,發現這是很多人常見的問題。 這里是:
我創建了一個powershell腳本,如下所示:
Stop-Service "SQL Server Reporting Services (MSSQLSERVER)" Start-Service "SQL Server Reporting Services (MSSQLSERVER)" $wc = New-Object system.net.webClient $cred = [System.Net.CredentialCache]::DefaultNetworkCredentials $wc.Credentials = $cred $src = $wc.DownloadString("http://example.com/Reports/Pages/Folder.aspx")
當我從poweshell cmd運行此腳本時,拋出的錯誤提示我無法打開/訪問sql報表服務器服務。 似乎是權限問題。 然后,我想到了這個在線解決方案,該解決方案調用/提升管理員權限以對該特定用戶運行腳本。
function Invoke-Admin() { param ( [string]$program = $(throw "Please specify a program" ), [string]$argumentString = "", [switch]$waitForExit ) $psi = new-object "Diagnostics.ProcessStartInfo" $psi.FileName = $program $psi.Arguments = $argumentString $psi.Verb = "runas" $proc = [Diagnostics.Process]::Start($psi) if ( $waitForExit ) { $proc.WaitForExit(); } }
但是我不知道如何在運行該腳本之前運行此功能。 請提出建議。 我還將此功能添加到了相同的腳本文件中,並在腳本頂部添加了function-Admin()調用以執行此功能,然后按如下所示運行腳本:
function-Admin()
Stop-Service "SQL Server Reporting Services (MSSQLSERVER)"
Start-Service "SQL Server Reporting Services (MSSQLSERVER)"
$wc = New-Object system.net.webClient
$cred = [System.Net.CredentialCache]::DefaultNetworkCredentials
$wc.Credentials = $cred
$src = $wc.DownloadString("http://example.com/Reports/Pages/Folder.aspx")
但是拋出以下錯誤:
Please specify a program
At C:\SSRS_Script\SSRSScript.ps1:3 char:39
+ param ( [string]$program = $(throw <<<< "Please specify a program" ),
+ CategoryInfo : OperationStopped: (Please specify a program:String) [], RuntimeException
+ FullyQualifiedErrorId : Please specify a program
之所以會出現此錯誤,是因為Invoke-Admin()
函數旨在為要使用提升特權運行的程序傳遞參數。 如果您希望SSRSScript.ps1
腳本SSRSScript.ps1
使用此Invoke-Admin()
,則可以將其轉換為獨立腳本。
拿不帶函數聲明和外括號的代碼。 保存此文件名為Invoke-Admin.ps1
param ( [string]$program = $(throw "Please specify a program" ),
[string]$argumentString = "",
[switch]$waitForExit )
$psi = new-object "Diagnostics.ProcessStartInfo"
$psi.FileName = $program
$psi.Arguments = $argumentString
$psi.Verb = "runas"
$proc = [Diagnostics.Process]::Start($psi)
if ( $waitForExit ) {
$proc.WaitForExit();
}
創建該腳本后,您可以嘗試使用以下命令提升腳本:
C:\*pathtoscript*\Invoke-Admin.ps1 -program "Powershell.exe" -argumentString "-file C:\SSRS_Script\SSRSScript.ps1"
您應該會在此時得到海拔高度提示,然后一旦接受,將使用管理員權限使用您的腳本運行另一個窗口。
這絕不是實現這一目標的唯一途徑。
排程器
您在標題中有此名稱,但並未在問題中完全涵蓋。 由於需要用戶輸入,因此無法將其作為計划任務運行。 但是,您可以使用腳本執行任務,就像假定腳本無人值守一樣。
常規選項卡
Run whether user is logged on or not
Run with highest privileges
動作>新...
Action: Start a program Program/script: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Add arguments: -ExecutionPolicy Unrestricted -NoProfile -File C:\SSRS_Script\SSRSScript.ps1
Start in (optional): %SystemRoot%\system32\WindowsPowerShell\v1.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.