$Adminusername = 'domainblah'
$password = 'blah'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $Adminusername, $securePassword
$path = "\blahscript.ps1"
Start-Process powershell -ArgumentList "-noexit", "-file $path", "-command &{Set-ExecutionPolicy Bypass}", "-username $username", "-roamingprofilepath $RoamingProfilePath", "-localappdatapath $localappdatapath" -credential $Credential
上面的脚本是启动第二个PowerShell的PowerShell过程,但作为其他用户运行。第一个PowerShell会话的论点传递到第二个。
它可以正常工作,除了在第二个PowerShell过程中,执行策略不会绕过,并且会不断提示您允许脚本运行。我在这里做错了什么?
这对我有用
Start-Process powershell.exe -ArgumentList "/noexit", "-executionpolicy bypass","-file \serversharetest.ps1"