You can write PowerShell script and save in separate file for reuse in different situations. Lets first create the module.
Create a file with extension .psm1, and place a sample function like this:
function Get-TestMessage()
{
Write-Host("Get-TestMessage function is involed")
}
Now we have a TestModule.psm1 file with one function Get-TestMessage(). Place this module either in one of the PowerShell default module folders that you can query by this special variable $env:PSModulePath. Or also you can place anywhere on your disk, but in this case you have to define the complete file path for the module while importing.
Now, lets create another module, lets say WorkingModule.psm1. Here we want to use a function from the TestModule. We have to import TestModule by this cmdlet:
Import-Module "C:\PATH_TO_YOUR_MODULES_FOLDER\TestModulePSM.psm1"
And now we can simply call our function and it works.
Get-TestMessage
You can find the available functions defined within a module by passing the module name in -module parameter for Get-Command cmdlet.
Get-Command -Module TestModule
In our case it will only return one function.
Once you finished work with a module, you can remove that module by simply call this cmdlet.
Remove-Module TestModulePSM
If this article helps you then please post your valuable comments.
Create a file with extension .psm1, and place a sample function like this:
function Get-TestMessage()
{
Write-Host("Get-TestMessage function is involed")
}
Now we have a TestModule.psm1 file with one function Get-TestMessage(). Place this module either in one of the PowerShell default module folders that you can query by this special variable $env:PSModulePath. Or also you can place anywhere on your disk, but in this case you have to define the complete file path for the module while importing.
Now, lets create another module, lets say WorkingModule.psm1. Here we want to use a function from the TestModule. We have to import TestModule by this cmdlet:
Import-Module "C:\PATH_TO_YOUR_MODULES_FOLDER\TestModulePSM.psm1"
And now we can simply call our function and it works.
Get-TestMessage
You can find the available functions defined within a module by passing the module name in -module parameter for Get-Command cmdlet.
Get-Command -Module TestModule
In our case it will only return one function.
Once you finished work with a module, you can remove that module by simply call this cmdlet.
Remove-Module TestModulePSM
If this article helps you then please post your valuable comments.
No comments:
Post a Comment