Script: Hide-InternetNewsgroups.ps1 – Removing “Internet Newsgroups” in Exchange Server 2007 – Part II – Automate It!
In an earlier post, Removing “Internet Newsgroups” in Exchange Server 2007, I showed you how to use Exchange Management Shell to hide the Internet Newsgroups public folder.
If you’re a consultant like me, you might find a need to hide the Internet Newsgroups during each and every project. So I came up with this PowerShell script to just do the job all automagically. The script enumerates the rights, and then removes each one, resulting in the default user account having no rights. Let me show you how it’s done.
As mentioned, we enumerate the rights that ‘default’ has. We do this by creating an array:
$perm = Get-PublicFolderClientPermission "\Internet Newsgroups" -user default
$perm now holds the results for the query. For Public Folder client permissions, you can’t remove ‘DeleteOwnedItems’ if the user also has the ‘DeleteAllItems’ right. The same applies with the ‘EditAllItems’ right if the user has ‘EditOwnedItems’ right. So we’ll check for those and remove those first.
if ($perm.AccessRights -contains "DeleteAllItems") {
Remove-PublicFolderClientPermission "\Internet Newsgroups" -user default -accessrights DeleteAllItems -confirm:$false
}
if ($perm.AccessRights -contains "EditAllItems") {
Remove-PublicFolderClientPermission "\Internet Newsgroups" -user default -accessrights EditAllItems -confirm:$false
}
Now that those are done, we can use a ForEach loop and cycle through the rest and remove each. First, we get the rights again, since we may have removed some above, then remove what’s left:
$perm=Get-PublicFolderClientPermission "\Internet Newsgroups" -user default
ForEach ($right in $perm.AccessRights){
Remove-PublicFolderClientPermission "\Internet Newsgroups" -user default -accessrights $right -confirm:$false
}
This results in default having ‘none’ for rights, as seen below. We verify this by looking at the rights again:
Get-PublicFolderClientPermission "\Internet Newsgroups" -user default | ft user,accessrights -auto
And that’s it. The full script looks like this:
# Hide-InternetNewsgroups.ps1
# http://www.ehloworld.com/123
$perm = Get-PublicFolderClientPermission "\Internet Newsgroups" -user default
# first, delete the rights that must be deleted before others (to avoid an error)
if ($perm.AccessRights -contains "DeleteAllItems") {
Remove-PublicFolderClientPermission "\Internet Newsgroups" -user default -accessrights DeleteAllItems -confirm:$false
}
if ($perm.AccessRights -contains "EditAllItems") {
Remove-PublicFolderClientPermission "\Internet Newsgroups" -user default -accessrights EditAllItems -confirm:$false
}
# now do the rest
$perm=Get-PublicFolderClientPermission "\Internet Newsgroups" -user default
ForEach ($right in $perm.AccessRights){
Remove-PublicFolderClientPermission "\Internet Newsgroups" -user default -accessrights $right -confirm:$false
}
Get-PublicFolderClientPermission "\Internet Newsgroups" -user default | ft user,accessrights -auto
Installation
Execution Policy: Third-party PowerShell scripts may require that the PowerShell Execution Policy be set to either AllSigned, RemoteSigned, or Unrestricted. The default is Restricted, which prevents scripts – even code signed scripts – from running. For more information about setting your Execution Policy, see Using the Set-ExecutionPolicy Cmdlet.





Follow Me