3

「sender-ip = 10.10.10.10」の形式でパラメーターを受け入れ、昇格された資格情報で完全に実行されるpowershellスクリプトがあります

#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
}

このスクリプトは昇格された資格情報を使用しないサード パーティ プログラムによって呼び出されるため、(サード パーティ プログラムが呼び出す) 昇格された特権を含む 2 つ目の 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

そして、私は手動でlevated.ps1を呼び出すことができます

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

2 つのスクリプト、つまり開始プロセスが他のスクリプトを呼び出す 1 つのスクリプトを使用する代わりに、これを 1 つのスクリプトで作成するにはどうすればよいですか? サードパーティのプログラムが script.ps1 を呼び出す elevate.ps1 を呼び出す必要があり、どこかでエラーが発生しているため、これによりパラメーターの受け渡しが簡単になると思います。

4

2 に答える 2

4

Windows では、実行中にプロセスを昇格させることはできません。いくつかのトリックがありますが、何らかの方法で昇格された権限を持つ新しいプロセスを生成します。詳細については、こちらを参照してください。したがって、PowerShell の最も簡単な方法は、1 つのスクリプトを使用して、昇格した別のスクリプトを Start-Process することです。

于 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 に答える