N
N
NewDevLab2020-04-16 10:35:13
PowerShell
NewDevLab, 2020-04-16 10:35:13

Turning one ps1 into another?

Who connects files without installing packages?
The most versatile thing I've done is

.(Join-Path -Path (Split-Path $script:MyInvocation.MyCommand.Path -Parent) -ChildPath ".\any.ps1")

I would like to
.(Join-Path -Path (Split-Path $script:MyInvocation.MyCommand.Path -Parent) -ChildPath ".\PS.Get-Include.ps1")
Get-Include ".\any1.ps1"
...
Get-Include ".\any2.ps1"

but scope seems to suffer from such an inclusion.
Maybe there is a default package for this behavior or what?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxKozlov, 2020-04-17
@NewDevLab

Frequently used functions can be folded into a module
Structure example:

MyUtils/
  MyUtils.psd1
  MyUtils.psm1
  functions/
    Get-Something.ps1
    Get-SomethingOther.ps1

The contents of MyUtils.psd1 , the minimum option. only completed fields
@{
  ModuleVersion = '1.0'
  GUID = '72d739dd-bddf-4d7c-a358-1a40e2ff961d'
  Description = 'MyUtils module'
  NestedModules = @('MyUtils.psm1')
}

Guid must be generated via [guid]::NewGuid()
Content of MyUtils.psm1
Try {
    Get-ChildItem "$PSScriptRoot\functions\*.ps1" -Exclude *.tests.ps1, *profile.ps1 |
    ForEach-Object {
      $Function = $_.Name
      . $_.FullName
    }
} Catch {
    Write-Warning ("{0}: {1}" -f $Function,$_.Exception.Message)
    Continue
}

An example of the content of one of the functions
function Get-Something {
param(
  $parameter
)
  Write-Host "I'm Get-Something with $parameter"
}

All additional functions can be developed separately, and then simply transferred to the MyUtils\functions folder.
and they will connect to the module themselves when it is reloaded together with powershell or
via Import-Module MyUtils
-Force

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question