Module to Connect to Remote PS Enviroments

This topic contains 9 replies, has 4 voices, and was last updated by  Dan Potter 1 year, 2 months ago.

  • Author
    Posts
  • #44118

    Jeff Hayes
    Participant

    So I created a Module to connect to my Office 365 Remote PS, and two on-perm PS environments.

    I have loaded the Module and ran the three cmdlets that I use to run out of my powershell profile. However now cmdlets like Get-Mailbox are showing up after I connect to the remote PS Environment.

    Here is part of the module.

    
    Function Connect-Office365
    {
    [CmdletBinding()]
    param
    (
    [Parameter(Mandatory = $False)]
    [System.Management.Automation.PsCredential]$Credential = $Host.UI.PromptForCredential("Enter MSOL Admin Credential",
    "Enter the username and password of an MSOnline Administrator account.","","userCreds"),
    [Parameter(Mandatory = $False)]
    [System.Uri]$Uri = "https://ps.outlook.com/PowerShell-LiveID?PSVersion=5.0.10514.6"
    )
    connect-msolservice -credential $credential
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $Uri -Credential $Credential -Authentication "Basic" -AllowRedirection
    Import-PSSession $session -prefix EXO -AllowClobber
    
    
    Return $session
    }
    
    • This topic was modified 1 year, 5 months ago by  Jeff Hayes.
  • #44126

    Don Jones
    Keymaster

    I'm confused – you don't want Get-Mailbox to show up?

  • #44132

    Jeff Hayes
    Participant

    No they are not showing up. Whenever I try to run something that I normally would have ran before when I just had this as a profile script I get an error telling me it's not recognized.

    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.

  • #44138

    Don Jones
    Keymaster

    You applied a prefix to the imported commands. It'd be Get-ExoMailbox.

  • #44142

    Jeff Hayes
    Participant

    I still get the error when I try it with the prefix. The Moduletype is a Script. I'm not sure if this would matter?

  • #44146

    Don Jones
    Keymaster

    No. Imported modules are always scripts – it's just a set of proxy functions, not the "real" functions.

    So here's the thing I think you're running into: scope. Because you're creating the PSSession within a function, isn't it likely that the PSSession ceases to exist after the function runs? You can test that by running the function and then running Get-PSSession to see what sessions are available and what their status is. if the session isn't opened and available, then the proxy module can't do its job.

  • #44148

    Jeff Hayes
    Participant

    I was wondering about that myself but I do see the PSconnection.

    PS H:\IT-Share> Get-PSSession | fl
    
    
    ComputerName           : ps.outlook.com
    ConfigurationName      : Microsoft.Exchange
    InstanceId             : daf95a44-9417-46e7-8b37-b76363d8fe57
    Id                     : 3
    Name                   : Session3
    Availability           : Available
    ApplicationPrivateData : {SupportedVersions, ImplicitRemoting, PSVersionTable}
    Runspace               : System.Management.Automation.RemoteRunspace
    State                  : Opened
    IdleTimeout            : 900000
    OutputBufferingMode    : None
    DisconnectedOn         : 
    ExpiresOn              : 
    
    
  • #44150

    Don Jones
    Keymaster

    Then... not sure. I'd probably try to manually load the proxy module, and look inside it (in the ISE) to make sure it contained the commands I thought it did.

  • #53742

    Raymond van Eupen
    Participant

    I also use this script for using Exchange on-premises and Exchange Online command in 1 powershell. This script is also found here

    What i would like to add to this script is automatic logging in to the Exchange Online envrionment.

    As my powershell scripting knowledge is not very good i am not sure how to add this.

    If i where to add to variable as such:
    $username="account@domain.onmicrosoft.com"
    $password=(ConvertTo-SecureString -String "Password" -AsPlainText -Force)

    What has to be adjusted to this command to get it to automatically log in:
    [System.Management.Automation.PsCredential]$Credential = $Host.UI.PromptForCredential("Enter MSOL Admin Credential",
    "Enter the username and password of an MSOnline Administrator account.","","userCreds"),

  • #53747

    Dan Potter
    Participant

    get-credential | export-clixml ohthreesixtyfive.xml

    $creds = import-clixml ohthreesixtyfive.xml

    Connect-MsolService -Credential $creds

You must be logged in to reply to this topic.