Reputation: 3
I found this script to remove groups from a single user but i'm afraid to try it in my environment: Question - is there a test lab online where i can run these scripts without breaking my own environment? Also can someone with more scripting/powershell knowledge verify that this is safe to run if i want to remove groups from a user? I was instructed to run the script with an argument after it i.e. c:/sripts/removegroups.ps1 username@domain" is that correct?
$user = $args[0] if (!$args[0]) {
} $mailbox=get-mailbox $user
$dgs= Get-DistributionGroup
foreach($dg in $dgs){
$DGMs = Get-DistributionGroupMember -identity $dg.Identity
foreach ($dgm in $DGMs){
if ($dgm.name -eq $mailbox.name){
write-host 'User Found In Group' $dg.identity
Remove-DistributionGroupMember $dg.Name -Member $user
}
}
}
Upvotes: 0
Views: 885
Reputation: 3
PS C:\Windows\system32> $WhatIfPreference = $true
PS C:\Windows\system32> $user = $args[0] if (!$args[0]) {
At line:1 char:18
+ $user = $args[0] if (!$args[0]) {
+ ~~
Unexpected token 'if' in expression or statement.
At line:1 char:33
+ $user = $args[0] if (!$args[0]) {
+ ~
Missing closing '}' in statement block.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx
ception
+ FullyQualifiedErrorId : UnexpectedToken
PS C:\Windows\system32>
PS C:\Windows\system32> } $mailbox=get-mailbox $user
At line:1 char:1
+ } $mailbox=get-mailbox $user
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx
ception
+ FullyQualifiedErrorId : UnexpectedToken
PS C:\Windows\system32>
PS C:\Windows\system32> $dgs= Get-DistributionGroup
WARNING: By default, only the first 1000 items are returned. Use the ResultSize
parameter to specify the number of items returned. To return all items,
specify "-ResultSize Unlimited". Be aware that, depending on the actual number
of items, returning all items can take a long time and consume a large amount
of memory. Also, we don't recommend storing the results in a variable. Instead,
pipe the results to another task or script to perform batch changes.
PS C:\Windows\system32>
PS C:\Windows\system32> foreach($dg in $dgs){
>>enter code here
>> $DGMs = Get-DistributionGroupMember -identity $dg.Identity
>> foreach ($dgm in $DGMs){
>> if ($dgm.name -eq $mailbox.name){
>>
>> write-host 'User Found In Group' $dg.identity
>> Remove-DistributionGroupMember $dg.Name -Member $user
Upvotes: 0
Reputation: 3
Would it look something like this?
$WhatIfPreference = $true
$user = $args[0] if (!$args[0]) {
} $mailbox=get-mailbox $user
$dgs= Get-DistributionGroup
foreach($dg in $dgs){
$DGMs = Get-DistributionGroupMember -identity $dg.Identity
foreach ($dgm in $DGMs){
if ($dgm.name -eq $mailbox.name){
write-host 'User Found In Group' $dg.identity
Remove-DistributionGroupMember $dg.Name -Member $user
}
}
}
Upvotes: 0
Reputation: 2244
Set $WhatIfPreference
to $true
, and all the commands in the script should display what would have happened had they been run for real. This is (slightly) easier than modifying the script commands one by one.
Upvotes: 0
Reputation: 181
You could use the whatif switch on the Remove-DistributionGroupMember command
$user = $args[0] if (!$args[0]) {
} $mailbox=get-mailbox $user
$dgs= Get-DistributionGroup
foreach($dg in $dgs){
$DGMs = Get-DistributionGroupMember -identity $dg.Identity
foreach ($dgm in $DGMs){
if ($dgm.name -eq $mailbox.name){
write-host 'User Found In Group' $dg.identity
Remove-DistributionGroupMember $dg.Name -Member $user -Whatif
}
}
}
The WhatIf switch instructs the command to simulate the actions that it would take on the object. By using the WhatIf switch, you can view what changes would occur without having to apply any of those changes. You don't have to specify a value with the WhatIf switch.
http://technet.microsoft.com/en-us/library/aa998016(v=exchg.150).aspx
Upvotes: 1