我已经构建了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"
答案 0 :(得分:1)
问题确实很可能是由New-MailboxExportRequest
引起的,正如您已经怀疑的那样。 cmdlet打印有关创建的对象的信息,该信息与您在循环中创建的其余输出集中在一起,然后分配给变量$Processed
。
为避免这种情况,您可以像这样抑制cmdlet输出:
New-MailboxExportRequest -Mailbox ... | Out-Null
或者像这样:
New-MailboxExportRequest -Mailbox ... >$null
将输出分配给变量也应该有效:
$exportRequest = New-MailboxExportRequest -Mailbox ...
答案 1 :(得分:0)
在Export-CSV上,尝试添加标志:“ - NoTypeInformation”
答案 2 :(得分:0)
我认为这可能是自定义对象与另一个现有对象(可能是Exchange服务器上的mailboxexportrequest对象)之间的某种名称空间交叉问题。在搞乱了一段时间后,我能够以一种新的方式让它失败,其中生成的csv文件充满了来自邮箱导出的详细信息,并且它们是一个也列出了用户名的“名称”列。我将输入csv上的哈希值从'name更改为'username',并且生成的MailboxExport条目已停止。现在有空白行,但我当然愿意忍受这种不完美,因为它不会破坏这个(短暂的)过程。
如果有人对根本原因有任何了解,我当然希望听到它是什么,但我想我已经找到了解决方案,我可以忍受。