How to use logon module modulair.

Hi Powershell brothers.

Please help to understand how to accomplish the following.

I wrote a script to logon in o365 and save encrypted the ID and password in the registry.

Now I want to use it as module (function) but I dont know how to do this.

I know there is a such thing as import-pssesion, tried to use this but result was negative.

So again the intention is to use new-logon365 modulair so I can use it within every script to accomplish tasks withing Office365. 

Guys help me to understand how to do this

The code :  

function Logon365 {


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


If (!(Test-Path $RegKeyPath)) {
write-host "Registry Bestaat Niet, wordt aangemaakt"
md $RegKeyPath

$RegPathItem = Get-Item $RegKeyPath

If (($RegPathItem.GetValue("IDO365", $Null) -eq $Null) -or ($RegPathItem.GetValue("PWO365", $Null) -eq $Null)) {
$OCreds = Get-Credential -Message "Geef je credentials voor O365"

$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



No Data
  • As for 'Now I want to use it as module (function) but I don't know how to do this.', it really does not have to be. You can just Dot Source the script. You can rename the script as a .psm1, put it in a folder of the same name and put it in your module path and import as you would any other.

No Data