这就是我想要完成的事情:我需要用户在登录计算机时自动运行powershell脚本,让脚本启动Elevated Powershell提示符(就像用户可以以管理员身份单击Run Powershell一样),然后让它在新的中运行一些命令Powershell对象,然...

这就是我想要完成的事情:
我需要用户在登录计算机时自动运行powershell脚本,让脚本启动Elevated Powershell提示符(就像用户可以以管理员身份单击Run Powershell一样),然后让它在新的中运行一些命令Powershell对象,然后关闭新的Powershell对象.
此函数当前将在Elevated模式下创建并运行新的Powershell对象.
function Set-Elevation
{
# Create a new process object that starts PowerShell
$newProcess = New-Object System.Diagnostics.ProcessStartInfo "powershell";
# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess) | Out-Null
}
但是,如何让它在那里运行新命令?之后如何关闭物体?
任何关于语法的提示都将不胜感激.
解决方法:
您可以使用内置的Start-Process命令:
function IsAdministrator
{
$Identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$Principal = New-Object System.Security.Principal.WindowsPrincipal($Identity)
$Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
function IsUacEnabled
{
(Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System).EnableLua -ne 0
}
#
# Main script
#
if (!(IsAdministrator))
{
if (IsUacEnabled)
{
[string[]]$argList = @('-NoProfile', '-NoExit', '-File', $MyInvocation.MyCommand.Path)
$argList += $MyInvocation.BoundParameters.GetEnumerator() | Foreach {"-$($_.Key)", "$($_.Value)"}
$argList += $MyInvocation.UnboundArguments
Start-Process PowerShell.exe -Verb Runas -WorkingDirectory $pwd -ArgumentList $argList
return
}
else
{
# Log an error, do nothing or Start-Process -Credentials <admin_creds>
}
}
织梦狗教程
本文标题为:c# – 如何启动新的PowerShell实例并在其中运行命令?


基础教程推荐
猜你喜欢
- C# 解析XML和反序列化的示例 2023-04-14
- C#中的Linq to JSON操作详解 2023-06-08
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- 实例详解C#实现http不同方法的请求 2022-12-26
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#中 Json 序列化去掉null值的方法 2022-11-18
- Unity shader实现高斯模糊效果 2023-01-16