1

我已经构建了 powershell 脚本,并且在 CSV 文件中得到了一个“随机”的输出。该字符串是 MailboxExport(和一个数字)。它看起来像 (Get-MailboxExportRequest).name 将返回的值,但我看不到我会在哪里拉出类似的东西或它是如何插入的。我想我可能只是盯着它太久了,我可能只需要一双新的眼睛来发现我的错误。我会进入脚本试图做的事情,但我在脚本中放了很多注释,应该可以很好地解释它。

##################################################  PST Extraction Script  ##################################################
# Completed October 2013 by Trey Nuckolls
#
# This script is meant to extract PST files from the Site 1 Exchange server at the Site2 site and deliver those PST
# files to a share on the Site2 network.  The script will change the input CSV file to keep track of which PSTfiles have been
# extracted and when that occoured.  The script will also set security on the PST file so only the user and IT administraion
# can access the PST file.
#
# To run this script, enter the username of the Site 1 domain account that you want to target for extraction of a PST file then
# Run the script.  Can be run from any machine on the network as long as it is run by someone with domain admin rights on the
# Site 2 network.  Powershell v2 or v3 is required to run the script.
#
#############################################################################################################################
$InPstPath = '\\Site1_Server\PST_Store'
$OutPstPath = '\\Site2_Server\PST_Store' 
$AdminPath = '\\Site2_Server\PST_Store\Admin\'

#Container for Site1 username
$User = Get-Content $AdminPath'login.txt'

#Container for encrypted Site1 Password
$PWord = Cat $AdminPath'pass.txt' | ConvertTo-SecureString

#Credential package for accessing Site1 resouces
$Credentials = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $PWord

#Creation of Powershell Drives for use during session
New-PSDrive -Name Site1Share -PSProvider FileSystem -Root $InPstPath -Credential $Credentials
New-PSDrive -Name Site2Share -PSProvider FileSystem -Root $OutPstPath

#Container for Powershell session to Exchange server
$PSSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://Site1_Server/powershell -Credential $Credentials

#Creation of Powershell session to Site1 Exchange server, including import of exchange commandlets
Import-PSSession $PSSession

#Import of the CSV file that lists users to be targeted 
$In_List = Invoke-Command {Import-Csv "\\Site1_Server\PST_Store\To_Be_Exported.csv"} -computername Site1_Server -Credential $Credentials


$Processed = foreach ($objUser in $In_List) {
    if ($objUser.Completed -ne "Yes") {
        $TargetUser = $objUser.name
        $ShortDate = (Get-Date).toshortdatestring()
        $SourceFile = "Site1Share:\$TargetUser.pst"
        $DestinationFile = "Site2Share:\$TargetUser.pst"

        #Export Mailbox to PST File
        New-MailboxExportRequest -Mailbox $TargetUser -Filepath $InPstPath\$TargetUser.pst
        do {Start-Sleep -Seconds 10}
            until((Get-MailboxExportRequest -Status InProgress).count -eq 0)

        #Copy PST File to PST Share
        Copy-Item -Path $SourceFile -Destination $DestinationFile

        #Add Security access on PST file (Target_User-Modify).  Domain Admin-Full is inherited from parent.
        $Acl = Get-Acl $DestinationFile
        $Permission = "Site2_Domain\$TargetUser","Modify","Allow"
        $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $Permission
        $Acl.SetAccessRule($AccessRule)
        $Acl | Set-Acl $DestinationFile

        #Remove PST file From Temporary area
        Remove-Item -Path $SourceFile -Force

        #Write back to checklist for new items that have just been processed
        [PSCustomObject]@{Name=$TargetUser;Completed="Yes";Date=$ShortDate}
    } else { if ($objUser.Completed -eq "Yes") {
        #Passthrough of items that have already been completed
            [PSCustomObject]@{Name=$objUser.name;Completed=$objUser.Completed;Date=$objUser.Date}}
}} 
    #Output the new version of the checklist
    $Processed | export-csv -Path C:\TEMP\processed.csv

    #Overwrite the old version checklist with the new one
    Move-Item -Path C:\TEMP\processed.csv -Destination Site1Share:\To_Be_Exported.csv -force

#Cleanup PsDrives and PsSessions
Remove-PSDrive -Name Site1Share
Remove-PSDrive -Name Site2Share
Remove-PSSession -Session (Get-PSSession)

输入 CSV 是...

"Name","Completed","Date"
"User1","Yes","10/8/2013"
"User2","Yes","10/11/2013"
"User3",,

输出是...

"Name","Completed","Date"
"User1","Yes","10/8/2013"
"User2","Yes","10/11/2013"
"MailboxExport7",,
"User3","Yes","10/11/2013"
4

3 回答 3

1

New-MailboxExportRequest正如您已经怀疑的那样,问题确实很可能是由 引起的。该 cmdlet 打印有关已创建对象的信息,这些信息与您在循环中创建的其余输出集中在一起,然后分配给变量$Processed.

为避免这种情况,您可以像这样抑制 cmdlet 输出:

New-MailboxExportRequest -Mailbox ... | Out-Null

或像这样:

New-MailboxExportRequest -Mailbox ... >$null

将输出分配给变量也应该有效:

$exportRequest = New-MailboxExportRequest -Mailbox ...
于 2013-10-14T17:32:57.257 回答
0

在您的 Export-CSV 上,尝试添加标志:“-NoTypeInformation”

于 2013-10-11T22:13:28.160 回答
0

我认为这可能是自定义对象和另一个现有对象(可能是交换服务器上的 mailboxexportrequest 对象)之间的某种名称空间交叉问题。在搞砸了一段时间后,我能够以一种新的方式让它失败,其中生成的 csv 文件充满了邮箱导出的详细信息,并且它们是一个“名称”列,其中也列出了用户名。我将输入 csv 上的哈希值从“名称”更改为“用户名”,生成的 MailboxExport 条目已停止。现在有空白行,但我当然愿意忍受这种不完美,因为它不会破坏这个(短暂的)过程。

如果有人对根本原因有任何见解,我当然很想听听它是什么,但我想我已经找到了一个我可以忍受的解决方案。

于 2013-10-14T15:50:00.463 回答