gpt4 book ai didi

windows - 使用 "-noprofile"参数提升当前脚本

转载 作者:行者123 更新时间:2023-12-02 23:34:03 26 4
gpt4 key购买 nike

我目前正在使用下面的代码通过将代码放在我的脚本开头来提升我当前的脚本。我还想在提升脚本时包含“-noprofile”参数。我如何包含 -noprofile $newProcess.Arguments 线?

$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;

if ($myWindowsPrincipal.IsInRole($adminRole))
{

$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)";
#$Host.UI.RawUI.BackgroundColor = "DarkBlue";
Clear-Host;

} else {

$newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";
$newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"
$newProcess.WindowStyle = 'Hidden'
$newProcess.CreateNoWindow = $True
$newProcess.Verb = "runas";
[System.Diagnostics.Process]::Start($newProcess);
Exit;
}

最佳答案

以下Arguments即使脚本路径包含空格,属性设置也应该有效:

$newProcess.Arguments = "-NoProfile -Command `"& '$($script:MyInvocation.MyCommand.Path)' `""

另一种选择:
$newProcess.Arguments = "-NoProfile -File `"$($script:MyInvocation.MyCommand.Path)`""

关于windows - 使用 "-noprofile"参数提升当前脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59882362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com