简体   繁体   中英

Manipulating AD groups with powershell - prompt for yes or no for each object in selection?

Here's what I have so far:

$UserFrom = Read-Host -Prompt 'Enter username of person to pull group memberships from'
$UserTo = Read-Host -Prompt 'Enter username of person to apply these groups to'
$Groups = Get-ADPrincipalGroupMembership $UserFrom | Where-Object {$_.name -ne "Domain Users"}
Write-Host 'Copying over the groups...'
Add-ADPrincipalGroupMembership $UserTo -MemberOf $Groups
Write-Host 'Completed!'

This works perfectly, what I'd like to do is instead of copying all the groups over, receive a prompt for each one. Do you want to copy this one? (Y/N). Then the next, and the next, until it has gone through them all.

If anyone can help me with this I would be very grateful. If it is giving me the code I need that is amazing but even if you can just tell what I need to read about that would be very helpful. I am a beginner.

Thank you

How about adding a confirm requirement like so:

Add-ADPrincipalGroupMembership $UserTo -MemberOf $Groups -Confirm:$true

For confirmation for each group, you can do a loop:

Foreach ($Group in $Groups) {
  Add-ADPrincipalGroupMembership $UserTo -MemberOf $Group -Confirm:$true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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