Hallo,
ich soll für einen Kunden jeden Monatsanfang eine Auswertung der Postfachgrößen machen.
Ich muss dazu sagen, dass ich noch keinen Scheduled Task mit einem PS-Script verbunden habe, daher nicht 100% weiß, welche die beste Methode ist.
Ich habe im Scheduled Task testweise den Domain-Admin als Authentication verwendet, um sicherzugehen, dass es hier nicht an den Rechten scheitert, das Häkchen "Mit höchsten Berechtigungen ausführen" ist auch gesetzt.
Im Scheduled Task führe ich folgendes aus: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe mit den Argumenten: -ExecutionPolicy Bypass C:\Install\Powershell\MailboxSizeAuswertung\AuswertungTotalItemSize.ps1
Bevor ich die Funktion ConvertCSV-ToExcel eingebaut hatte, funktionierte es mittels Scheduled Task noch einwandfrei. Dann hat der Kunde halt eine .csv bekommen, was mir aber nicht so gefällt, da man hier keine Formatierung einbauen könnte.
Wenn ich das Script jedoch händisch ausführe, funktioniert alles so wie es sein soll.
Die Funktion ConvertToXLS habe ich von http://gallery.technet.microsoft.com/scriptcenter/7c56c444-2476-4625-b1d9-821f30280e44/ und habe es fast 1:1 gleich gelassen...
Bitte um Hilfe, da das Script an sich passen würde, jedoch der Scheduled Task hiermit nicht klar kommt, ich vermute wegen dem New-Object Excel.. Bitte um Info ob man das hier irgendwie anders lösen könnte.
# für Loggingzwecke wird hiermit das Startdatum festgehalten. $Startdate = Get-Date function LoadExchangeSnapin { if (! (Get-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction:SilentlyContinue) ) { Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 } } function Auswertung { $Mailboxes = Get-Mailbox -Filter {Alias -ne "mailboxsize" -and Alias -ne "Postmaster" -and Alias -ne "Administrator" -and Alias -ne "DiscoverySearchMailbox{D919BA05-46A6-415f-80AD-7E09334BB852}"} ` -Resultsize Unlimited"Name" + ";" + "Megabyte belegt" foreach ($Mailbox in $Mailboxes) { $MB = (Get-MailboxStatistics $Mailbox.SamAccountName).TotalItemSize.Value.ToMB() $User = (Get-MailboxStatistics $Mailbox.SamAccountName).Displayname $User + ";" + $MB + " MB" } } # Exchange Snapin laden, falls noch nicht geschehen. LoadExchangeSnapin # Variablen $date = (Get-Date).ToShortDateString() $monat = Get-Date -Format MMMM $csvPath = "C:\Install\Powershell\MailboxSizeAuswertung\Auswertung_$date.csv" $xlsxPath = "C:\Install\Powershell\MailboxSizeAuswertung\Auswertung_$date.xlsx" $pw = Get-Content C:\Install\Powershell\MailboxSizeAuswertung\MailPW.txt | ConvertTo-SecureString $cred = New-Object System.Management.Automation.PSCredential "mailboxsize", $pw $body = "<p style='font-family:calibri'>Sehr geehrte Dame, sehr geehrter Herr,</p><p style='font-family:calibri'>Hiermit übermitteln wir Ihnen den Report der Postfachgrößen aller Benutzer von Monat $monat.</p><p style='font-family:calibri'></p><p style='font-family:calibri'>MfG Ihr Systemadministrator</a></p>" # Function Auswertung starten und als csv speichern Auswertung | Out-File $csvPath -Encoding UTF8 Function Release-Ref ($ref) { ([System.Runtime.InteropServices.Marshal]::ReleaseComObject( [System.__ComObject]$ref) -gt 0) [System.GC]::Collect() [System.GC]::WaitForPendingFinalizers() } Function ConvertCSV-ToExcel { [CmdletBinding( SupportsShouldProcess = $True, ConfirmImpact = 'low', DefaultParameterSetName = 'file' )] Param ( [Parameter( ValueFromPipeline=$True, Position=0, Mandatory=$True, HelpMessage="Name of CSV/s to import")] [ValidateNotNullOrEmpty()] [array]$inputfile, [Parameter( ValueFromPipeline=$False, Position=1, Mandatory=$True, HelpMessage="Name of excel file output")] [ValidateNotNullOrEmpty()] [string]$output ) Begin { #Configure regular expression to match full path of each file [regex]$regex = "^\w\:\\" #Find the number of CSVs being imported $count = ($inputfile.count -1) #Create Excel Com Object $excel = new-object -com excel.application #Disable alerts $excel.DisplayAlerts = $False #Show Excel application $excel.Visible = $False #Add workbook $workbook = $excel.workbooks.Add() #Remove other worksheets $workbook.worksheets.Item(2).delete() #After the first worksheet is removed,the next one takes its place $workbook.worksheets.Item(2).delete() #Define initial worksheet number $i = 1 } Process { ForEach ($input in $inputfile) { #If more than one file, create another worksheet for each file If ($i -gt 1) { $workbook.worksheets.Add() | Out-Null } #Use the first worksheet in the workbook (also the newest created worksheet is always 1) $worksheet = $workbook.worksheets.Item(1) #Add name of CSV as worksheet name $worksheet.name = "$((GCI $input).basename)" #Open the CSV file in Excel, must be converted into complete path if no already done If ($regex.ismatch($input)) { $tempcsv = $excel.Workbooks.Open($input) } ElseIf ($regex.ismatch("$($input.fullname)")) { $tempcsv = $excel.Workbooks.Open("$($input.fullname)") } Else { $tempcsv = $excel.Workbooks.Open("$input") } $tempsheet = $tempcsv.Worksheets.Item(1) #Copy contents of the CSV file $tempSheet.UsedRange.Copy() | Out-Null #Paste contents of CSV into existing workbook $worksheet.Paste() #Close temp workbook $tempcsv.close() #Select all used cells $range = $worksheet.UsedRange #Autofit the columns $range.EntireColumn.Autofit() | out-null $i++ } } End { #Save spreadsheet $workbook.saveas("$output") Write-Host -Fore Green "File saved to $output" #Close Excel $excel.quit() #Release processes for Excel $a = Release-Ref($range) } } ConvertCSV-ToExcel -Inputfile $csvPath -Output $xlsxPath # Mail generieren und an die ensprechenden Empfänger senden Send-MailMessage -Credential $cred -Encoding ([System.Text.Encoding]::UTF8) ` -Subject "Postfachgrößen der Benutzer - Monat $monat" ` -Body $body -BodyAsHtml ` -From "mailboxsize@domain.tld" -To "mail1@domain.tld","mail2@domain.tld","mail3@domain.tld" ` -Attachments $xlsxPath -SmtpServer "server" # nicht mehr benötigte Files löschen Get-Item -Path C:\install\powershell\mailboxsizeauswertung\*.csv | Remove-Item Get-Item -Path C:\install\powershell\mailboxsizeauswertung\*.xlsx | Remove-Item # nach dem Script wird hiermit das Enddatum festgehalten um es dann zu subtrahieren. $Enddate = Get-Date # Unterschied berechnen und in Logfile schreiben "Durchlauf des Scripts am " + $date + " hat folgende Zeit in Anspruch genommen: " + ($Enddate - $Startdate).ToString() | Add-Content C:\Install\Powershell\MailboxSizeAuswertung\Logfile.txt