我有一个正在设置的脚本,用于将用户的Exchange邮箱迁移到.pst文件。我的想法是,我将拥有一个CSV文件,可以放置用户名,然后在每晚启动脚本时,它将打开CSV文件并查找已添加的用户,对这些用户帐户执行请求的操作(导出,移动设置的权限等),然后写回到CSV文件,将这些用户标记为已完成,并写下他们的完成日期。这是我到目前为止所拥有的。
$InPstPath = '\\server1\PST_Store\'
$OutPstPath = '\\server2\PST_Store\'
$User = Get-Content $OutPstPath'login.txt'
$PWord = cat $OutPstPath'pass.txt' | convertto-securestring
$Credentials = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $PWord
$PSSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://Server1/powershell -Credential $Credentials
Import-PSSession $PSSession
$In_List = Invoke-Command {Import-Csv "\\Server1\PST_Store\Admin\To_Be_Exported.csv"} -computername Server1 -Credential $Credentials
foreach ($objUser in $In_List) {
if ($objUser.Completed -ne "Yes") {
$TargetUser = $objUser.name
$ShortDate = (Get-Date).toshortdatestring()
New-MailboxExportRequest -Mailbox $TargetUser -Filepath "$InPstPath$TargetUser.pst"
$objUser.Completed = "Yes"
$objUser.Date = $ShortDate
}
}
Remove-PSSession -Session (Get-PSSession)
我想不出一种不错的方法将$ objUser.Completed和$ objUser.Date值写回CSV。
首先,这很明显,但无论如何我还是要说一下。首次运行此脚本时,$ objUser.name,$ objUser.Completed和$ objUser.Date将不存在;所以,线
$TargetUser=$objUser.name
除非您实际上在该csv中具有适当的结构(即,具有标题名称,completed,date),否则它将无法工作。
现在,假设您已经完成了该部分,那么您所要做的就是创建一个对象,该对象捕获对象中的状态,然后将其写回。
$Processed = foreach ($objUser in $In_List) {
if ($objUser.Completed -ne "Yes") {
$TargetUser = $objUser.name
$ShortDate = (Get-Date).toshortdatestring()
New-MailboxExportRequest -Mailbox $TargetUser -Filepath "$InPstPath$TargetUser.pst"
[PSCustomObject]@{Name=$objUser.name;Completed="Yes";Date=$ShortDate}
}
} else {
[PSCustomObject]@{Name=$objUser.name;Completed="No";Date=$null}
}
## export to a temp file
$Processed | export-csv -Path $env:TEMP\processed.csv
## You should probably check to see if original file was modified since you started working on it
# and copy over if not
Copy-Item $env:TEMP\processed.csv $OutPstPath'login.txt' -force
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句