Vote count: 0
Since i have searched like forever and just didn't find what i was looking for and there might be some mistakes in my code (i started powershell like 2 months ago) i want to ask for help.
I have written a simple WebServer in Powershell, that executest some comannds on the machine it is running. If i start the script directly from ise or "execute with powershell" everything is alright.
Problem is, if i try to run it as a Job at Startup, it either stops in seconds or if not, the comands i run have no effect on the machine.
My goal is, even with no user logged in, to be able to lock the machine and screen to release both and to take screenshots of what is going on the Desktop.
Here is the code for my server.
<#
creates a WindowsForm for locking the screen
$f = Path to image file
#>
function create-LockScreen($f){
$file = (get-item $f)
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$form = new-object Windows.Forms.Form
$form.Text = "Desktop Locked"
#$form.width = 1280
#$form.height = 1024
$form.WindowState = "Maximized"
$form.FormBorderStyle = "None"
$form.TopMost = $true
$form.BackgroundImage = [System.Drawing.Image]::Fromfile($file)
return $form
}
function lock-Screen($img){
$form = create-LockScreen($img)
$form.Show()
return $form
}
function enable-Screen($img){
$img.Close()
}
function parseRequest($req){
write-host $req
if ($req.Contains("?Action=")){
$do = $req.Substring(8)
}else{
$do = "Error"
}
return $do
}
function getScreenShot{
$pwd = $env:windir
$screenCapturePathBase = "$pwd\ScreenCapture"
$name = "${screenCapturePathBase}$"+ (Get-Date).ToString('yyyyMMdd_HHmmss')+".jpg"
#$bitmap.Save($name, $jpegCodec, $ep)
$b = New-Object System.Drawing.Bitmap([System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width, [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height)
$g = [System.Drawing.Graphics]::FromImage($b)
$g.CopyFromScreen((New-Object System.Drawing.Point(0,0)), (New-Object System.Drawing.Point(0,0)), $b.Size)
$g.Dispose()
$b.Save($name)
return $name
}
function evalRequest($action){
switch ($action) {
"getScreenShot" {
try{
Write-Output "Capturing Screen" | Out-File $logfile -Append
$b = getScreenShot
}
catch{
Write-Output $Error.ExceptionMessage | Out-File $logfile -Append
}
$response.ContentType = "image/jpeg"
$buffer =[System.IO.File]::ReadAllBytes($b)
return $buffer
break;
}
"disableInput" {
Disable-UserInput
$m = "Disabled user input"
$buffer = [System.Text.Encoding]::Unicode.GetBytes($m)
$response.ContentType = "text/html"
return $buffer
break;
}
"enableInput" {
Enable-UserInput
$m = "Enabled user input"
$buffer = [System.Text.Encoding]::Unicode.GetBytes($m)
$response.ContentType = "text/html"
return $buffer
break;
}
"lockScreen" {
Write-Output "LockSreen" | Out-File $logfile -Append
Disable-UserInput
try{
$global:lockedscreen = lock-Screen("C:\users\Administrator\Desktop\keep-calm-and-listen-to-your-teacher-50.png")
}catch{
}
$m = "Locked Screen"
$buffer = [System.Text.Encoding]::Unicode.GetBytes($m)
$response.ContentType = "text/html"
return $buffer
break;
}
"enableScreen" {
Enable-UserInput
enable-Screen($global:lockedscreen)
$m = "Enabled Screen"
$buffer = [System.Text.Encoding]::Unicode.GetBytes($m)
$response.ContentType = "text/html"
return $buffer
break;
}
"getBefore" {
}
"quit" {
$global:running = $false
# return $false
# return $running
break;
}
default {
$m = "Error: Action does not exist!"
$buffer = [System.Text.Encoding]::Unicode.GetBytes($m)
$response.ContentType = "text/html"
return $buffer
break;
}
}
}
#function Start-Server{
$url = 'http://*:1992/'
$logfile = "C:\Server_running.txt"
write-host $url
Add-Type -AssemblyName System.Windows.Forms
Import-Module -Name ClientControl
$listener = new-object system.net.httplistener
$listener.prefixes.add($url)
$listener.start()
write-output "Start listener_$((Get- Date).ToString('yyyy.MM.dd_HH:mm:ss'))" | Out-File $logfile -Append
$global:running = $true
$global:lockedscreen
try{
# listening url.
while ($listener.islistening) {
# write-host "running1 = $running"
write-output "Start listening...$((Get-Date).ToString('yyyy.MM.dd_HH:mm:ss'))" | Out-File $logfile -Append
$context = $listener.getcontext()
$request = $context.request.Url
$response = $context.response
#$pattern = "{0} {1}" -f $request.httpmethod, $request.url.localpath
write-output "$"+(Get-Date).ToString('yyyyMMdd_HHmmss')+"> $request" | Out-File $logfile -Append
# write-host $request.query
$todo = parseRequest($request.Query)
$action = evalRequest($todo)
#write-output "Action : $action" | Out-File $logfile -Append
if (!$global:running){
$m = "Stopped Server"
$buffer = [System.Text.Encoding]::Unicode.GetBytes($m)
$response.ContentType = "text/html"
$response.contentlength64 = $buffer.length
$response.outputstream.write($buffer, 0, $buffer.length)
$response.OutputStream.flush()
$response.close()
break;
}else{
# $buffer =[System.IO.File]::ReadAllBytes($content)
# $con = Get-Content -Stream -Path $content
$response.contentlength64 = $action.length
$response.outputstream.write($action, 0, $action.length)
$response.OutputStream.flush()
$response.close()
}
}
}
finally{
Write-Output "Server closed $((Get-Date).ToString('yyyy.MM.dd_HH:mm:ss'))" | Out-File $logfile -Append
$LISTENER.Stop()
$LISTENER.Close()
}
# }
asked 55 secs ago
Run a powershell WebSever AtStartup as Job/Task...
Aucun commentaire:
Enregistrer un commentaire