Jay
Jay

Reputation: 21

WebAdministration module not loading when ran from Scheduled Task

I am running into an issue with a script where a cmdlet from an imported module is not being executed when running from a scheduled task.

I created a basic test script which uses the Stop-Website cmdlet and added a try/catch to trap any errors from the cmdlet.

When I run this code from the ISE or Cmd line it works as expected; Website1 gets stopped.

When I run this from the scheduled task GUI, WebSite1 does not get stopped and the following error is caught by the catch:

The cmdlet error is: Cannot find drive. A drive with the name 'IIS' does not exist.

From what I've read this is just means the cmdlet is not available.

I've confirmed the module is installed. On the Action tab I'm passing

powershell -ExecutionPolicy ByPass -NoProfile -file "D:\temp\stop_site.ps1"
Import-Module WebAdministration
try {
    Stop-Website -Name Website1 -ErrorAction Stop
} catch {
    $ErrorMessage = $_.Exception.Message
    Write-Host "The cmdlet Error is: $ErrorMessage" 
}

I'm looking for any suggestions on how I can get the Stop-Website cmdlet to work when running it from a scheduled task.

Upvotes: 0

Views: 326

Answers (1)

Jay
Jay

Reputation: 21

I found the issue to the problem. The user account that was launching the task did not have the appropriate user permissions. When I used an account from the Local Admin group it worked.

Upvotes: 1

Related Questions