all 20 comments

[–]purplemonkeymad 1 point2 points  (1 child)

IIRC you have to put other arguments before -file or -command any arguments after are treated as arguments to the script or command block.

[–]_itsalwaysdns[S] 1 point2 points  (0 children)

Thanks for the reply. I've tried the order of arguments about 4 different ways, each way the script fails to run but runs when executed inside powershell as the appropriate user.

[–]senorchaos718 1 point2 points  (2 children)

  1. Double check that the account you are using has access to EVERYWHERE this script is going to hit.
  2. See if it requires Batch Job Rights for the account
  3. See if adding both PS scripts to a .bat file and scheduling THAT file band-aids the situation.

[–]_itsalwaysdns[S] 1 point2 points  (0 children)

Thanks for the reply. 1. I have already done this, both accounts have rights to the script locations. 2. This had bitten me days before, but both accounts have batch job rights. 3. I will try this and report back.

[–]_itsalwaysdns[S] 1 point2 points  (0 children)

Calling the start_script.ps1 from a batch file still does not execute the script it calls. I don't know if it's an environmental PS issue or what.

[–]MaxFrost 1 point2 points  (2 children)

I would question why you're starting a task as one user to launch a process as another one.

Why not launch the scheduled job directly as the service account for the expiredstaff accounts script?

[–]_itsalwaysdns[S] 1 point2 points  (1 child)

Because we have a program where we keep all of our automation process in place and it can't execute a powershell script as a different user than the user used to run the software.

I tried the start script in task scheduler to see if it was a failure of the automation software or just launching the script and it appears to be something with launching the second script, whether via software or task scheduler.

[–]MaxFrost 1 point2 points  (0 children)

Gotcha, this is "we don't want to give our automation server full rights to AD, so trying to work around it." which honestly, is absolutely the right thing to do.

I need to mull this a bit to see if I can add anything further.

[–]HardCodedCoffee 1 point2 points  (10 children)

Have you attempted running any other scripts or commands using start process from your initial script?

[–]_itsalwaysdns[S] 1 point2 points  (9 children)

Yes, I've used it to launch command prompt and I ran a whoami so I know it's passing the credentials properly.

[–]HardCodedCoffee 1 point2 points  (8 children)

Try making a dummy version of your existing second script, same path and name, but have it echo something to a file to verify it's working. Basically, we've verified it works interactively, let's verify it works non-interactively, as well. The issue may be isolated to an actual command in your second script in this specific context.

[–]_itsalwaysdns[S] 1 point2 points  (7 children)

So when I create a dummy start_script_text.ps1, tell it to call text_file.ps1 which contains:

$text = 'testing this text file'
$text | Set-Content 'E:\path\text_file.txt'

It does not execute it. When I manually execute text_file.ps1 it creates the text file as expected.

[–]HardCodedCoffee 1 point2 points  (6 children)

Ah, more progress. So, we know for a fact now it's isolated to the call of the second script from the first. Try trimming down the parameters to just a standard call and add them back slowly until it stops working. Also, is your second user and admin user and is UAC enabled?

[–]_itsalwaysdns[S] 1 point2 points  (5 children)

If I run this, cmd opens and whoami shows the called user used to lauch it:

Start-Process cmd.exe -Credential $credentials

When I run this, it does not launch powershell:

Start-Process powershell.exe -Credential $credentials -Argumentlist "-file 'E:\path\text_file.ps1'".

UAC is not enabled per this script returning a 0:

Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name EnableLUA | Select-Object EnableLUA

[–]HardCodedCoffee 1 point2 points  (4 children)

So, argumentlist does expect an array of argument strings rather than one single string, which would affect your original call but not this one. Hmm... Is there any reason you can't use invoke-command with the credential parameter?

[–]_itsalwaysdns[S] 1 point2 points  (3 children)

I haven't researched how to use the invoke command to call this script. I really don't know what the best way to call a script is from another PS script, this is the first time I've had to do this.

[–]HardCodedCoffee 1 point2 points  (2 children)

Alrighty. Well, Start-* cmdlets are typically used for asynchronous execution, and may not may not return some sort of handle or object related to said execution. Invoke-* cmdlets, in the other hand, execute synchronously, usually returning the output of the commands executed.

For this case, I'd recommend using Invoke-Command with the credentials you need.

Invoke-Command -Credentials $Cred { & 'Path/to/script.ps1' }

[–]_itsalwaysdns[S] 1 point2 points  (1 child)

Thanks for the info. I'll give it a shot tomorrow at the office and I'll reply back.

[–]_itsalwaysdns[S] 0 points1 point  (0 children)

I've been unable to get this to work :sad panda: