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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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