Тестовый пользователь существует с полем ввода Powershell - PullRequest
0 голосов
/ 07 сентября 2018

Я пытаюсь написать небольшое приложение, которое позволило бы скопировать группы безопасности пользователя AD и вставить их в профиль другого.

Мне хорошо с этой частью, но я хочу немного усложнить ее, добавив некоторые поля ввода, которые будут искать пользователя AD, с ошибками в случае, если он не выйдет из моей AD, и не будет выводить запрос еще раз, пока пользователь был найден.

ipmo activedirectory

Add-type -assemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms

$userref = [Microsoft.VisualBasic.Interaction]::Inputbox("Enter username 
", "Prime User")
$usertar = [Microsoft.VisualBasic.Interaction]::Inputbox("Enter username", 
"Target")

$userref, $usertar | foreach {

if ([bool](Get-ADUser -Filter {samaccountname -eq $_})  -ne $true) {

[System.Windows.Forms.MessageBox]::Show("This user does not exist!")
}

else {Write-Host "User Ok"}

}

1 Ответ

0 голосов
/ 07 сентября 2018

Так как вам нужно проверить существование двух пользователей AD, прежде чем можно будет запустить остальную часть вашего кода, вы в основном дважды запрашиваете одно и то же, используя поля ввода. В этом случае я бы предложил добавить небольшую пользовательскую функцию для этого.

Что-то вроде этого возможно:

Import-Module ActiveDirectory

Add-type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms

function Get-UserFromInputbox ([string]$Title) {
    do {
        $account = [Microsoft.VisualBasic.Interaction]::Inputbox("Enter user accountname", $Title)
        # On Cancel the InputBox function simply returns an empty string.
        # in that case, just return $null so the calling code can handle it
        if ([string]::IsNullOrEmpty($account)) { return $null }

        # Check if the user can be found
        $user = Get-ADUser -Filter "SamAccountName -eq '$account'" –Properties MemberOf -ErrorAction SilentlyContinue
        if (!$user) {
            # If not found, show the same InputBox again until a valid 
            # accountname was given or the dialog is cancelled.
            [System.Windows.Forms.MessageBox]::Show("User '$account' does not exist!")
        }
    }
    while (!$user)

    return $user
}

# Get the AD User object for the source user
$userref = Get-UserFromInputbox -Title "Source User"
if (!$userref) { exit }

# Ditto for the target user
$usertar = Get-UserFromInputbox -Title "Target User"
if (!$usertar) { exit }

# From here on you should have two valid AD user objects with the default properties `DistinguishedName, Enabled,
# GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName`.
# In the function we extended that to also have the `MemberOf` property.

Надеюсь, это поможет

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...