Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

Frage zum Script...Datenbanken auslesen

$
0
0

Nachdem ich nun mehrere Stunden den Fehler gesucht habe, versuche ich es nun auf diesem Wege....warum wird mir in der .txt kein Speicherplatz angezeigt?

param(
	[Parameter(ParameterSetName='database')] [string]$database,
	[Parameter(ParameterSetName='file')] [string]$file,
	[Parameter(ParameterSetName='server')] [string]$server,
	[Parameter(ParameterSetName='mailbox')] [string]$mailbox,
	[Parameter(ParameterSetName='all')] [switch]$all,
	[string]$filename
)

#...................................
# Variables
#...................................

$ErrorActionPreference = "SilentlyContinue"
$WarningPreference = "SilentlyContinue"
$report = @()


#...................................
# Initialize
#...................................

#Set recipient scope
$2007snapin = Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.Admin
if ($2007snapin)
{
	$AdminSessionADSettings.ViewEntireForest = 1
}
else
{
	$2010snapin = Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010
	if ($2010snapin)
	{
		Set-ADServerSettings -ViewEntireForest $true

	}
}

#...................................
# Script
#...................................

#Add dependencies
Import-Module ActiveDirectory

#Get the mailbox list

		Set-ADServerSettings -PreferredServer server.xy.dns
		Set-ADServerSettings -RecipientViewRoot xy.dns

Write-Host -ForegroundColor White "Collecting mailbox list"

if($all) { $mailboxes = @(Get-Mailbox -resultsize unlimited -IgnoreDefaultScope) }

if($server) { $mailboxes = @(Get-Mailbox -server $server -resultsize unlimited -IgnoreDefaultScope) }

if($database){ $mailboxes = @(Get-Mailbox -database $database -resultsize unlimited -IgnoreDefaultScope) }

if($file) {	$mailboxes = @(Get-Content $file | Get-Mailbox -resultsize unlimited) }

if($mailbox) { $mailboxes = @(Get-Mailbox $mailbox) }

#Get the report

Write-Host -ForegroundColor White "Collecting report data"

$mailboxcount = $mailboxes.count
$i = 0

$mailboxdatabases = @(Get-MailboxDatabase)

#Loop through mailbox list and collect the mailbox statistics
foreach ($mb in $mailboxes)
{
	$i = $i + 1
	$pct = $i/$mailboxcount * 100
	Write-Progress -Activity "Collecting mailbox details" -Status "Processing mailbox $i of $mailboxcount - $mb" -PercentComplete $pct

	$stats = $mb | Get-MailboxStatistics | Select-Object TotalItemSize,TotalDeletedItemSize,ItemCount,LastLogonTime,LastLoggedOnUserAccount

    if ($mb.ArchiveDatabase)
    {
        $archivestats = $mb | Get-MailboxStatistics -Archive | Select-Object TotalItemSize,TotalDeletedItemSize,ItemCount
    }
    else
    {
        $archivestats = "n/a"
    }

    $inboxstats = Get-MailboxFolderStatistics $mb -FolderScope Inbox | Where {$_.FolderPath -eq "/Inbox"}
    $sentitemsstats = Get-MailboxFolderStatistics $mb -FolderScope SentItems | Where {$_.FolderPath -eq "/Sent Items"}
    $deleteditemsstats = Get-MailboxFolderStatistics $mb -FolderScope DeletedItems | Where {$_.FolderPath -eq "/Deleted Items"}
    #FolderandSubFolderSize.ToMB()

	$lastlogon = $stats.LastLogonTime

	$user = Get-User $mb
	$aduser = Get-ADUser $mb.samaccountname -Properties Enabled,AccountExpirationDate

	#Create a custom PS object to aggregate the data we're interested in

	$userObj = New-Object PSObject
	$userObj | Add-Member NoteProperty -Name "Company" -Value $user.Company
    $userObj | Add-Member NoteProperty -Name "Office" -Value $user.Office
	$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $mb.DisplayName
    $userObj | Add-Member NoteProperty -Name "Total Mailbox Size (MB)" -Value ($stats.TotalItemSize.Value.ToMB() + $stats.TotalDeletedItemSize.Value.ToMB())
    #$userObj | Add-Member NoteProperty -Name "Total Mailbox Size (GB)" -Value ($stats.TotalItemSize.Value.ToGB() + $stats.TotalDeletedItemSize.Value.ToGB())

	#Add the object to the report
	$report = $report += $userObj
}

#Catch zero item results
$reportcount = $report.count
$billdate = Get-Date -UFormat %Y%m%d

if ($reportcount -eq 0)
{
	Write-Host -ForegroundColor Yellow "No mailboxes were found matching that criteria."
}
else
{
	#Output single mailbox report to console, otherwise output to CSV file
	if ($mailbox)
	{
		$report | Format-List
	}
	else
	{
		$report | Out-File -Encoding utf8 Z:\$billdate\Exchange\$filename.txt
		#$report | Out-File -Encoding utf8 C:\Users\dmontag\Desktop\$filename.txt
		Write-Host -ForegroundColor White "Report written to $filename .txt in current path."
		Get-Item $filename
	}
}


#####################################################################################################################################################

Set-ExecutionPolicy Unrestricted

$billdate = Get-Date -UFormat %Y%m%d
$path="Z:\$billdate\Exchange\Exchange.xlsx"

Z:\Scripte\Exchange\exchange.ps1

#Instanzieren eines COM-Objektes für Excel
$excel = new-object -comobject excel.application
$excel.visible = $False
$excel.DisplayAlerts = $False

#Arbeitsmappe laden
$workbook = $excel.Workbooks.Add()

foreach ($companys in (Get-Content Z:\Scripte\Exchange\companys.txt))
{
    # Ignore blank lines
    if ($companys) {
		$worksheet = $excel.Worksheets.Add()
		$companys = $companys.Trim()
        $workbook.Worksheets.Item(1).Name = "$companys"
		$workbook.Worksheets.Item(1).Cells.Item(1,1) = "Company"
		$workbook.Worksheets.Item(1).Cells.Item(1,2) = "Office"
		$workbook.Worksheets.Item(1).Cells.Item(1,3) = "Username"
		$workbook.Worksheets.Item(1).Cells.Item(1,4) = "Size in MB"
		$workbook.Worksheets.Item(1).Cells.Item(1,5) = "Price in Euro"
    }
}

$Firma1 = Get-Content Z:\$billdate\Exchange\"Firma1.txt"
$Firma1 -replace 'Firma1', 'F1    ' > Z:\$billdate\Exchange\"Firma1.txt"

$Firma2 = Get-Content Z:\$billdate\Exchange\"Firma2.txt"
$Firma2 -replace 'irma2', '     ' -replace 'Firma2', 'F2     ' > Z:\$billdate\Exchange\"Firma2.txt"

$postfaecher = 0

echo "Excel Tabelle wird gebaut"

foreach ($companylist in (Get-Content Z:\Scripte\Exchange\companylist.txt))
{
	foreach ($postfaecher in (Get-Content Z:\$billdate\Exchange\$companylist.txt | Where-Object { $_.readcount -gt 4}))
	{
		# Ignore blank lines
		if ($postfaecher) {
			$company,$office,$user,$size = $postfaecher -split ' {2,}'
			if ([string]::IsNullOrEmpty($company) -or [string]::IsNullOrEmpty($office))
			{
				$worksheet = $workbook.worksheets | where {$_.name -eq "Tester"}
				$postfaecherCount++
				$count = ($worksheet.UsedRange.Rows).count+1
				$worksheet.Cells.Item($count,1) = "$company"
				$worksheet.Cells.Item($count,2) = "$office"
				$worksheet.Cells.Item($count,3) = "$user"
				$worksheet.Cells.Item($count,4) = "$size"

$worksheet.Cells.Item($count,5) = "$price"

foreach ($companys in (Get-Content Z:\Scripte\Exchange\companys.txt))
{
	$worksheet = $workbook.worksheets | where {$_.name -eq "$companys"}
	$row = $worksheet.UsedRange.rows.count
	$range = $worksheet.Range("B2:AK$row")
	$range2 = $worksheet.Range("B2")
	$range.Sort($range2)
	#Autofit the columns
}

$workbook.SaveAs($path)
$workbook.Close()

#COM-Objektes beenden
$Excel.Quit()
#COM-Objektes aus dem Speicher entfernen
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)


Viewing all articles
Browse latest Browse all 2314


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>