Combining Two Powershell Scripts Not Working

Posted on 2013-12-24
Last Modified: 2013-12-24
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

Question by:fireguy1125
LVL 40

Accepted Solution

Subsun earned 500 total points
ID: 39738156
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


Author Closing Comment

ID: 39738318
Perfect, works, thanks!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now