用于从用户中删除组的Powershell脚本

时间:2014-12-12 16:09:18

标签: powershell office365

我发现这个脚本可以从单个用户中删除组,但我害怕在我的环境中尝试它: 问题 - 是否有在线测试实验室,我可以运行这些脚本而不会破坏我自己的环境?如果我想从用户中删除组,那么拥有更多脚本/ powershell知识的人可以验证这是否可以安全运行?我被指示在它之后运行带有参数的脚本,即c:/sripts/removegroups.ps1 username @ domain“是正确的吗?

$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
}
}
}

4 个答案:

答案 0 :(得分:1)

您可以在Remove-DistributionGroupMember命令上使用 whatif 开关

$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
}
}
}

WhatIf开关指示命令模拟对对象执行的操作。通过使用WhatIf开关,您可以查看将发生的更改,而无需应用任何这些更改。您不必使用WhatIf开关指定值。

http://technet.microsoft.com/en-us/library/aa998016(v=exchg.150).aspx

答案 1 :(得分:0)

$WhatIfPreference设置为$true,脚本中的所有命令都应显示在真实运行时会发生什么。这比稍微修改脚本命令(稍微)容易一些。

答案 2 :(得分:0)

看起来像这样吗?

$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
}
}
}

答案 3 :(得分:0)

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