Link to home
Start Free TrialLog in
Avatar of fireguy1125
fireguy1125

asked on

Combining Two Powershell Scripts Not Working

I have 2 Powershell scripts that I want to combine into one.  However when I do so, it doesn't appear to complete the script and hangs at the html conversion portion.  I'm assuming I can't just copy/paste the 2 scripts together.  The two work individually without issue, just for simplicity, i'd like to combine them and make it a scheduled task.  I'm a powershell novice, so I'd need the full script. Thanks in advance.

# Script to report Calendar Permissions for All mailbox and Email
# Get the mailboxes
$Mailboxes = Get-Mailbox -Filter {RecipientTypeDetails -eq "UserMailbox"} -ResultSize Unlimited
# An array for the output
$Output = @()
# Loop through the mailboxes
ForEach ($Mailbox in $Mailboxes) {
 # Get the name of the calendar folder
 $Calendar = (($Mailbox.PrimarySmtpAddress.ToString())+ ":\" + (Get-MailboxFolderStatistics -Identity $Mailbox.DistinguishedName -FolderScope Calendar | Select-Object -First 1).Name)
 # Get the permissions on the folder
 $Permissions = Get-MailboxFolderPermission -Identity $Calendar
 # Loop through the permissions, populating the output array
 ForEach ($Permission in $Permissions) {
  $Permission | Add-Member -MemberType NoteProperty -Name "Mailbox" -value $Mailbox.DisplayName
  $Output = $Output + $Permission
 }
}
# Write the output to a CSV file
$Output | Sort-Object Mailbox, User | Select-Object Mailbox, User, {$_.AccessRights}, IsValid | Export-Csv -Path "C:\scripts\CalPermissionsReport\$(Get-Date -Format yyyyMMdd)CalPermissions.csv" -NoTypeInformation
# 2nd Script Start HTML Conversion
$a = "<style>"
$a = $a + "BODY{background-color:White;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#0066FF}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:White}"
$a = $a + "</style>"
compare-object (import-csv "$(get-date -f yyyyMMdd)calpermissions.csv" | sort-object Mailbox, User) (import-csv "$("{0:yyyyMMdd}" -f (get-date).AddDays(-1))calpermissions.csv" | sort-object Mailbox, User) -passthru -property Mailbox,User,'$_.AccessRights',IsValid |
  % { $obj = $null } {
  if ($obj -and $obj.mailbox -eq $_.mailbox)
  {
    if ($_.SideIndicator -eq '<=') {
      $obj.NewUser         = $_.User
      $obj.NewAccessRights = $_.'$_.AccessRights'
      $obj.NewIsValid      = $_.IsValid
    } else {
      $obj.OldUser         = $_.User
      $obj.OldAccessRights = $_.'$_.AccessRights'
      $obj.OldIsValid      = $_.IsValid
    }
  } else {
    if ($obj) { Write-Output $obj }
    $obj = New-Object PSObject -Property @{
      Mailbox = $_.Mailbox
      NewUser         = $_.User              * ($_.SideIndicator -eq '<=')
      NewAccessRights = $_.'$_.AccessRights' * ($_.SideIndicator -eq '<=')
      NewIsValid      = $_.IsValid           * ($_.SideIndicator -eq '<=')
      OldUser         = $_.User              * ($_.SideIndicator -eq '=>')
      OldAccessRights = $_.'$_.AccessRights' * ($_.SideIndicator -eq '=>')
      OldIsValid      = $_.IsValid           * ($_.SideIndicator -eq '=>')
    }
  }
} {$obj} | select Mailbox, oldUser, OldAccessRights, OldIsValid, newUser, newAccessRights, newIsValid |
ConvertTo-HTML -head $a | 
Out-File .\"CalendarPermissionsReport.htm"
# use Send-MailMessage for sending the CSV file here.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fireguy1125
fireguy1125

ASKER

Perfect, works, thanks!