Function Show-Menu {
Param ([string]$Title = 'Menu')
cls
Write-Host "================ $Title ================"
Write-Host ""
Write-Host "1: Archive .PST"
Write-Host "Q: Quit."
}
$ExchangeServer = "server"
$Credential = Get-Credential -Message "Enter Domain\username"
$SessionParams = @{
ConfigurationName = 'Microsoft.Exchange'
ConnectionURI = "http://$ExchangeServer/powershell/"
Authentication = 'Kerberos'
Credential = $Credential
}
$Session = New-PSSession @SessionParams
Do {
Show-Menu
$Input = Read-Host "Choose one"
Switch ($Input) {
'1' {
cls
$PstPath = "\\server\c$\temp"
$Mailbox = Read-Host "Enter mailbox"
New-Item $PstPath\$Mailbox -Type Directory -Force
$ArchiveDate = Read-Host "Enter month/year (m-yyyy)"
Invoke-Command -Session $Session -ArgumentList $PstPath, $Mailbox, $ArchiveDate -ScriptBlock {
Param($PstPath, $Mailbox, $ArchiveDate)
$Month, $Year = $ArchiveDate.Split('-')
$FirstDayOfMonth = Get-Date -Date "$($Year)-$($Month)-1 00:00:00"
$LastDayOfMonth = $FirstDayOfMonth.AddMonths(1).AddDays(-1)
New-MailboxExportRequest -ContentFilter {(Received -ge $FirstDayOfMonth) -and (Received -le $LastDayOfMonth)} -Mailbox $Mailbox -FilePath $PstPath\$Mailbox\$ArchiveDate.pst
}
}
'q' {
Remove-PSSession $Session
}
default {
Write-Warning "Unknown choice '$_' - try again."
}
}
pause
} Until ($Input -eq 'q')
The term 'Get-Date' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (Get-Date:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : server
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (AddMonths:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : server
'1' {
cls
$PstPath = "\\server\c$\temp"
$Mailbox = Read-Host "Enter mailbox"
New-Item $PstPath\$Mailbox -Type Directory -Force
$ArchiveDate = Read-Host "Enter month/year (m-yyyy)"
Invoke-Command -Session $Session -ArgumentList $PstPath, $Mailbox, $ArchiveDate -ScriptBlock {
Param($PstPath, $Mailbox, $ArchiveDate)
$Month, $Year = $ArchiveDate.Split('-')
$FirstDayOfMonth = Get-Date -Date "$($Year)-$($Month)-1 00:00:00"
$LastDayOfMonth = $FirstDayOfMonth.AddMonths(1).AddDays(-1)
New-MailboxExportRequest -ContentFilter {(Received -ge $FirstDayOfMonth) -and (Received -le $LastDayOfMonth)} -Mailbox $Mailbox -FilePath $PstPath\$Mailbox\$ArchiveDate.pst
}
...
Param($PstPath, $Mailbox, $ArchiveDate)
Import-Module Microsoft.PowerShell.Utility
...
Or you can work your way around it by calculating the date locally and then passing it on to the remote machine:Function Show-Menu {
Param ([string]$Title = 'Menu')
cls
Write-Host "================ $Title ================"
Write-Host ""
Write-Host "1: Archive .PST"
Write-Host "Q: Quit."
}
$ExchangeServer = "server"
$Credential = Get-Credential -Message "Enter Domain\username"
$SessionParams = @{
ConfigurationName = 'Microsoft.Exchange'
ConnectionURI = "http://$ExchangeServer/powershell/"
Authentication = 'Kerberos'
Credential = $Credential
}
$Session = New-PSSession @SessionParams
Do {
Show-Menu
$Input = Read-Host "Choose one"
Switch ($Input) {
'1' {
cls
$PstPath = "\\server\c$\temp"
$Mailbox = Read-Host "Enter mailbox"
New-Item $PstPath\$Mailbox -Type Directory -Force
$ArchiveDate = Read-Host "Enter month/year (m-yyyy)"
$Month, $Year = $ArchiveDate.Split('-')
$FirstDayOfMonth = Get-Date -Date "$($Year)-$($Month)-1 00:00:00"
Invoke-Command -Session $Session -ArgumentList $PstPath, $Mailbox, $ArchiveDate, $FirstDayOfMonth -ScriptBlock {
Param($PstPath, $Mailbox, $ArchiveDate, $FirstDayOfMonth)
$LastDayOfMonth = $FirstDayOfMonth.AddMonths(1).AddDays(-1)
New-MailboxExportRequest -ContentFilter {(Received -ge $FirstDayOfMonth) -and (Received -le $LastDayOfMonth)} -Mailbox $Mailbox -FilePath $PstPath\$Mailbox\$ArchiveDate.pst
}
}
'q' {
Remove-PSSession $Session
}
default {
Write-Warning "Unknown choice '$_' - try again."
}
}
pause
} Until ($Input -eq 'q')
The provided ContentFilter value is invalid. ContentFilter is invalid. Unable to cast object of type 'System.Management.Automation.PSObje ct' to type 'System.String'. -->
Unable to cast object of type 'System.Management.Automation.PSObje ct' to type 'System.String'.
+ CategoryInfo : InvalidArgument: ((Received -ge $...LastDayOfMonth):String) [], ContentFilterInvalidPerman entExcepti on
+ FullyQualifiedErrorId : FBF5F982
+ PSComputerName : server
The term 'Import-Module' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included
, verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (Import-Module:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : server
The term 'Get-Date' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (Get-Date:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : server
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (AddMonths:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : server
Function Show-Menu {
Param ([string]$Title = 'Menu')
cls
Write-Host "================ $Title ================"
Write-Host ""
Write-Host "1: Archive .PST"
Write-Host "Q: Quit."
}
$ExchangeServer = "server"
$Credential = Get-Credential -Message "Enter Domain\username"
$SessionParams = @{
ConfigurationName = 'Microsoft.Exchange'
ConnectionURI = "http://$ExchangeServer/powershell/"
Authentication = 'Kerberos'
Credential = $Credential
}
$Session = New-PSSession @SessionParams
Do {
Show-Menu
$Input = Read-Host "Choose one"
Switch ($Input) {
'1' {
cls
$PstPath = "\\server\c$\temp"
$Mailbox = Read-Host "Enter mailbox"
New-Item $PstPath\$Mailbox -Type Directory -Force
$ArchiveDate = Read-Host "Enter month/year (m-yyyy)"
$Month, $Year = $ArchiveDate.Split('-')
$FirstDayOfMonth = Get-Date -Date "$($Year)-$($Month)-1 00:00:00"
$LastDayOfMonth = $FirstDayOfMonth.AddMonths(1).AddDays(-1)
Invoke-Command -Session $Session -ArgumentList $PstPath, $Mailbox, $ArchiveDate, $FirstDayOfMonth, $LastDayOfMonth -ScriptBlock {
Param($PstPath, $Mailbox, $ArchiveDate, $FirstDayOfMonth, $LastDayOfMonth)
New-MailboxExportRequest -ContentFilter {(Received -ge $FirstDayOfMonth) -and (Received -le $LastDayOfMonth)} -Mailbox $Mailbox -FilePath $PstPath\$Mailbox\$ArchiveDate.pst
}
}
'q' {
Remove-PSSession $Session
}
default {
Write-Warning "Unknown choice '$_' - try again."
}
}
pause
} Until ($Input -eq 'q')
The provided ContentFilter value is invalid. ContentFilter is invalid. Unable to cast object of type 'System.Management.Automation.PSObje ct' to type 'System.String'. -->
Unable to cast object of type 'System.Management.Automation.PSObje ct' to type 'System.String'.
+ CategoryInfo : InvalidArgument: ((Received -ge $...LastDayOfMonth):String) [], ContentFilterInvalidPerman entExcepti on
+ FullyQualifiedErrorId : FBF5F982
+ PSComputerName : server
Exchange EMS
Name Value
---- -----
CLRVersion 2.0.50727.6421
BuildVersion 6.1.7600.16385
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
Windows Powershell at server:
Name Value
---- -----
PSVersion 3.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.18449
BuildVersion 6.2.9200.17065
PSCompatibleVersions {1.0, 2.0, 3.0}
PSRemotingProtocolVersion 2.2
Function Show-Menu {
Param ([string]$Title = 'Menu')
cls
Write-Host "================ $Title ================"
Write-Host ""
Write-Host "1: Archive .PST"
Write-Host "Q: Quit."
}
$ExchangeServer = "server"
$Credential = Get-Credential -Message "Enter Domain\username"
$SessionParams = @{
ConfigurationName = 'Microsoft.Exchange'
ConnectionURI = "http://$ExchangeServer/powershell/"
Authentication = 'Kerberos'
Credential = $Credential
}
$Session = New-PSSession @SessionParams
Do {
Show-Menu
$Input = Read-Host "Choose one"
Switch ($Input) {
'1' {
cls
$PstPath = "\\server\c$\temp"
$Mailbox = Read-Host "Enter mailbox"
New-Item $PstPath\$Mailbox -Type Directory -Force
$ArchiveDate = Read-Host "Enter month/year (m-yyyy)"
$Month, $Year = $ArchiveDate.Split('-')
$FirstDayOfMonth = Get-Date -Date "$($Year)-$($Month)-1 00:00:00"
$LastDayOfMonth = $FirstDayOfMonth.AddMonths(1).AddDays(-1)
Invoke-Command -Session $Session -ArgumentList $PstPath, $Mailbox, $ArchiveDate, $FirstDayOfMonth.ToShortDateString(), $LastDayOfMonth.ToShortDateString() -ScriptBlock {
Param($PstPath, $Mailbox, $ArchiveDate, $FirstDayOfMonth, $LastDayOfMonth)
New-MailboxExportRequest -ContentFilter {(Received -ge $FirstDayOfMonth) -and (Received -le $LastDayOfMonth)} -Mailbox $Mailbox -FilePath $PstPath\$Mailbox\$ArchiveDate.pst
}
}
'q' {
Remove-PSSession $Session
}
default {
Write-Warning "Unknown choice '$_' - try again."
}
}
pause
} Until ($Input -eq 'q')
The provided ContentFilter value is invalid. ContentFilter is invalid. The value "31-03-2017" could not be converted to type System.DateTime. --> The value "31-03-2017" could not be converted to type System.DateTime.
+ CategoryInfo : InvalidArgument: ((Received -ge $...LastDayOfMonth):String) [], ContentFilterInvalidPerman entExcepti on
+ FullyQualifiedErrorId : D23A681B
+ PSComputerName : server