【发布时间】:2021-12-19 02:50:31
【问题描述】:
这段代码:
$username = 'Username'
$password = 'Password'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
Start-Process -FilePath powershell -WorkingDirectory "$env:ALLUSERSPROFILE" -Credential $credential -WindowStyle Hidden -ArgumentList "-NoProfile -Command `"Start-Process -FilePath wscript -Verb RunAs -ArgumentList '\`"$((Get-Location).Path -replace "'", "''")\test.vbs\`" \`"/CurrentDirectory:$((Get-Location).Path -replace "'", "''")\`" \`"/AppData:$($env:APPDATA -replace "'", "''")\`"'`""
当我从 PowerShell 命令提示符下逐行运行它时可以工作。
当我将 .ps1 扩展名关联到“Windows PowerShell”然后双击脚本时,以及通过 IronMan Software 的“PSScriptPad”在 .exe 中编译脚本时,都会出现问题。
以下链接也存在类似问题:
Powershell script executes correctly when I choose "run with powershell", but not when I "open with" powershell or double-click it
但在我的情况下,它甚至不适用于“使用 PowerShell 运行”,而且问题的原因可能与影响可执行文件的原因相同。
另一个有用的链接如下:
https://stackoverflow.com/a/58245206/45375
我该如何解决这个问题?
Windows 10 专业版 64 位
Powershell 版本:5.1.19041.1237(集成在 Windows 10 中)。
【问题讨论】: