Powershell核心到powershell

问题描述 投票:0回答:1

我正在运行安装了Pwsh的Ubuntu EC2实例,以在我们的其中一台服务器上远程执行AD命令。 2SD Hop设置正确,我能够运行AD命令,但是在执行我的脚本时出现以下错误(脚本在2SD Hop机器上可以正常工作):

无法识别搜索过滤器+ CategoryInfo:未指定:(:) [Get-ADUser],ADException + FullyQualifiedErrorId:ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser+ PSComputerName:corpmaint02

#!/usr/bin/pwsh
$employeeEmail = '[email protected]'
$session = New-PSSession -ComputerName corpmaint02 -ConfigurationName corpmaint02 -Credential contoso\myadminaccount
Invoke-Command -Session $session -ArgumentList $employeeEmail -ScriptBlock{
Get-ADUser -Filter "EmailAddress -eq '$employeeEmail'" -Properties EmailAddress | Disable-ADAccount
Write-Host $employeeEmail has been 'disabled.'
}
Remove-PSSession -ID $session.ID
[GC]::Collect()

任何帮助将不胜感激。

更新:新代码:

#!/usr/bin/pwsh
$cred=Get-Credential domain\myadmin
$employeeEmail = '[email protected]'
Invoke-Command -ComputerName corpmaint02 -Credential $cred -ConfigurationName corpmaint02 -Authentication Negotiate  -ArgumentList $employeeEmail -$
Get-ADUser -Filter "EmailAddress -eq '$($Args[0])'" -Properties EmailAddress | Disable-ADAccount -verbose
Write-Host $employeeEmail has been 'disabled.'
}
I modified my code as follow and it works expect for the lack of permissions to disable the account which odd because my admin account has rights to do so. 

访问权限不足,无法执行操作+ CategoryInfo:未指定:(CN = xxxxx \ domain,DC = com:ADUser)[Disable-ADAccount],ADException+ FullyQualifiedErrorId:ActiveDirectoryServer:8344,Microsoft.ActiveDirectory.Management.Commands.DisableADAccount+ PSComputerName:corpmaint02

powershell powershell-remoting powershell-core
1个回答
0
投票

Invoke-Command并未以提升的权限运行,因此您可以检索数据但不能进行更改。

https://ss64.com/ps/syntax-elevate.html如果使用Invoke-Command在远程计算机上运行脚本或命令,则即使本地会话处于运行状态,它也不会以提升权限运行。这是因为在非交互式会话中,任何提升权限的提示都会在远程计算机上发生,因此将失败。

您可以在Invoke-Command脚本块中尝试自我提升(从上面的链接)

If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
  # Relaunch as an elevated process:
  Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs
  exit
}
# Now running elevated so launch the script:
& "d:\long path name\script name.ps1" "Long Argument 1" "Long Argument 2"
© www.soinside.com 2019 - 2024. All rights reserved.