Script Not working as a Module

Welcome Forums General PowerShell Q&A Script Not working as a Module

Viewing 5 reply threads
  • Author
    Posts
    • #189340
      Participant
      Topics: 1
      Replies: 2
      Points: 17
      Rank: Member

      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
      
      }
      
      
    • #189376
      Participant
      Topics: 2
      Replies: 510
      Points: 1,301
      Helping Hand
      Rank: Community Hero

      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.

    • #189373
      Participant
      Topics: 8
      Replies: 1274
      Points: 1,033
      Helping Hand
      Rank: Community Hero

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

      How to Encrypt Passwords in PowerShell

      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:

      https://powershellexplained.com/2017-05-27-Powershell-module-building-basics/
      https://www.pdq.com/blog/secure-password-with-powershell-encrypting-credentials-part-1/

    • #189493
      Participant
      Topics: 1
      Replies: 2
      Points: 17
      Rank: Member

      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 🙂  because unfortunately I'm not constantly busy with scriptig.

       

       

    • #189775
      Participant
      Topics: 1
      Replies: 2
      Points: 17
      Rank: Member

      Joel,

      tried what you told me but I get only errors 🙁  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 🙂

       

       

       

       

       

       

    • #189799
      Participant
      Topics: 2
      Replies: 510
      Points: 1,301
      Helping Hand
      Rank: Community Hero

      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. 🙂

       

Viewing 5 reply threads
  • You must be logged in to reply to this topic.