Powershell script only running in ISE

Viewed 127

Running a script that starts and kills/cleans two jobs via batch file before I package it for an EXE, works great in ISE elevated but immediately fails in console or command prompt with the following:

Error

The code I have put together is here: https://pastebin.com/FWaZD249

  • I tested it with:
    1. PS1 to EXE, get the same results
    2. Non-elevated ISE same results
    3. Elevated console same results
    4. Elevated CMD same results
    5. Elevated ISE works (only after saving?)

It's really close to being done, basically, it's just a little script that checks the 5900 port for established connections, updates a form of a list of connections, and sends a little notification if someone new has connected, it runs on our print server computer which is screen-shared remotely via TightVNC, so operators don't get surprised when their mouse starts moving on them.

cmd batch code looks like this

powershell.exe -NoExit ". C:\Users\VS-Print-Server\Desktop\Userchecker.PS1"
1 Answers

As I mentioned in the comment: you initialize the jobs $job1 and job2 with $FormLib respectively $LisLib before those variables are set/initialized. Moving line 1 through 30 (everything before Write-Verbose -Verbose 'Before:') to the very end should make it work.

Related