windows - 使用“-noprofile”参数提升当前脚本
问题描述
我目前正在使用以下代码通过将代码放在我的脚本开头来提升我当前的脚本。我还想在提升脚本时包含“-noprofile”参数。如何在$newProcess.Arguments行中包含-noprofile?
$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)`""
推荐阅读
- powershell - 基于 Robocopy 结果的 Powershell Conditional
- verilog - FSM 的这种风格是否正确且可综合?
- c++ - std::condition_variable 的内存位置可能导致 futex 错误
- php - 如何从php中的对象获取值
- python - 在 django 中,用户应该只能看到他们上传的文件
- python - 检查 NumPy 数组是否在 Python 列表中
- python - Python-Selenium:如何在新行中将列表全部导出为 CSV?
- convertigo - 为什么步骤“Get_from_session”在带有 corvertigo 服务器 7.7 的 ios 中不起作用?
- class - 将类变量转换为对象
- pycharm - 如何从 PyCharm/JetBrains IDE 启动 Windows 终端