none
Powershell remoting job not accepting credentials

    Question

  • Hi All

    Recently I've been trying to create a function that exports the contents of a mailbox to a share, waits for the process to finish and then imports the resultant pst file into another mailbox. I've found that enter-pssession combined with start-job cmdlet would be the best for me as I am aiming for the job to run on the remote machine so that the initial session can be freely closed and it would still continue running.

    The code so far is:

    function transfer-mailbox {
        [CmdletBinding()]
        Param(
        [parameter(mandatory=$true)]
        [string] $sam,
        [parameter(mandatory=$true)]
        [string] $newowner
        )
        #need to pass arguments into a csv file from which it can be read

        Enter-PSSession -ComputerName machinename -ConfigurationName microsoft.exchange -Authentication Kerberos
        $sam = "fromcsv"
        $newowner = "fromcsv"
        $credential = Get-Credential

        start-job -Name $sam -ArgumentList $sam,$newowner,$credential -InitializationScript {Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn} -ScriptBlock {

            get-mailbox $sam | New-MailboxExportRequest -FilePath "path\$sam.pst" -name $sam
            do
              {
                  start-sleep -Seconds 60
                  $status = (get-mailboxexportrequest -name $sam).status
              }
              while ($status -eq "completed" -or $status -eq "failed") 

            if ($status -eq "completed") {
                new-MailboxImportRequest -Mailbox $newowner -TargetRootFolder  ""$sam"_mailbox_contents" -FilePath "path\$sam.pst"
                }
            else {write-host "RUN *****, RUN!!" }
            }
    }

    what I get is the below:

    Active Directory operation failed on . The supplied credential for 'cred' is invalid.
        + CategoryInfo          : NotSpecified: (:) [], ADInvalidCredentialException
        + FullyQualifiedErrorId : [Server=server,RequestId=88d9481f-ab1e-4938-953a-f3bcc6a5d1e9,TimeStamp=07/11/2018
        12:17:56] [FailureCategory=Cmdlet-ADInvalidCredentialException] CB6309E
        + PSComputerName        : localhost

    What I don't understand is that why it fails? Credentials are explicitly provided, psremoting is correctly enabled as this server also offers published applications like ADUC.

    Thanks for any help.

    Tamas Boda


    • Edited by BODA_AGCO Wednesday, November 7, 2018 1:03 PM
    Wednesday, November 7, 2018 1:03 PM

All replies