title: How to run a scheduled task to stop and start SSRS service with elevated permissions?
body: <p>I have this SSRS latency issues on my site. So I have googled it and found out that it is the common issues for so many people. Here it is:</p>
  
  <ol>
  <li><p>I have created a powershell script as follows:</p>
  
  <pre><code>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")
  </code></pre></li>
  <li><p>When i run this script from poweshell cmd it is throwing me an error says cannot open/access sql report server service. It seems like permissions issue. Then I came with this online solution, which invokes/elevates admin permissions to run the script to that perticular user.</p>
  
  <pre><code>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();
      }
  }
  </code></pre></li>
  </ol>
  
  <p>But I dont know how to run this function before running that script. Please suggest. I have added this function also to the same script file and added function-Admin() call at the top of the script to to execute this function before running the script as follows:</p>
  
  <pre><code>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")
  </code></pre>
  
  <p>But is throwing following error:</p>
  
  <pre><code>Please specify a program
  At C:\SSRS_Script\SSRSScript.ps1:3 char:39
  +     param ( [string]$program = $(throw &lt;&lt;&lt;&lt;  "Please specify a program" ),
      + CategoryInfo          : OperationStopped: (Please specify a program:String) [], RuntimeException
      + FullyQualifiedErrorId : Please specify a program
  </code></pre>