Powershell启动进程脚本调用第二个脚本-如何仅生成一个脚本
我有一个powershell脚本,它接受“sender ip=10.10.10.10”形式的参数,并且可以使用提升的凭据完美运行Powershell启动进程脚本调用第二个脚本-如何仅生成一个脚本,powershell,parameter-passing,elevated-privileges,start-process,Powershell,Parameter Passing,Elevated Privileges,Start Process,我有一个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){ $mul
#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
}
如何在一个脚本中实现这一点,而不是使用两个脚本,即一个脚本的启动进程调用另一个脚本?我相信这将简化参数传递,因为第三方程序必须调用调用script.ps1的elevate.ps1,并且某些地方发生了错误。Windows不允许进程在运行时提升。有一些技巧,但以某种方式,它们将产生一个具有提升权限的新进程。有关更多信息,请参阅。因此,PowerShell最简单的方法仍然是使用一个脚本来启动进程,而另一个脚本被提升。与我的人Dilbert相反,我说这是可以做到的。只需使用内置的
“$myInvocation.MyCommand.Definition”
,它是获取运行脚本的完整路径的秘密变量。编写循环语句以验证是否以管理员身份运行,若不是,则以$myIvocation.MyCommand.Definition作为参数启动进程
把这个放在脚本的顶部。如果处于UAC模式,您将得到确认提示。将提升的脚本添加到写入主机开始的末尾
$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')
这里还有一些示例我认为script.ps1将值返回给highted.ps1可能更好。我将创建另一个线程来问这个问题。我在这里发布了更新的问题,如果你能帮助我,我将非常感激!!!
.\elevated.ps1 "sender-ip=10.10.10.10"
$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')