Home > Exchange Server > Script: Hide-InternetNewsgroups.ps1 – Removing “Internet Newsgroups” in Exchange Server 2007 – Part II – Automate It!

Script: Hide-InternetNewsgroups.ps1 – Removing “Internet Newsgroups” in Exchange Server 2007 – Part II – Automate It!

Description

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 | Format-Table User,AccessRights -AutoWidth

And that’s it. The full script looks like this:

# Hide-InternetNewsgroups.ps1
# https://www.ucunleashed.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 | Format-Table User,AccessRights -AutoWidth

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.

Donations

I’ve never been one to really solicit donations for my work. My offerings are created because *I* need to solve a problem, and once I do, it makes sense to offer the results of my work to the public. I mean, let’s face it: I can’t be the only one with that particular issue, right? Quite often, to my surprise, I’m asked why I don’t have a “donate” button so people can donate a few bucks. I’ve never really put much thought into it. But those inquiries are coming more often now, so I’m yielding to them. If you’d like to donate, you can send a few bucks via PayPal at https://www.paypal.me/PatRichard. Money collected from that will go to the costs of my website (hosting and domain names), as well as to my home lab.

Downloads

Hide-InternetNewsgroups.zip

  1. No comments yet.
  1. No trackbacks yet.