Script Not working as a Module

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 {


$RegKeyPath = "HKCU:\Software\Adminscript"
$keyGevonden = $false
$pathfound= $false


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 -Credential $Ocreds -Authentication Basic -AllowRedirection
Import-PSSession $Session -Prefix C -AllowClobber -Verbose | out-null

Connect-MsolService -Credential $Ocreds

return $OCreds


First, it’s a really bad idea to store passwords in plain text.

Second, how exactly are you calling this as a module. Look at this blog to understand how to create and call functions stored in a module:

The Import-PSSession only affects the current scope, it won’t affect the session outside the module scope. You’ll need to instead output the $session variable to the caller and import the session into the global scope after calling the Logon365 function. (For best practices’ sake, I’d also recommend using a Verb-Noun function name with an approved verb; perhaps New-O365Session?)

So you would have a pattern like this, essentially:

New-O365Session | Import-PSSession

Where the New-O365Session command just creates a PSSession for you and returns that session object for you to then import the session into your current scope as the caller.

Hi Rob and Joel,

Thanks champs for your reply .

@Joel Will try what you are explaining, also verb-noun name convention.

@Ron The password is not saved as plain text in the registry and will study the link you are referring to.

Thanks again guys and till my next stupid question :slight_smile: because unfortunately I’m not constantly busy with scriptig.




tried what you told me but I get only errors :frowning: bro.

New-O365Session | Import-PSSession

Do you have another tip for me.

Maybe the thought is wrong and its better to use it as function within the main script.

But on the other hand I think it must be possible to use it as “logon” module.

Greetings from Holland :slight_smile:







You will need to provide a more complete explanation of what you attempted, and any errors you encountered in as full information as you are able to get. I also mentioned you would need to modify your initial function to return the actual session variable instead of the credential; did you make that change as well?

“I get only errors” is missing all of the crucial error information that we can use to debug the problem. :slight_smile: