troubleshooting Question

Powershell Exchange - help using invoke-command

Avatar of Member_2_8006433
Member_2_8006433 asked on
ExchangePowershell
16 Comments1 Solution997 ViewsLast Modified:
I am trying make a remote session to Exchange server and then archiving to PST. I am having some trouble figuring how to pass variables to the invoke-command line.


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 "Chose one"
     switch ($input)
     {
        '1' {
                cls

                $PstPath = "\\server\c$\temp"

                $user = Read-Host "Enter mailbox"
                $date = Read-Host "Enter month/year (m-yyyy)"
                $arrDate = $date.Split('-')
                $firstDayOfMonth = Get-Date -Date "$($arrDate[1])-$($arrDate[0])-1 00:00:00"
                $lastDayOfMonth = $firstDayOfMonth.AddMonths(1).AddDays(-1)


                New-Item $PstPath\$user -type directory -Force

                Invoke-command -ScriptBlock { New-MailboxExportRequest -ContentFilter {(Received -ge $firstDayOfMonth) -and (Received -lt $lastDayOfMonth)} -Mailbox $user -FilePath $PstPath\$user\$date.pst} -Session $Session
                }

        'q' {
                Remove-PSSession $exchange10
                return
            }
     }
     pause
     
}
until ($input -eq 'q')
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros