Scheduled a task using the below script but the task is running but not generating the file.
When i run it normally it is working perfectly. could anyone please help? or guide me how to split this to two part. like one script is to generate the file and the other one is to send the file.
i need to run this script on last Saturday of every month
$report = @() $file = "Path\MbxReport _$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv" $Mbxs = Get-Mailbox -ResultSize unlimited | Select-Object -First 5 foreach ($Mbx in $Mbxs) { $TotalItemSize = (Get-MailboxStatistics $Mbx).TotalItemSize $ItemCount = (Get-MailboxStatistics $Mbx).ItemCount $email= get-mailbox $mbx | select @{Name='EmailAddresses'; Expression={$_.EmailAddresses -join ","}} $reportObj = New-Object PSObject $reportObj | Add-Member NoteProperty -Name "Name" -Value $mbx.Name $reportObj | Add-Member NoteProperty -Name "DisplayName" -Value $mbx.DisplayName $reportObj | Add-Member NoteProperty -Name "Alias" -Value $mbx.Alias $reportObj | Add-Member NoteProperty -Name "RecipientTypeDetails" -Value $mbx.RecipientTypeDetails $reportObj | Add-Member NoteProperty -Name "SamAccountName" -Value $mbx.SamAccountName $reportObj | Add-Member NoteProperty -Name "PrimarySmtpAddress" -Value $mbx.PrimarySmtpAddress $reportObj | Add-Member NoteProperty -Name "EmailAddresses" -Value $email $reportObj | Add-Member NoteProperty -Name "Location" -Value $mbx.Office $reportObj | Add-Member NoteProperty -Name "GWC Journal Exclusion" -Value $mbx.CustomAttribute12 $reportObj | Add-Member NoteProperty -Name "MBX Retention Policy " -Value $mbx.RetentionPolicy $reportObj | Add-Member NoteProperty -Name "Database" -Value $mbx.Database $reportObj | Add-Member NoteProperty -Name "TotalItemSize" -Value $TotalItemSize $reportObj | Add-Member NoteProperty -Name "ItemCount" -Value $ItemCount $reportObj | Add-Member NoteProperty -Name "DistinguishedName" -Value $mbx.DistinguishedName $reportObj | Add-Member NoteProperty -Name "LegacyExchangeDN" -Value $mbx.LegacyExchangeDN $reportObj | Add-Member NoteProperty -Name "WhenMailboxCreated" -Value $mbx.WhenMailboxCreated $reportObj | Add-Member NoteProperty -Name "OrganizationalUnit" -Value $mbx.OrganizationalUnit $report += $reportObj } $report | Export-Csv -path $file -NoTypeInformation $SendMail = @{ 'SmtpServer' = "smtp-*****" 'To' = "*****" 'From' = "MonthlyMBXReport@iconplc.com" 'Subject' = "Monthly Report" 'Attachments' = $file } Send-MailMessage @SendMail