3

我有一个 powershell 脚本,它接受“sender-ip=10.10.10.10”形式的参数,并且可以使用提升的凭据完美运行

#script.ps1

$userID=$NULL
$line_array = @()
$multi_array = @()
[hashtable]$my_hash = @{}

foreach ($i in $args){
   $line_array+= $i.split(" ")
}

foreach ($j in $line_array){
    $multi_array += ,@($j.split("="))
}

foreach ($k in $multi_array){
    $my_hash.add($k[0],$k[1])
}

$Sender_IP = $my_hash.Get_Item("sender-ip")


<#Gather information on the computer corresponding to $Sender_IP#>
$Win32OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Sender_IP 

<#Determine the build number#>
$Build = $Win32OS.BuildNumber


<#Running Windows Vista with SP1 and later, i.e. $Build is greater than or equal to 6001#>
if($Build -ge 6001){
    $Win32User = Get-WmiObject -Class Win32_UserProfile -ComputerName $Sender_IP
    $Win32User = $Win32User | Sort-Object -Property LastUseTime -Descending
    $LastUser = $Win32User | Select-Object -First 1
    $UserSID = New-Object System.Security.Principal.SecurityIdentifier($LastUser.SID)
    $userId = $UserSID.Translate([System.Security.Principal.NTAccount])
    $userId = $userId.Value
}

<#Running Windows Vista without SP1 and earlier, i.e $Build is less than or equal to 6000#>
elseif ($Build -le 6000){
    $SysDrv = $Win32OS.SystemDrive
    $SysDrv = $SysDrv.Replace(":","$")
    $ProfDrv = "\\" + $Sender_IP + "\" + $SysDrv
    $ProfLoc = Join-Path -Path $ProfDrv -ChildPath "Documents and Settings"
    $Profiles = Get-ChildItem -Path $ProfLoc
    $LastProf = $Profiles | ForEach-Object -Process {$_.GetFiles("ntuser.dat.LOG")}
    $LastProf = $LastProf | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
    $userId = $LastProf.DirectoryName.Replace("$ProfLoc","").Trim("\").ToUpper()
}

else{
    $userId = "Unknown/UserID"
}

if ($userId -ne $NULL){
    return "userId=" + $userId
}
elseif ($userID -eq $NULL)
{
    $userId = "Unknown/UserID"
    return "userId=" + $userId
}

由于此脚本将由不使用提升凭据的第三方程序调用,因此我必须创建第二个包含提升权限的 powershell 脚本(第三方程序将调用该脚本)

#elevated.ps1

[string]$abc = $args

<#Previously created password file in C:\Script\cred.txt, read-host -assecurestring | convertfrom-securestring | out-file C:\Script\cred.txt#>

$password = get-content C:\Script\cred.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username",$password


[string]$output = start-process powershell -Credential $credentials -ArgumentList '-noexit','-File', 'C:\script\script.ps1', $abc

return $output

我可以手动调用提升的.ps1

.\elevated.ps1 "sender-ip=10.10.10.10"

而不是有两个脚本,即一个脚本,启动进程调用另一个脚本,如何在一个脚本中制作它?我相信这会简化参数传递,因为第三方程序必须调用 elevate.ps1 调用 script.ps1 并且某处发生了一些错误。

4

2 回答 2

4

Windows 不允许进程在运行时提升。有一些技巧,但以一种或另一种方式,它们会产生一个具有更高权限的新进程。请参阅此处了解更多信息。因此,PowerShell 最简单的方法仍然是使用一个脚本来启动处理提升的另一个脚本。

于 2013-08-29T20:31:23.720 回答
1

与我的男人迪尔伯特相反,我说这是可以做到的。只需使用作为秘密变量的内置"$myInvocation.MyCommand.Definition"函数来获取正在运行的脚本的完整路径。编写一个循环语句来验证是否以管理员身份运行,如果不是,则以 $myIvocation.MyCommand.Definition 作为参数启动进程。

把它放在脚本的顶部。如果在 UAC 模式下,您将收到确认提示。在开始的末尾添加提升的脚本Write-Host

$WID=[System.Security.Principal.WindowsIdentity]::GetCurrent();
$WIP=new-object System.Security.Principal.WindowsPrincipal($WID);
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator;
If ($WIP.IsInRole($adminRole)){
}else {
  $newProcess = new-object System.Diagnostics.ProcessStartInfo 'PowerShell';
  $newProcess.Arguments = $myInvocation.MyCommand.Definition
  $newProcess.Verb = 'runas'
  [System.Diagnostics.Process]::Start($newProcess);Write-Host 'Prompting for Elevation'
  exit
}

Write-Host 'ElevatedCodeRunsHere';
Write-Host 'Press any key to continue...'
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')

这里还有一些启动过程示例

于 2014-01-25T04:45:11.100 回答