none
PowerShell Script review tjek,remove, add MailboxFolderPermission RRS feed

  • Question

  • Hello

    I could use an extra eye on the following script.
    The idea is to set the User "ALLE"  on everyone's calender with rights "LimitedDetails"

    problem:
    users are only using one Calendar and can have the name "calender" or "Kalender".

    Feedback is Welcome (note the -whatif)

    #Description: Tjek if MailboxFolderPermission is set for users calender or kalender
    
    #Get all MailboxFolderPermission alias's
    $AllMailboxArray = @( Get-Mailbox -Filter {IsMailboxEnabled -eq $True -and IsResource -ne $True -and RecipientTypeDetails -eq 'UserMailbox' } | Select -ExpandProperty Alias )
    
    #foreach n Array --> find out what name calender  object has
    	foreach ($Mailbox in $AllMailboxArray)	{
    		$AllCalKalVarGet = Get-MailboxFolderStatistics $Mailbox -FolderScope calendar | Select -ExpandProperty Name
    			foreach ($CalKalVarGet in $AllCalKalVarGet)
        {
    
    #Tjek if  $CalKalVarGet contains "Calendar"	       
    	   if ($CalKalVarGet -eq "Calendar")	{
                     
    #Tjek if ALLE is Already present. IF  then remove for later add.
    	$array =   @(Get-MailboxFolderPermission -identity "$($MailBox):\calendar" |Select -ExpandProperty User)
    
    if ($array -match 'ALLE')	{
              Write-host " User ALLE is found under $($MailBox):\calendar ,Removal commencing and later added again"
    #Remove rights
    Remove-MailboxFolderPermission -identity "$($MailBox):\calendar" -User ALLE -confirm:$false -whatif
    
    							}
    
    								else{
    											Write-host "All ok ALLE can be set"
    										}
    										
    										
    
    #set MailboxFolderPermission
              Add-MailboxFolderPermission -identity "$($MailBox):\calendar" -AccessRights LimitedDetails -User ALLE -whatif
             
    											}
    											
    											
    #Tjek if  $CalKalVarGet contains "Kalender"	 
    		  	  		  
    		  ElseIf ($CalKalVarGet -eq "Kalender")  {
                        					 
    #Tjek if ALLE is Allready pressen. IF  then remove for later add.
    $array =   @(Get-MailboxFolderPermission -identity "$($MailBox):\Kalender" |Select -ExpandProperty User)
    
    if ($array -match 'ALLE')	{
                   Write-host " User ALLE is found under $($MailBox):\Kalender ,Removal commencing and later added again"
    #Fjern Rettigheder
    Remove-MailboxFolderPermission -identity "$($MailBox):\Kalender" -User ALLE -confirm:$false -whatif
    							}
    
    								else {
    										Write-host "All ok ALLE can be set"
    										}
    
    
    #set MailboxFolderPermission
              Add-MailboxFolderPermission -identity "$($MailBox):\Kalender" -AccessRights LimitedDetails -User ALLE -whatif
             
    												}		   
                        else {
    #Throwing the calendar names that are NOT "Calender" or "Kalender" in a text file, the file must exist.
    							Add-Content c:\scripts\test.txt "The name of Calender on mailbox $MailBox is: $CalKalVarGet"
    						}
         }
    											}

    "Carpe Diem" does not mean "fish of the day."

    Thursday, January 9, 2014 7:37 AM