Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何使用'-命令';行政模式中的争论_Powershell - Fatal编程技术网

Powershell 如何使用'-命令';行政模式中的争论

Powershell 如何使用'-命令';行政模式中的争论,powershell,Powershell,如何使用-command参数运行powershell 我尝试添加'-Verb runAs',但得到一个空值表达式 powershell -Verb runAs -command "(Get-Date (Get-Process explorer).StartTime).ToString('yyyyMMdd')" 我打开一个具有管理员权限的powershell,命令 (获取日期(获取进程资源管理器).StartTime.ToString('yyyyMMdd')) 返回正确的值。但是当我启动一个没有

如何使用-command参数运行powershell

我尝试添加'-Verb runAs',但得到一个空值表达式

powershell -Verb runAs -command "(Get-Date (Get-Process explorer).StartTime).ToString('yyyyMMdd')"
我打开一个具有管理员权限的powershell,命令

(获取日期(获取进程资源管理器).StartTime.ToString('yyyyMMdd'))

返回正确的值。但是当我启动一个没有管理员权限的powershell时,我会得到一个空值

因此,我认为问题在于“powershell-Verb runAs”没有在管理模式下运行该命令


注意:尝试此操作时,我以ad管理员身份登录。

因此,如果您不介意运行脚本而不仅仅是执行命令,则可以从脚本中提升PowerShell的进程。这将检查进程是否已提升,如果未提升,则将使用RunAs动词重新启动进程,使其以提升的权限运行

# Elevate UAC if not already running As Administrator
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)

# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator

# Check to see if we are currently running "as Administrator"
if (!$myWindowsPrincipal.IsInRole($adminRole))
{
    # We are not running "as Administrator" - so relaunch as administrator
    # Create an encoded string to re-launch the script bypassing execution policy
    $Code = ". '$($myInvocation.MyCommand.Definition)'"
    $Encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))

    # Indicate that the process should be elevated
    Start-Process PowerShell.exe -Verb RunAs -ArgumentList "-EncodedCommand",$Encoded

    # Exit from the current, unelevated, process
    exit
}
# End UACElevation

代码与命令的加密有点纠结,但我发现如果不这样做,有时执行策略会阻止我。这避免了执行策略阻止PowerShell运行脚本,因为从技术上讲,它不是在运行脚本,而是在运行编码的命令。该命令恰好用于在PSSession启动后运行脚本。

以本地管理员身份登录与以UAC管理员身份运行进程之间存在显著差异。你能接受典型的UAC提示,以管理员的身份运行某些东西吗,或者这将作为以提升权限运行的计划任务运行?我已从“控制面板”关闭了UAC。目前,我有一个perl脚本,它可以从运行Powershell脚本(如$result=`Powershell-command'我的Powershell语句)获得结果,需要在管理模式“”下执行。如果我切换到使用“启动进程”,我如何才能找回reults?将上面的代码保存为.ps1文件,然后让perl执行
powershell.exe-文件c:\path\to\script.ps1
(使用实际路径)。应该这样做。