locked
create new user user mail box using power shell script RRS feed

  • Question

  • hi ,the following is the script to create new user mail box in office 365 trail

    -------------------------------------------------------------------------------------------------------------------

    • Shell command:

    $secpasswd=ConvertTo-SecureString "${AdminPassword}" -AsPlainText -Force;

                        $mycreds=New-Object System.Management.Automation.PSCredential("${AdminUserId}",$secpasswd);

                        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri 
    https://ps.outlook.com/powershell/  -Credential $mycreds -Authentication Basic -AllowRedirection -WarningAction SilentlyContinue;

                        Import-PSSession $Session -CommandName Remove-mailbox, Get-mailbox, New-Mailbox -AllowClobber -DisableNameChecking -WarningAction SilentlyContinue | Out-Null;

                        get-mailbox '${Name}' | select-object name | out-null;

                        if($? -eq "True")

                        {remove-PSSession $Session; Write-Host "This mailbox already exist"; exit 1;}

                        else

                        {

                        get-mailbox '${UserPrincipalName}' | select-object name | out-null;

                        if($? -eq "True")

                        {remove-PSSession $Session;Write-Host "This mailbox already exist"; exit 1;} else { New-Mailbox -Alias '${Alias}' -Name '${Name}' -FirstName '${FirstName}' -LastName '${LastName}' -DisplayName '${DisplayName}' -MicrosoftOnlineServicesID '${UserPrincipalName}' -Password (ConvertTo-SecureString -String '${UserPassword}' -AsPlainText -Force) -ResetPasswordOnNextLogon $false -WarningAction SilentlyContinue | out-null;

                        if($? -eq "True") {remove-PSSession $Session; exit 0;} else {remove-PSSession $Session; write-host "Mailbox could not be created. Check for password or UserPrincipalName."; exit 1;}

                        }

                        }

    --------------------------------------

    • The error which i am getting is :

    Failed

     

    Cause:

    New-PSSession :

    At line:3 char:17

    + $Session = New-PSSession -ConfigurationName

    Microsoft.Exchange -ConnectionU ...

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    ~~~~~~~~~~~~~~~~~~

      + CategoryInfo : InvalidArgument: (
    https://ps.outlook.com/powershell/:Uri) [New-PSSession], PSInvalidOperationException

      + FullyQualifiedErrorId : CreateRemoteRunspace,Microsoft.PowerShell.

      Commands.NewPSSessionCommand

     

    Import-PSSession : Cannot validate argument on parameter 'Session'. The

    argument is null. Supply a non-null argument and try the command again.

    At line:4 char:23

    + Import-PSSession $Session -CommandName Remove-mailbox,

    Get-mailbox, New-Mai ...

    + ~~~~~~~~

      + CategoryInfo : InvalidData: (:) [Import-PSSession], ParameterBi

      ndingValidationException

      + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power

      Shell.Commands.ImportPSSessionCommand

     

    get-mailbox : The term 'get-mailbox' 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.

    At line:5 char:6

    + get-mailbox 'vinod' | select-object name | out-null;

    + ~~~~~~~~~~~

      + CategoryInfo : ObjectNotFound: (get-mailbox:String) [], Command

      NotFoundException

      + FullyQualifiedErrorId : CommandNotFoundException

     

    get-mailbox : The term 'get-mailbox' 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.

    At line:10 char:6

    + get-mailbox 'vinod@pesit.onmicrosoft.com' |

    select-object name | out-null;

    + ~~~~~~~~~~~

      + CategoryInfo : ObjectNotFound: (get-mailbox:String) [], Command

      NotFoundException

      + FullyQualifiedErrorId : CommandNotFoundException

     

    New-Mailbox : The term 'New-Mailbox' 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.

    At line:12 char:90

    + ... xit 1;} else { New-Mailbox -Alias 'vinod' -Name 'vinod' -FirstName

    'vinod' -Last ...

    + ~~~~~~~~~~~

      + CategoryInfo : ObjectNotFound: (New-Mailbox:String) [], Command

      NotFoundException

      + FullyQualifiedErrorId : CommandNotFoundException

     

    Remove-PSSession : Cannot validate argument on parameter 'Id'. The argument is

    null. Supply a non-null argument and try the command again.

    At line:13 char:84

    + ... move-PSSession $Session; write-host "Mailbox could not be created. Check

    for pas ...

    + ~~~~~~~~

      + CategoryInfo : InvalidData: (:) [Remove-PSSession], ParameterBi

      ndingValidationException

      + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power

      Shell.Commands.RemovePSSessionCommand

     

    Mailbox could not be created. Check for password or UserPrincipalName.

    ---------------------------------------------------------------------------------------------------------

    • pre requisites which i am using is:

    - Microsoft Online Services Sign-in Assistant

    - Windows Azure Active Directory Module

    -Windows Management Framework 3.0

    -SharePoint Online Management Shell

    -SSH Server (Version 3.0)

    -Set-ExecutionPolicy Unrestricted

    Please help

    • Moved by Bill_Stewart Tuesday, May 2, 2017 6:58 PM User apparently solved own problem
    Wednesday, June 15, 2016 11:02 AM

All replies

  • Hi Paul,

    well, you are using a WMF Version that should no longer be in use. You may want to update, just to be sure. This may be the cause, as it would run for me flawlessly.

    Anyway, looking at your error, it all starts with the New-PSSession call, because it cannot understand the connection uri. Try replacing the weblink with this:

    (new-object system.uri("https://ps.outlook.com/powershell/"))

    Cheers,
    Fred

    Note: This shouldn't really be necessary - it accepts the string for me without an itch, both for v2 and v5 - but to be on the safe side. If it's a bug in the PowerShell system, this eliminates one more step it might hickup upon (it won't have to coerce input types anymore).


    There's no place like 127.0.0.1

    Wednesday, June 15, 2016 12:17 PM
  • $Session = New-PSSession -ConfigurationName Microsoft.Exchange  -ConnectionUri  (new-object system.uri("https://ps.outlook.com/powershell/"))  -Credential $mycreds -Authentication Basic -AllowRedirection -WarningAction SilentlyContinue;

    but same error is coming sir.

    Wednesday, June 15, 2016 12:29 PM
  • Hi Paul,

    in that case your only real choice is ...

    a) Trying to run this from another computer - does it work from that?
    b) Updating your computer to a current state

    I recommend doing b) anyway, as it is only good practice to have a fully patched computer.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Wednesday, June 15, 2016 12:56 PM
  • The following works in all systems with online Exchange.  It is the recommended method:

    New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $myCreds -Authentication Basic –AllowRedirection

    Do not add warningaction.

    This works on any system PS2 and later and does not require any additional modules or software to be installed.


    \_(ツ)_/



    • Edited by jrv Wednesday, June 15, 2016 3:43 PM
    Wednesday, June 15, 2016 3:40 PM
  •  

    now such errors are coming if not adding any warning

    Cause: New-PSSession : A positional parameter cannot be found that accepts argument '–AllowRedirection'. At line:5 char:31 + $Session =New-PSSession -ConfigurationName Microsoft.Exchang ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~

    Thursday, June 16, 2016 4:42 AM
  • Fix the dash. That looks to be en.

    • Proposed as answer by jrv Thursday, June 16, 2016 12:40 PM
    • Unproposed as answer by Bill_Stewart Tuesday, May 2, 2017 6:58 PM
    Thursday, June 16, 2016 12:26 PM
  • Good call Mike:

    PS D:\scripts> [int]('–AllowRedirection'[0])
    8211
    PS D:\scripts> [int]('-')[0]
    45


    \_(ツ)_/

    Thursday, June 16, 2016 12:40 PM
  • Cheers.

    I've gotten to the point where the first thing I look for in any code posted here is smartquotes and em/en dashes. Seems to come up quite often.


    Thursday, June 16, 2016 12:46 PM
  • You have good eyes.

    \_(ツ)_/

    Thursday, June 16, 2016 12:54 PM
  • i changed the dash  now the same problem 

    https://ps.outlook.com/powershell/  this uri is also not working 

    i changed the uri also 

    New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https:// ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (https://outlook...ershell-live id/:Uri) [New-PSSession], PSInvalidOperationException + FullyQualifiedErrorId : CreateRemoteRunspace,Microsoft.PowerShell. Commands.NewPSSessionCommand

    solve the problem boss?


    Thursday, June 23, 2016 1:17 PM
  • I use this almost every day wit no issues. It is the official method:

    $session=New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $o365account -Authentication Basic –AllowRedirection


    \_(ツ)_/

    Thursday, June 23, 2016 2:20 PM
  • This is an easier way to keep track of the arguments:

    #requires -version 3
    $o365=@{ ConfigurationName='Microsoft.Exchange' ConnectionUri='https://ps.outlook.com/powershell/' Credential=$o365account Authentication='Basic' AllowRedirection=$true } $O365Session =New-PSSession @o365 Import-PSSession $O365Session


    \_(ツ)_/


    • Edited by jrv Thursday, June 23, 2016 2:28 PM
    Thursday, June 23, 2016 2:27 PM
  • see i am calling this script from a java ssh connector ,but why that  InvalidArgument: (https://ps.outlook.com/powershell/' is comming

    Friday, June 24, 2016 6:55 AM
  • finally i solve  the problem . the connection uri which i gave finally is 

    https://outlook.office365.com/powershell-liveid/    instead of   https://ps.outlook.com/powershell/  

    and the command is 

     $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $mycreds -Authentication Basic -AllowRedirection

    its working fine and i am able to provision user from my application using java connector.

    Friday, June 24, 2016 10:33 AM
  • finally i solve  the problem . the connection uri which i gave finally is 

    https://outlook.office365.com/powershell-liveid/    instead of   https://ps.outlook.com/powershell/  

    and the command is 

     $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $mycreds -Authentication Basic -AllowRedirection

    its working fine and i am able to provision user from my application using java connector.

    Both Urls will work and should work.  I suspect you have a subtle typo somewhere.


    \_(ツ)_/

    Friday, June 24, 2016 10:48 AM