Exchange 2010 Powershell指令碼攻略(三)

ImSunkist發表於2011-07-12

AggregatePFData

Param(

[string] $PublicFolder,

[string] $Server

)

$script:SkipCount = 0

$script:SkippedString = ""

# Function that dumps aggregated information from various pf cmdlets

function dump-pf($pf, $TargetServer)

{

# We don't care about the IPM root

if ($pf.Identity.ToString() -ne "")

{

$HasReplica = $False

if ($TargetServer)

{

$Server = $TargetServer

$dbName = (get-publicfolderdatabase -Server $TargetServer).Name

$pf.Replicas | foreach-object { if ($_.Name -eq $dbName) { $HasReplica = $True } }

}

else

{

# We need to figure out where we can find a replica for this pf

$Server = (get-publicfolderdatabase $pf.Replicas[0]).Server

$HasReplica = $True

}

if ($HasReplica)

{

$pfStats = get-publicfolderstatistics $pf.Identity -server $Server

# Figure out who's the owner of this pf

$ipfOwner = 0

$pfOwnerString = ""

$pfOwners = Get-PublicFolderClientPermission $pf.Identity -server $Server | where {$_.AccessRights[0].ToString() -eq "Owner"}

$pfOwners | foreach-object { $pfOwnerString += $_.User; if ($ipfOwner -lt ($pfOwners.Count -1)) { $pfOwnerString += ", " } $ipfOwner = $ipfOwner + 1 }

# Remove duplicate item types.

$pfItemTypes = get-publicfolderItemStatistics $pf.Identity -server $Server | sort-object -Property ItemType -Unique

# Convert the list of ItemTypes into a comma separated string

$ipfItemType = 0

$pfItemTypesString = ""

$pfItemTypes | foreach-object { $pfItemTypesString += $_.ItemType; if ($ipfItemType -lt ($pfItemTypes.Count - 1)) { $pfItemTypesString += ", " } $ipfItemType = $ipfItemType + 1 }

# Create a result object to return to the pipeline

$configEntry = new-object System.Management.Automation.PSObject;

add-member -InputObject:$configEntry -MemberType:NoteProperty -Name:Identity -value:$pf.Identity

add-member -InputObject:$configEntry -MemberType:NoteProperty -Name:Owner -value:$pfOwnerString

add-member -InputObject:$configEntry -MemberType:NoteProperty -Name:ItemTypes -value:$pfItemTypesString

add-member -InputObject:$configEntry -MemberType:NoteProperty -Name:FolderType -value:$pf.FolderType

add-member -InputObject:$configEntry -MemberType:NoteProperty -Name:MailEnabled -value:$pf.MailEnabled

add-member -InputObject:$configEntry -MemberType:NoteProperty -Name:HasModerator -value:$pf.HasModerator

add-member -InputObject:$configEntry -MemberType:NoteProperty -Name:HasRules -value:$pf.HasRules

add-member -InputObject:$configEntry -MemberType:NoteProperty -Name:ItemCount -value:$pfStats.ItemCount

add-member -InputObject:$configEntry -MemberType:NoteProperty -Name:TotalItemSize -value:$pfStats.TotalItemSize

add-member -InputObject:$configEntry -MemberType:NoteProperty -Name:LastUserAccessTime -value:$pfStats.LastUserAccessTime

add-member -InputObject:$configEntry -MemberType:NoteProperty -Name:LastUserModificationTime -value:$pfStats.LastUserModificationTime

write-output $configEntry

}

# If there's no replica, just skip this folder

# and let the user know we skipped some.

else

{

if ($script:SkipCount -ge 25)

{

if ($script:SkipCount -eq 25)

{

$script:SkippedString += ", ..."

}

}

else

{

if ($script:SkipCount -gt 0)

{

$script:SkippedString += ", "

}

$script:SkippedString += $pf.Identity.ToString()

}

$script:SkipCount = $script:SkipCount + 1

}

}

}

# Function that returns true if the incoming argument is a help request

function IsHelpRequest

{

param($argument)

return ($argument -eq "-?" -or $argument -eq "-help");

}

# Function that displays the help related to this script following

# the same format provided by get-help or -?

function Usage

{

@"

NAME:

`tAggregatePFData.ps1

SYNOPSIS:

`tCrawls to the public folder hierarchy and displays usage information

`tabout each folder.

SYNTAX:

`tAggregatePFData.ps1

`t`t[-PublicFolder ]

`t`t[-Server ]

PARAMETERS:

`t-PublicFolder (optional)

`t`tThe folder identity of the public folder to operate against. If none

`t`tspecified, then all the folders in the hierarchy are returned.

`t-Server (optional)

`t`tThe server to operate against. Must be an Exchange 2007 or later Mailbox

`t`tserver with a public folder database. Defaults to a convenient server.

`t-------------------------- EXAMPLE 1 --------------------------

C:PS> .AggregatePFData.ps1 -PublicFolder PF1

`t-------------------------- EXAMPLE 2 --------------------------

C:PS> .AggregatePFData.ps1 -PublicFolder PF1 -Server Server1

"@

}

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

# Script starts here

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

# Check for Usage Statement Request

$args | foreach { if (IsHelpRequest $_) { Usage; exit; } }

if ($PublicFolder)

{

if ($Server)

{

$pfs = get-publicfolder $PublicFolder -Server $Server

}

else

{

$pfs = get-publicfolder $PublicFolder

}

}

else

{

if ($Server)

{

$pfs = get-publicfolder "" -Server $Server -Recurse

}

else

{

# build a list of pfs in the org, use the pf dbs as a starting point and eliminate duplicate pfs

# do it in 2 steps because of remote powershell pipelining

$pfdbs = get-publicfolderdatabase

$pfdbs | foreach-object { $pfs += get-publicfolder "" -Server $_.Server -Recurse; $pfs = $pfs | Sort-Object -Property ParentPath,Name -Unique }

}

}

# now dump the information for each pf

$pfs | foreach-object { dump-pf -pf $_ -TargetServer $Server }

if ($script:SkipCount -gt 0)

{

Write-Warning "The following folders were skipped since they did not contain a replica on the server: "

Write-Warning $script:SkippedString

}

[@more@]Exchange 2010 Powershell指令碼攻略(一)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23700676/viewspace-1052329/,如需轉載,請註明出處,否則將追究法律責任。

相關文章