Using Class

Welcome Forums General PowerShell Q&A Using Class

This topic contains 7 replies, has 3 voices, and was last updated by

 
Participant
3 weeks, 3 days ago.

  • Author
    Posts
  • #112567

    Participant
    Points: 0
    Rank: Member

    Hi Everyone,

    I have this Log Class in a .psm1 file, in the 'Bin\Log' folder:

    c:\Program\Bin\Log\Log.psm1:

    Class Log
    {
    	LogToFile()
    	{
    		
    	}
    }

    Another class in a .psm1 file, in the 'Bin\Mycar' folder.

    c:\Program\Bin\Car\MyCar.psm1:

    using module c:\Program\Bin\Log\Log.psm1
    Class MyCar
    {
    	DoStuff()
    	{
    		[Log]::DoStuff()
    	}
    }

    And the PS1 file to wrap up everything.

    c:\Program\start.ps1

    using module c:\Program\Bin\Car\MyCar.psm1
    using module c:\Program\Bin\Log\Log.psm1
    [Log]::DoStuff()
    [MyCar]::DoStuff()
    

    Importing the classes from the PSM1 files with the "using module" command in the PS1 file works just fine, but if I want to use the Log Class within the MyCar class, it doesn't work.

    What am I doing wrong ?

    Thanks in advance.
    Nuno

  • #112582

    Participant
    Points: 1
    Rank: Member

    It's a bit tricky to do this with the classes in separate files. Typically what I see done is that when someone builds a module, the classes/enums/etc are often all compiled into a single file during the build step of their publishing pipeline.

    This helps to circumvent the weird issues with classes like this.

    • #112585

      Participant
      Points: 0
      Rank: Member

      All the imported files from the ps1 start script work fine... The problem is when I use a relative path from another psm1.

      I have a psm1 file with a Log class which I need to use everywhere.

      How am I supposed to use the class.

       

       

  • #112660

    Participant
    Points: 20
    Rank: Member

    You log class doesn't have a DoStuff method defined.

    Try inheritance.

    using module c:\Program\Bin\Log\Log.psm1
    Class MyCar:Log
    {
    	DoStuff()
    	{
    		$this.LogToFile()
    	}
    }
    
  • #112666

    Participant
    Points: 1
    Rank: Member

    It's not so much that OP wants one class to inherit another. They want one class to return an object type of the other as the returned object from a method... if I'm understanding the intention here.

    If you need a relative path, I'd make use of $PSScriptRoot; relative paths are fragile and easily break.

     

    • #112667

      Participant
      Points: 0
      Rank: Member

      I have already tried the $psroot. It does not work in the using module. ☹️

      I only need to log all errors from all my classes, which are in separated files in different folders. Using the full path for the log class file work. But if I move the script to another computer/location I have to change all references......

  • #112670

    Participant
    Points: 1
    Rank: Member

    Put the path in quotes?

    using module "$PSScriptRoot\ClassFile.psm1"
    • #112672

      Participant
      Points: 0
      Rank: Member

      [Powershell] "$PSScriptRoot\Modules\Log\RooxLog.psm1" is not a valid value for using name.

You must be logged in to reply to this topic.