首页 > 解决方案 > 如何在具有其他伴奏(和管理员权限)的 ps1 中启动其他 ps1?

问题描述

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

所以这就是我已经做的:

    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

而那个不工作的(我在这里尝试了很多东西,一些例子):

Start-Process powershell -Credential $credentials -ArgumentList '-noprofile -command &{Start-Process powershell -ArgumentList "-file "\\serveur\path\file.ps1" "}'
Invoke-Item (Start-Process powershell.exe -Credential $credentials ((Split-Path $MyInvocation.InvocationName) + "\\serveur\path\file.ps1" ))

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) :脚本确实启动了,但没有管理员权限...

标签: powershellrunasadmin-rights

解决方案


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

无法使其与我想在 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'"

推荐阅读