Unable to run powershell script in background using task scheduler?


I am unable to run a powershell script in the background using task scheduler. No matter what account I use it’s not working. The command to start the script works if I execute it directly in powershell.

The command to run in powershell is this one:

start-process powershell -WindowStyle Hidden -ArgumentList "\\my_server\Wake_On_Lan\Wake_on_lan_web_server.ps1"
The command in task scheduler:
The script is small web server in powershell:
# Web server

version 1.0

Web Server Powershell : https://hinchley.net/articles/create-a-web-server-using-powershell/

$url = ‘
$template = @’
<style type=“text/css”>
html, body, #container {height:95%}
body {font-family:verdana;line-height:1.5}
form, #container, p {align-items:center;display:flex;flex-direction:column;justify-content:center}
input {border:1px solid #999;border-radius:4px;margin-bottom:10px;padding:4px}
input[type=submit] {padding:6px 10px}
label, p {font-size:10px;padding-bottom:2px;text-transform:uppercase}
<div id=“container”>
<div id=“content”>

$form = @’
<form method=“post”>
<label for=“pc_Name”>Nom DTXXXX?</label>
<input type=“text” name=“pc_Name” value="" required />
<input type=“submit” name=“submit” value=“Submit” />
$hello = @’
<p>Wol transmit a {name}.<br/><a href="/">Ouvrir un autre PC?</p>

request actions.

$routes = @{
‘GET /’ = { return (render $template $form) }
‘POST /’ = {
# get post data.
$data = extract $request
# get the submitted name.
$name = $data.item(‘pc_Name’)
# render the name.
$page = render $hello @{name = $name}

# embed the snippet into the template.

render $template $page
start-process powershell -WindowStyle Hidden -ArgumentList “\my_server\Wake_On_Lan\Wake_on_Lan_Zenwork.ps1 -type pc -zenwork_group $name”


embed content into the default template.

function render($template, $content) {

shorthand for rendering the template.

if ($content -is [string]) { $content = @{page = $content} }
foreach ($key in $content.keys) {
$template = $template -replace “{$key}”, $content[$key]

return $template

get post data from the input stream.

function extract($request) {
$length = $request.contentlength64
$buffer = new-object “byte[]” $length
[void]$request.inputstream.read($buffer, 0, $length)
$body = [system.text.encoding]::ascii.getstring($buffer)
$data = @{}
$body.split(’&’) | %{
$part = $_.split(’=’)
$data.add($part[0], $part[1])
return $data
$listener = new-object system.net.httplistener
while ($listener.islistening) {
$context = $listener.getcontext()
$request = $context.request
$response = $context.response
$pattern = “{0} {1}” -f $request.httpmethod, $request.url.localpath
$route = $routes.get_item($pattern)
if ($route -eq $null) {
$response.statuscode = 404
} else {
$content = & $route
$buffer = [system.text.encoding]::utf8.getbytes($content)
$response.contentlength64 = $buffer.length
$response.outputstream.write($buffer, 0, $buffer.length)

Am I missing something? I need the script to run while I am not logged in.

Try moving the script to the local server. If it works, then you are most likely dealing with a double-hop issue by executing with credentials on the local server and then trying to execute a script via a UNC path which require authentication.

I just did and it’s working.

I have replaced “\my_server\Wake_On_Lan\Wake_on_lan_web_server.ps1” with “D:\Wake_On_Lan\Wake_on_lan_web_server.ps1”

Thanks a lot!