bitshift
bitshift

Reputation: 6862

Windows scheduled task invoking powershell to call web method results in 401

Testing this on my workstation. Setting up a windows task that executes under my account. The task simply calls a powershell script which has a call to a url inside. The web app is running under windows auth, so shouldnt my credentials from the windows task be used to invoke the powershell script? I am a local admin on my own workstation.

The powershell script is something like this:

# Setup reference to source folder, routes to api
$web = New-Object System.Net.WebClient
$order_api = "http://localhost:57110/Home/ManualInventory"
$order_response = $web.DownloadString($order_api);
#  TEST
Read-Host -Prompt "Press Enter to exit"

When I run the task, I get a http 401 error, as if the powershell script cant access the url. Will I still need to use credentials within the script?

Upvotes: 1

Views: 1803

Answers (2)

Matt
Matt

Reputation: 1240

You could try using Invoke-WebRequest and passing default credentials through that. For example:

$request = Invoke-WebRequest -Uri "http://localhost:57110/Home/ManualInventory" -UseDefaultCredentials -Method Get

You can then access its content through $request.Content. I tested this on an internal website I have access to that requires windows authentication.

Upvotes: 1

brendon.otto
brendon.otto

Reputation: 54

If you are hosting the web application within IIS you will need to apply your user credentials to the application pool for it to run as you might expect.

Upvotes: 0

Related Questions