none
Can't get script to run on multiple mailboxes RRS feed

  • Question

  • Hello!

    I need to run this script (expACCache.ps1) for all the mailboxes on Exchange Online organization. As I had not idea how to accomplish it in the original script, I have tried to invoke the script 'expACCache.ps1' via other PS script. See bellow:

    Connect-MsolService -Credential $psCred 
    $ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $psCred -Authentication "Basic" -AllowRedirection
      Import-PSSession $ExchangeSession
      $mbxs = Get-Mailbox
    $scriptPath="C:\scripts\expACCache.ps1"
    foreach ($MailboxName in $mbxs) {
    Invoke-Expression "& `"$scriptPath`" $MailboxName"
    
    }

    but got a lot of errors. For each block I have got:

    Get-Credential : Cannot process command because of one or more missing mandatory parameters: Credential.
    At C:\scripts\expACCache.ps1:20 char:11
    + $psCred = Get-Credential
    +           ~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Credential], ParameterBindingException
        + FullyQualifiedErrorId : MissingMandatoryParameter,Microsoft.PowerShell.Commands.GetCredentialCommand
    
    You cannot call a method on a null-valued expression.
    At C:\scripts\expACCache.ps1:21 char:1
    + $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$ps ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
    
    Exception calling "AutodiscoverUrl" with "2" argument(s): "The e-mail address is formed incorrectly."
    At C:\scripts\expACCache.ps1:69 char:1
    + $service.AutodiscoverUrl($MailboxName,{$true})
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : FormatException

    How to run this script and pass the credential for single time?

    Thanks!






    • Edited by tech.cons Monday, June 5, 2017 7:41 AM
    Monday, June 5, 2017 1:21 AM

Answers

  • The script works if "...|select -ExpandProperty PrimarySmtpAddress" is used on Invoke-Expression...

    • Edited by tech.cons Tuesday, June 6, 2017 11:59 AM
    • Marked as answer by tech.cons Tuesday, June 6, 2017 12:29 PM
    Tuesday, June 6, 2017 11:58 AM

All replies

  • The error does not match the script.  There is no "Get-Credential" in the script you posted.


    \_(ツ)_/

    Monday, June 5, 2017 1:55 AM
  • You have asked a question about a script but you didn't post the script.

    Ed Crowley MVP "There are seldom good technological solutions to behavioral problems."
    Celebrating 20 years of providing Exchange peer support!

    Monday, June 5, 2017 6:26 AM
  • Hello!

    I have updated the topic to get better understanding of the problem.

    Thanks!

    Monday, June 5, 2017 7:43 AM
  • The error does not match the script.  There is no "Get-Credential" in the script you posted.

    Please post the correct script.

    This is how to connect:

    $pscred = Get-Credential yourid@domain.com
    Connect-MsolService -Credential $psCred


    \_(ツ)_/


    • Edited by jrv Monday, June 5, 2017 6:01 PM
    Monday, June 5, 2017 5:58 PM
  • Hello!Script which invokes this script (expACCache.ps1) now is following

    $pscred = Get-Credential acccont@domain.com
    Connect-MsolService -Credential $psCred 
    $ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $psCred -Authentication "Basic" -AllowRedirection
      Import-PSSession $ExchangeSession
      $mbxs = Get-Mailbox
    $scriptPath="C:\scripts\expACCache.ps1"
    foreach ($MailboxName in $mbxs) {
    Invoke-Expression "& `"$scriptPath`" $MailboxName"
    
    }

    but errors now are following:

    Exception calling "AutodiscoverUrl" with "2" argument(s): "The e-mail address is formed incorrectly."
    At C:\scripts\expACCache.ps1:69 char:1
    + $service.AutodiscoverUrl($MailboxName,{$true})
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : FormatException
    
    Using CAS Server :
    Process Recipient Cache
    Exception calling "Bind" with "2" argument(s): "The Url property on the ExchangeService object must be set."
    At C:\scripts\expACCache.ps1:83 char:1
    + $RecipientCache = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$f ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ServiceLocalException
    
    Exception calling "FindItems" with "2" argument(s): "The element at position 0 is invalid
    Parameter name: parentFolderIds"
    At C:\scripts\expACCache.ps1:90 char:5
    +     $fiItems = $service.FindItems($RecipientCache.Id,$ivItemView)
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ArgumentException
    
    Exception calling "LoadPropertiesForItems" with "2" argument(s): "Value cannot be null.
    Parameter name: items"
    At C:\scripts\expACCache.ps1:91 char:5
    +     [Void]$service.LoadPropertiesForItems($fiItems,$psPropset)
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ArgumentNullException
    
    Process Suggested Contacts
    Exception calling "FindFolders" with "3" argument(s): "The Url property on the ExchangeService object must be set."
    At C:\scripts\expACCache.ps1:112 char:1
    + $findFolderResults = $service.FindFolders($folderid,$SfSearchFilter,$fvFolderVie ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ServiceLocalException
    
    Process OWA AutocompleteCache
    Exception calling "Bind" with "4" argument(s): "The Url property on the ExchangeService object must be set."
    At C:\scripts\expACCache.ps1:142 char:1
    + $UsrConfig = [Microsoft.Exchange.WebServices.Data.UserConfiguration]::Bind($serv ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ServiceLocalException
    
    Exception calling "GetString" with "1" argument(s): "Array cannot be null.
    Parameter name: bytes"
    At C:\scripts\expACCache.ps1:144 char:1
    + $acXML = [System.Text.Encoding]::UTF8.GetString($UsrConfig.XmlData)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ArgumentNullException
    
    You cannot call a method on a null-valued expression.
    At C:\scripts\expACCache.ps1:146 char:1
    + $boMark = $acXML.SubString(0,1)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
    
    You cannot call a method on a null-valued expression.
    At C:\scripts\expACCache.ps1:148 char:1
    + [XML]$acXML = $acXML.SubString(1)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull


    • Edited by tech.cons Monday, June 5, 2017 11:30 PM
    Monday, June 5, 2017 11:30 PM
  • You will have to ask the author of that script for help.  The original issue has obviously been solved.  Mark the answer.

    Note that you cannot just arbitrarily call any script without understanding how it is intended to work.  The author of your script has blogged about its use.  Read the blog carefully.


    \_(ツ)_/

    Monday, June 5, 2017 11:48 PM
  • Also note that the script was written for an older version of OWA and not for Office 365.


    \_(ツ)_/

    Monday, June 5, 2017 11:50 PM
  • The script works if "...|select -ExpandProperty PrimarySmtpAddress" is used on Invoke-Expression...

    • Edited by tech.cons Tuesday, June 6, 2017 11:59 AM
    • Marked as answer by tech.cons Tuesday, June 6, 2017 12:29 PM
    Tuesday, June 6, 2017 11:58 AM