1
$Adminusername = 'domain\blah'
$password = 'blah'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $Adminusername, $securePassword
$path = "\\blah\script.ps1"

Start-Process powershell -ArgumentList "-noexit", "-file $path", "-command &{Set-ExecutionPolicy Bypass}", "-username $username", "-roamingprofilepath $RoamingProfilePath", "-localappdatapath $localappdatapath" -credential $Credential

The above script is a PowerShell process that starts a 2nd PowerShell, but running it as a different user. The arguments from the first PowerShell session are passed into the second one.

It works fine, except that in the 2nd PowerShell process, execution policy doesn't bypass and it will keep prompting you to allow the script to run. What am I doing wrong here?

Clijsters
  • 4,031
  • 1
  • 27
  • 37
shadowz1337
  • 710
  • 1
  • 8
  • 21
  • did you try to add "-executionpolicy bypass" to the argumentlist ? – Loïc MICHEL Feb 08 '18 at 10:37
  • Yes, still getting prompt. Have you got it working? – shadowz1337 Feb 08 '18 at 10:56
  • Tip: The thing with SecureStrings is __NOT__ having to save your password in clear text. Tip2: You might want to use [`Get-Credential`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-credential?view=powershell-6) – Clijsters Feb 08 '18 at 10:56
  • I think besides adding `-ExecutionPolicy Bypass` you should remove the `Set-ExecutionPolicy` script block – Clijsters Feb 08 '18 at 10:57
  • I know about SecureStrings and it isn't really that much of an string encryption. This script is stored in Sysvol on a domain controller, so any authenticated user could grab the script and reverse the securestring. I'm using a service account anyway, so it doesn't matter. And get-credential won't help me here. This is a logon script that is applied for every user. – shadowz1337 Feb 08 '18 at 10:58
  • `Set-ExecutionPolicy` needs a `-Force` if you don't want it to nag you. (You may also want to use `-Scope Process` to ensure you don't trample over global settings.) – Jeroen Mostert Feb 08 '18 at 11:16

1 Answers1

1

this had worked for me

Start-Process powershell.exe -ArgumentList "/noexit", "-executionpolicy bypass","-file \\server\share\test.ps1"
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103