More Related Content
Similar to getSIDUsers (20)
getSIDUsers
- 1. <#Import in modules, if statement for PSSnapin so that it doesn't throw
an error if it is already loaded.#>
Import-Module ActiveDirectory
if ( (Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.Admin -
ErrorAction SilentlyContinue) -eq $null )
{
add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin
}
<#Clear variables so they are not retaining any old values#>
Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0
$errorLogPath = "c:scriptslogsSIDUsers.txt"
$adminUser = whoami
$mailboxes = Get-Mailbox -ResultSize unlimited | Where
{ $_.linkedMasterAccount -like "S-*" }
foreach ($mailbox in $mailboxes){
$fileCreationTime = Get-Date -UFormat "%Y%m%d%H%M%S"
$displayName = $mailbox.DisplayName
$pstFolderPath = Join-Path "SanitizedServerNameexchangePST"
$fileCreationTime$displayName.PST
$txtFolderPath = Join-Path "SanitizedServerNameexchangePST"
$fileCreationTime$displayName.txt
try {
Add-MailboxPermission -Identity $mailbox -User $adminUser -
AccessRights FullAccess -ErrorAction Stop -Verbose -Confirm:$false}
catch {
Write-Warning "Permission add problem, logging error to
$errorLogPath!"
Write-Warning $error[0]
$error[0] | Out-File $errorLogPath -Append}
try{
Export-Mailbox -Identity $mailbox -PSTFolderPath $pstFolderPath -
ErrorAction Stop -Verbose -Confirm:$false}
catch{
Write-Warning "Export problem!"
Write-Warning $error[0]
$error[0] | Out-File $errorLogPath -Append}
try {
Get-Mailbox -Identity $mailbox | FL | Out-File $txtFolderPath -
ErrorAction Stop -Verbose -Confirm:$false}
catch {
Write-Warning "Problem writing to txt"
Write-Warning $error[0]
$error[0] | Out-File $errorLogPath -Append}
try{
Write-Verbose "!!!!!!!!!!!!!!!!!!"
Remove-Mailbox -Identity $exUserCorpMatch[$c] -Permanent $true -
ErrorAction Stop -Verbose -Confirm:$false}
catch {
Write-Warning $error[0]
$error[0] | Out-File $errorLogPath -Append}
}