简体   繁体   中英

PowerShell Global Variable

I'm trying to create a Function on my module that will create a PsSession and to import it to my console. The script block itself is ok, but after running the cmdlet, I cannot import the PsSession although the function imports it.

Function ConnectTo-Office365 {
[cmdletbinding()]

$365Credential = Get-Credential -Message "Office365 Credentials";
$365Session = New-PSSession -ConfigurationName Microsoft.Exchange ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $365Credential -Authentication  Basic -AllowRedirection;
Import-PSSession $365Session}

I've read on Global Variables but could not understand how to use it to my purpose.

Thanks!

You don't need to use a global variable.

Function ConnectTo-Office365 {
  [cmdletbinding()]
  Param (
    $Credential = (Get-Credential -Message "Office365 Credentials")
  )

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

  Return $Session
}

PS> Import-PSSession (ConnectTo-Office365)

PS> Import-PSSession (ConnectTo-Office356 -Credential $Cred)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM