简体   繁体   English

Powershell 内核到 powershell

[英]Powershell core to powershell

I'm running an Ubuntu EC2 instance with Pwsh installed to remote execute AD commands on one of our servers.我正在运行一个安装了 Pwsh 的 Ubuntu EC2 实例,以在我们的一台服务器上远程执行 AD 命令。 2sd hop is set-up correctly and i'm able to run AD commands but when executing my script i get the following error (Scripts works fine directly on the 2sd hop machine): 2sd hop 设置正确,我能够运行 AD 命令,但是在执行我的脚本时出现以下错误(脚本直接在 2sd hop 机器上正常工作):

The search filter cannot be recognized无法识别搜索过滤器
+ CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser + PSComputerName : corpmaint02 + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException +fullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser + PSComputerName : corpmaint02

#!/usr/bin/pwsh
$employeeEmail = 'myemail@contoso.com'
$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()

Any help would be appreciated.任何帮助,将不胜感激。

Update: new code:更新:新代码:

#!/usr/bin/pwsh
$cred=Get-Credential domain\myadmin
$employeeEmail = 'myemail@contoso.com'
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. 

Insufficient access rights to perform the operation没有足够的访问权限来执行操作
+ CategoryInfo : NotSpecified: (CN=xxxxx\\domain,DC=com:ADUser) [Disable-ADAccount], ADException + CategoryInfo : NotSpecified: (CN=xxxxx\\domain,DC=com:ADUser) [Disable-ADAccount], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8344,Microsoft.ActiveDirectory.Management.Commands.DisableADAccount + PSComputerName : corpmaint02 + FullQualifiedErrorId : ActiveDirectoryServer:8344,Microsoft.ActiveDirectory.Management.Commands.DisableADAccount + PSComputerName: corpmaint02

New code to elevate:要提升的新代码:

#!/usr/bin/pwsh
$cred=Get-Credential domain\myadmin
$employeeEmail = 'user1@contoso.com' 
Invoke-Command -ComputerName corpmaint02 -Credential $cred -ConfigurationName corpmaint02 -Authentication Negotiate -ArgumentList $employeeEmail,$cred -ScriptBlock{
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
    if ($testadmin -eq $false) {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    exit $LASTEXITCODE
}
Get-ADUser -Filter "EmailAddress -eq '$($Args[0])'" -Properties EmailAddress | Disable-ADAccount -verbose -Credential $Args[1]
}
Write-Host $employeeEmail 'has been disabled.'

Invoke-Command isn't running with elevated rights, so you can retrieve data but not make changes. Invoke-Command 未以提升的权限运行,因此您可以检索数据但不能进行更改。

https://ss64.com/ps/syntax-elevate.html If you use Invoke-Command to run a script or command on a remote computer, then it will not run elevated even if the local session is. https://ss64.com/ps/syntax-elevate.html如果您使用 Invoke-Command 在远程计算机上运行脚本或命令,那么即使本地会话是,它也不会运行提升。 This is because any prompt for elevation will happen on the remote machine in a non-interactive session and so will fail.这是因为任何提升提示都将在非交互式会话中的远程计算机上发生,因此会失败。

You can try self elevating in the Invoke-Command scriptblock (from the link above)您可以尝试在 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM