Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
$Server = 'PRODFS01-VM'
$ServerBackupUNCPath = "\\$Server\PST\"
$InputCSVPath = 'C:\LOGS\Users.csv'
$ExportCSVPath = 'C:\LOGS\Output.csv'
$ExportExistsCSVPath = 'C:\LOGS\Exist.csv'
Import-PSSession ($Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://PRDEXC08-VM/Powershell/ -Authentication Kerberos)
Get-Content -Path $InputCSVPath |
Get-MailBox |
% {
Write-Host "Processing .... $($_.Name) ..." -ForegroundColor Green
# Check if the file already exist or not
$FileResult = Get-WmiObject CIM_DataFile -ComputerName $Server -filter "path='$ServerBackupUNCPath'" | ? {$_.name -match "$($_.Alias).PST" }
if ( $FileResult -eq $null ) {
#If there is no exported .PST file on the destination folder, then begin the export mailbox command and log if there any error to the AliasName.LOG file:
New-MailboxExportRequest -Mailbox $_ -FilePath "$ServerBackupUNCPath$($_.Alias).PST" -baditemlimit 50 -acceptlargedataloss
# wait until error or processed:
while ( ($req = Get-MailboxExportRequest $_) | ? { $_.Status -match 'Queued|InProgress' } )
{ Start-Sleep 180 }
$req | Get-MailboxExportRequestStatistics -IncludeReport | Select -Expand Report | Out-File "C:\LOGS\$($_.Alias).log"
} else {
Write-Output "The user $($_.Alias).PST file is already existing"
#Append the list of already existing list of users C:\LOGS\Exist.csv
($_.Alias) | Out-File -FilePath $ExportExistsCSVPath -Append
}
# I assume, whatever line I put here will be executed regardless of any of the condition above is met or not
Remove-Mailbox -Identity $_ -Confirm $false -WhatIf
}
The line to do the comparison and appending the name of the already existing files is not working, hence I need some help to fix it.
Any help would be greatly appreciated very muchly.
Unfortunately your CSV file is not a CSV file and it does not contain names that can be piped to Get-Mailbox. You can only pipe identities to the CmdLet.
$Server = 'PRODFS01-VM'
$ServerBackupUNCPath = "\\$Server\PST"
$InputCSVPath = 'C:\LOGS\Input.csv'
$ExportCSVPath = 'C:\LOGS\Output.csv'
$ExportExistsCSVPath = 'C:\LOGS\EmailExist.csv'
Import-PSSession ($Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://PRDEXC03-VM/Powershell/ -Authentication Kerberos)
$Users = Get-Content -Path $InputCSVPath
&{
foreach ($User in $Users) {
Write-Host "Processing.... $User"
Get-Mailbox $User.ToString() | Select WindowsEmailAddress
}
} | Export-Csv -Path $ExportCSVPath -NoTypeInformation
Import-Csv -Path $ExportCSVPath |
Get-MailBox |
% {
Write-Host "Processing .... $($_.Name) ..." -ForegroundColor Green
# Check if the file already exist or not
$FileResult = Test-Path -Path "$ServerBackupUNCPath\$($_.WindowsEmailAddress).PST" -PathType Leaf
if ( $FileResult -ne $True ) {
#If there is no exported .PST file on the destination folder, then begin the export mailbox command and log if there any error to the AliasName.LOG file:
New-MailboxExportRequest -Mailbox $_ -FilePath "$ServerBackupUNCPath\$($_.WindowsEmailAddress).PST" -BadItemLimit 50 -AcceptLargeDataLoss -WhatIf
# wait until error or processed:
while ( ($req = Get-MailboxExportRequest $_) | ? { $_.Status -match 'Queued|InProgress' } )
{ Start-Sleep 180 }
$req | Get-MailboxExportRequestStatistics -IncludeReport | Select -Expand Report | Out-File "C:\LOGS\$($_.WindowsEmailAddress).log"
} else {
Write-Output "The user $($_.WindowsEmailAddress).PST file is already existing"
#Append the list of already existing list of users C:\LOGS\Exist.csv
($_.Alias) | Out-File -FilePath $ExportExistsCSVPath -Append
}
# I assume, whatever line I put here will be executed regardless of any of the condition above is met or not
Write-Host "Removing Mailbox $($_.WindowsEmailAddress)" -ForegroundColor Red
Remove-Mailbox -Identity $_ -Confirm $false -WhatIf
}
The ExportedCSV is nicely formatted as the email address like the below:
The operation couldn't be performed because object '"WindowsEmailAddress"' couldn't be found on 'PRODDC01-VM.domain.com'.
+ CategoryInfo : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException
+ FullyQualifiedErrorId : [Server=PRDEXC03-VM,RequestId=c061c8a3-72b6-469a-8109-726e1b015441,TimeStamp=21/08/2018 12:12:13 AM] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] ED9B9A7,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
+ PSComputerName : PRDEXC03-VM
The operation couldn't be performed because object '"Russel.Peters@domain.com"' couldn't be found on 'PRODDC01-VM.domain.com'.
+ CategoryInfo : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException
+ FullyQualifiedErrorId : [Server=PRDEXC03-VM,RequestId=c061c8a3-72b6-469a-8109-726e1b015441,TimeStamp=21/08/2018 12:12:13 AM] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] FE47387B,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
+ PSComputerName : PRDEXC03-VM
The operation couldn't be performed because object '"Amalia.Zebech@domain.com"' couldn't be found on 'PRODDC01-VM.domain.com'.
+ CategoryInfo : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException
+ FullyQualifiedErrorId : [Server=PRDEXC03-VM,RequestId=c061c8a3-72b6-469a-8109-726e1b015441,TimeStamp=21/08/2018 12:12:13 AM] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 3DFB1CD9,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
+ PSComputerName : PRDEXC03-VM
.....
The operation couldn't be performed because object '"Karina.Michaels@domain.com"' couldn't be found on 'PRODDC01-VM.domain.com'.
+ CategoryInfo : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException
+ FullyQualifiedErrorId : [Server=PRDEXC03-VM,RequestId=c061c8a3-72b6-469a-8109-726e1b015441,TimeStamp=21/08/2018 12:12:13 AM] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 3DFB1CD9,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
+ PSComputerName : PRDEXC03-VM
$Server = 'PRDFILESVR01-VM'
$ServerBackupUNCPath = "\\$Server\PST"
$InputCSVPath = 'C:\LOGS\Input.csv'
$ExportCSVPath = 'C:\LOGS\Output.csv'
$ExportExistsCSVPath = 'C:\LOGS\Exist.csv'
#Import-PSSession ($Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://PRDEXC03-VM/Powershell/ -Authentication Kerberos)
$Users = Get-Content -Path $InputCSVPath
&{
foreach ($User in $Users) {
Write-Host "Processing.... $User"
Get-Mailbox $User.ToString() | Select PrimarySmtpAddress
}
} | Export-Csv -Path $ExportCSVPath -NoTypeInformation
Import-Csv -Path $ExportCSVPath |
Get-MailBox $_ |
% {
Write-Host "Processing .... $($_.Name) ..." -ForegroundColor Green
# Check if the file already exist or not
$FileResult = Test-Path -Path "$ServerBackupUNCPath\$($_.PrimarySmtpAddress).PST" -PathType Leaf
if ( $FileResult -ne $True ) {
#If there is no exported .PST file on the destination folder, then begin the export mailbox command and log if there any error to the AliasName.LOG file:
New-MailboxExportRequest -Mailbox $_ -FilePath "$ServerBackupUNCPath\$($_.PrimarySmtpAddress).PST" -BadItemLimit 50 -AcceptLargeDataLoss -WhatIf
} else {
Write-Output "The user $($_.PrimarySmtpAddress).PST file is already existing"
#Append the list of already existing list of users C:\LOGS\Exist.csv
Get-MailboxExportRequestStatistics -IncludeReport | Select -Expand Report | Out-File "C:\LOGS\$($_.PrimarySmtpAddress).log"
}
# I assume, whetever line I put here will be executed regardless of any of the condition above is met or not
#Remove-Mailbox -Identity $_.PrimarySmtpAddress -Confirm $false -WhatIf
Write-Host "Removing Mailbox $($_.PrimarySmtpAddress)" -ForegroundColor Red
}
Here's the error message:
The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
+ CategoryInfo : InvalidArgument: (@{PrimarySmtpAd...@Domain.com}:PSObject) [Get-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Get-Mailbox
+ PSComputerName : PRDEXC03-VM.Domain.com
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.