• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • Last Modified:

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

1 Solution
Does adding path to input files (line 27) make any difference?
compare-object (import-csv "C:\scripts\CalPermissionsReport\$(get-date -f yyyyMMdd)calpermissions.csv" | sort-object Mailbox, User) (import-csv "C:\scripts\CalPermissionsReport\$("{0:yyyyMMdd}" -f (get-date).AddDays(-1))calpermissions.csv" | sort-object Mailbox, User) -passthru -property Mailbox,User,'$_.AccessRights',IsValid |

Open in new window

and in line 54
Out-File "C:\scripts\CalPermissionsReport\CalendarPermissionsReport.htm"

Open in new window

fireguy1125Author Commented:
Perfect, works, thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now