Got a little script what I wrote to logon on 365 and write the credentials in the registry.
It works perfectly when its been executes as normal script (PS1) only not called as module.
Dont get any error messages.
So not working as PM1 but as PS1 no problem.
I guess it has to do something with misunderstanding at my site.
I expect when calling a modules to logon (in this case o365) the session will be available like running its as a normal script.
So I guess by calling the module it creates a session but its not usable within the main session from where its been called. So its better to change it and use it as procedure.
Please help me understand it.
function Logon365 { [CmdletBinding()] param() $RegKeyPath = "HKCU:\Software\Adminscript" $keyGevonden = $false $pathfound= $false $ValueDataRegex="ID" If (!(Test-Path $RegKeyPath)) { write-host "Registry Not existing, will created" md $RegKeyPath } $RegPathItem = Get-Item $RegKeyPath If (($RegPathItem.GetValue("IDO365", $Null) -eq $Null) -or ($RegPathItem.GetValue("PWO365", $Null) -eq $Null)) { $OCreds = Get-Credential -Message "Enter your o365 credentials" $Account = $OCreds.UserName | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString Set-ItemProperty -Path $RegKeyPath -Name "IDO365" -Value $Account $Wachtwoord = $OCreds.Password | ConvertFrom-SecureString Set-ItemProperty -Path $RegKeyPath -Name "PWO365" -Value $Wachtwoord } Else { $Account = Get-ItemPropertyValue -Path $RegKeyPath -Name "IDO365" $Wachtwoord = Get-ItemPropertyValue -Path $RegKeyPath -Name "PWO365" $SecAccount = $Account | ConvertTo-SecureString $BstrAccount = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecAccount) $CredAccount = [Runtime.InteropServices.Marshal]::PtrToStringAuto($BstrAccount) $CredWachtwoord = $Wachtwoord | ConvertTo-SecureString $Global:OCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $CredAccount, $CredWachtwoord } $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Ocreds -Authentication Basic -AllowRedirection Import-PSSession $Session -Prefix C -AllowClobber -Verbose | out-null Connect-MsolService -Credential $Ocreds return $OCreds }