请问怎样用powershell实现一个 类linux su功能 可以在当前命令窗口提权


PowerShell交流中心分类: 变量-函数-脚本-条件-循环-参数请问怎样用powershell实现一个 类linux su功能 可以在当前命令窗口提权
1
vizodream@outlook.com asked 7 年 ago

我的想法是先获取当前目录  然后提权 然后再set-location
到当前目录  但是实现起来问题很大

Start your code here
$currentPwd= (Get-Location).Path
$currentPwd
#admin
$currentWi = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentWp = [Security.Principal.WindowsPrincipal]$currentWi
  if( -not $currentWp.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){
     $boundPara=($MyInvocation.BoundParameters.Keys|foreach{
     '-{0} {1}'-f$_,$MyInvocation.BoundParameters[$_]} )-join' '
     $currentFile=(Resolve-Path$MyInvocation.InvocationName).Path
     $fullPara=$boundPara+' '+$args-join' '
     Start-Process"$psHome\powershell.exe"-ArgumentList "$currentFile$fullPara"-verb runas
     return
 }
powershell.exe
Set-Location "$currentPwd"

 

1 Answers
0
Best Answer
Mooser Lee 管理员 answered 7 年 ago

$loc = (Get-Location ).Path
Start-Process powershell.exe -Verb runas -ArgumentList “-NoExit”,”-Command”,”& {cd $loc }”

vizodream@outlook.com replied 7 年 ago

谢谢 十分感谢