Issues with Start-Job

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
andycarpenter@westat.com
Posts: 18
Last visit: Wed Jan 17, 2024 9:56 am

Issues with Start-Job

Post by andycarpenter@westat.com »

I have a need to run a few lines of PowerShell under different credentials than those of the account that's logged in and running the main script. Basically, this packaged EXE will be running due to an autologon&autorun in an account that has limited privilege. I need to change a Registry value to turn off autologon and so need to do that with credentials for another account with some privilege.

The first approach was using Invoke-Command of a ScriptBlock supplying the Credential and specifying localhost as the ComputerName. I got some errors related to localhost and abandoned the approach.

Now I am trying to use Start-Job to run the ScriptBlock. I am seeing some behavior that I don't understand.

Here's some sample code that just adds a line to file. For now, I'm not even providing credentials for another account.
# A quick sample Start-Job, will in work when packaged?
$SB={
$date=Get-Date -format d
$time=(Get-Date).ToLongTimeString()
$Mes=$date+" "+$time+": The Start-Job ran and worked"
Add-Content -Path "C:\SystemsTeam\Scripts\ConfigureTablet\ConfigureTablet_log.log" -Value $Mes
}
$SBJob=Start-Job -ScriptBlock $SB

When Run from PowerShell Studio, it DOES NOT work (i.e., the job appears not to run because no content is added). When Run-in-console from PowerShell Studio, it DOES appear to work. From a packaged EXE, it DOES NOT work.
PowerShell Code
Double-click the code block to select all.
.

I'm sure that this must be some simple contextual problem. Can someone please set me straight?  Then I can move on to using credentials, packaging, etc.

Thanks,
Andy
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Issues with Start-Job

Post by jvierra »

There is no reason for that to not work assuming the account has write privileges to the folder and file.

I would change to this:

$SBJob = Start-Job -ScriptBlock $SB | Wait-Job
User avatar
andycarpenter@westat.com
Posts: 18
Last visit: Wed Jan 17, 2024 9:56 am

Re: Issues with Start-Job

Post by andycarpenter@westat.com »

Thanks. I agree with you. Could not see any reason it was not working. I made the recommended change.

Did some more testing in a lot of different contexts (accounts). The job is clearly starting and doing what's required in most accounts. In one account, UAC kicks in and re-requires user type the password, even though that's been supplied in the Credential. In the most important account, which is seriously locked down, the job simply never runs. I have no idea what's preventing it, but that is not really a scripting issue.

So, thanks for your quick assistance. The packaged script is now working as expected in general, just not in this very-locked-down account. I'll have to try to find some other way to get the Registry key changed in this case.

Thanks again!
Andy
This topic is 7 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked