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

Powershell

Avatar of undefined
Last Comment
fireguy1125

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
SubSun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
fireguy1125

ASKER
Perfect, works, thanks!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck