【问题标题】:How to launch an other ps1 within a ps1 with an other accompt (and admin rights)?如何在具有其他伴奏(和管理员权限)的 ps1 中启动其他 ps1?
【发布时间】:2023-04-05 13:02:01
【问题描述】:

我的一些用户计算机存在错误,我需要我的用户从他们的计算机启动 .ps1 来解决问题,以便我可以在他们需要时通过 NetSupport 访问他们的计算机。 问题是他们的计算机没有管理员权限。

这就是我已经做过的:

  • 在 .txt 中加密管理员密码(此密码将由我以管理员权限启动)
    Function RandomKey {
    $RKey = @()
    For ($i=1; $i -le 16; $i++) {
    [Byte]$RByte = Get-Random -Minimum 0 -Maximum 256
    $RKey += $RByte
    }
    $RKey
}
$Key = RandomKey

$key |Out-File "$path\Key.txt"

Read-Host "Enter one admin Password" -assecurestring | ConvertFrom-SecureString -key $Key | Out-file "$path\EncKey.txt"

这部分似乎工作正常。 现在,开始工作的“客户”部分:

$PassKey = get-content "$Path\Key.txt"
$Password = get-content "$Path\EncKey.txt" | Convertto-SecureString -Key $PassKey
$User = Read-Host "Enter the ID given by your administrator"
$credentials = New-Object System.Management.Automation.Pscredential `
-Argumentlist $User,$Password

还有一个不起作用的(我在这里尝试了很多东西):

  • 1:当我设置本地管理员 (.\administrator) 时,新的 powershell Windows 以管理员权限启动,但不执行 file.ps1 应该执行的操作,如果我设置 domain\adminaccount 它只是启动一个新的 posershell 窗口,但没有管理员权限。
Start-Process powershell -Credential $credentials -ArgumentList '-noprofile -command &{Start-Process powershell -ArgumentList "-file "\\serveur\path\file.ps1" "}'
  • 2 : 当我设置本地管理员 (.\administrator) 时,一个新的 powershell Windows 以管理员权限启动,但只有一半的脚本 (file.ps1) 有效,如果我设置 domain\adminaccount : 同上。
Invoke-Item (Start-Process powershell.exe -Credential $credentials ((Split-Path $MyInvocation.InvocationName) + "\\serveur\path\file.ps1" ))
  • 3 等等

Start-Process powershell -ArgumentList '-executionpolicy, bypass, -file "\\serveur\path\file.ps1", -Credential $credentials, -verb RunAs'

Start-Process -filepath "\\serveur\path\file.ps1" -Credential $credentials -ArgumentList '-noprofile -noexit -command  -verb runas}'

Start-Process powershell -Credential $credentials -ArgumentList '-noprofile -command &{Start-Process powershell -ArgumentList "-file "\\serveur\path\file.ps1" "}'

但没有什么能按预期工作...... 如果你们有想法,那就太好了!!

--------------------- 编辑---------------- 我在 file.ps1 中犯了一个错误,所以

Invoke-Item (Start-Process powershell.exe -Credential $credentials ((Split-Path $MyInvocation.InvocationName) + "\\serveur\path\file.ps1" ))

本地管理员 (.\administrator) 可以正常工作,该脚本确实以管理员权限开始并按预期工作。 但是...它不适用于 domaine admin (domain\admin) :脚本会启动,但没有管理员权限...

【问题讨论】:

  • set-executionpolicy remotesigned 可能会有所帮助
  • 感谢您的回答,但我可以在哪里使用它?因为您需要管理员权限来修改执行策略,这就是重点,能够从没有管理员权限的帐户使用加密凭据并以管理员身份重新启动 powershell(我很确定我写的一些示例可能会重新启动 powershell使用我的管理员帐户但没有管理员权限...不确定我是否清楚我要做什么...:/
  • 如果您希望创建一个允许非管理员连接并执行管理员任务的情况,那么您可能需要查看contrained endpoints
  • Thx Boxdog,但我认为远程命令需要在远程计算机上启动 Netlogon 服务。可悲的是,这是我要解决的问题:这里是我的 file.ps1:Powershell Set-Service -name Netlogon -StartupType Automatic -Status Running Set-NetFirewallProfile -Profile Domain,Public -Enabled False
  • 哇哦!我刚刚再次阅读了我刚刚写的内容,我注意到我在防火墙部分犯了一个错误:Powershell Set-NetFirewallProfile -Profile Domain,Public -Enabled False 它应该是域和私有的,而不是公共的!这就是为什么我认为第 2 点成功了一半!好吧,它可以与本地管理员 (.\administrator) 一起使用...但是任何想法让它与域帐户管理员一起使用?

标签: powershell runas admin-rights


【解决方案1】:

你可以试试这个script 吗?

我能够如下调用它并获得提升的会话。

Invoke-Elevated -FilePath \\server\share\file.ps1

【讨论】:

  • 似乎很有前途,但我不确定如何使用它,因为它需要管理员权限才能工作,关键是我需要使用加密的 $credentials 来 RunAs "DomainAdmin" the file.ps1 : " .DESCRIPTION [...] 重要的是,该命令确实使用当前用户上下文。这意味着,当前用户需要本地系统的管理权限。如果未指定文件路径或脚本块,则当前运行的进程将是以管理员身份运行。”
  • @"Karthik SN" :如果这对你有帮助,这就是我在计算机上拥有管理员权限时重新启动 powershell 文件的方式(所以你只需要执行该文件,它将以管理员权限重新启动):Powershell if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){ Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
  • 是的,你是对的,我不考虑凭证是我的错。感谢分享脚本。
  • 没关系,感谢您的帮助;)如果有用,我很高兴。
【解决方案2】:

如果有人对我发现的解决方案感兴趣,可以在这里使用本地管理员帐户,那就是:

无法使其与我想在 UNC 路径上执行的 file.ps1 一起使用。 所以我不得不在执行脚本的本地计算机上第一次复制它。

$path="[...]\temp"
$source= "[...]file.ps1"
$destination = "c:\users\$env:USERNAME\documents"
if (!(Test-Path "$destination\Netlogon_Firewall.ps1")){Copy-Item -Path $source -Destination $destination}

然后我导入我的凭据:

$PassKey = get-content "$Path\Key.txt"
$Password = get-content "$Path\EncKey.txt" | Convertto-SecureString -Key $PassKey
$User = Read-Host "Enter the ID given by your administrator"
$credentials = New-Object System.Management.Automation.Pscredential `
-Argumentlist $User,$Password

最后我可以使用管理员权限启动 file.ps1 脚本:

Start-Process -Credential $Credentials "$PSHOME\powershell.exe" -WorkingDirectory "C:\Users\$env:USERNAME" -ArgumentList "-ExecutionPolicy Bypass & '$destination\file.ps1'"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2014-05-11
    • 2012-06-25
    相关资源
    最近更新 更多