Hi,
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:
# Web serverversion 1.0
Web Server Powershell : https://hinchley.net/articles/create-a-web-server-using-powershell/
$url = ‘http://192.168.0.1:66/’
$template = @’
<!DOCTYPE HTML>
<html>
<head>
<title>WOL</title>
<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}
</style>
</head>
<body>
<div id=“container”>
<div id=“content”>
{page}</body>
</html>
‘@
$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” />
</form>
‘@
$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
$listener.prefixes.add($url)
$listener.start()
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)
}
$response.close()
}
Am I missing something? I need the script to run while I am not logged in.
