Powershell modules are extension to inbuilt cmdlets. You may download third-party modules or you can write them using C#. Along with some installation, modules are installed in Powershell.
List the modules available:
Use get-module to list the modules.
Import an existing module:
import-module <module_name>
Writing your own module with Powershell
You can write your own module in Powershell using below steps:
1. Create a file with below content:
function do-nothing()
{
echo "You are in nothing"
}
function do-something( $i, $j)
{
echo "Result : $($i + $j)"
}
2. Save the file with .psm1 extension. I use test.psm1 as filename.
3. Import the module
import-module c:\tst\test.psm1
4. Check if module is loaded
PS D:\Org\Powershell> Get-Module
ModuleType Name ExportedCommands
---------- ---- ----------------
Script test {do-something, do-nothing}
5. Run the command used
PS D:\Org\Powershell> do-something 5 6
Result : 11
CONCLUSION
Overall, we have below advantages using this approach:
1. Increases code manageability
Any changes made in one module is copied over all the modules.
2. Increases code reusability :
You can use common tasks like sending mails or logging in one module and share in all the scripts.
3. Team work
If a team of people working, working in modules reduces burden on single person and everyone owns his own part.
List the modules available:
Use get-module to list the modules.
Import an existing module:
import-module <module_name>
Writing your own module with Powershell
You can write your own module in Powershell using below steps:
1. Create a file with below content:
function do-nothing()
{
echo "You are in nothing"
}
function do-something( $i, $j)
{
echo "Result : $($i + $j)"
}
2. Save the file with .psm1 extension. I use test.psm1 as filename.
3. Import the module
import-module c:\tst\test.psm1
4. Check if module is loaded
PS D:\Org\Powershell> Get-Module
ModuleType Name ExportedCommands
---------- ---- ----------------
Script test {do-something, do-nothing}
5. Run the command used
PS D:\Org\Powershell> do-something 5 6
Result : 11
CONCLUSION
Overall, we have below advantages using this approach:
1. Increases code manageability
Any changes made in one module is copied over all the modules.
2. Increases code reusability :
You can use common tasks like sending mails or logging in one module and share in all the scripts.
3. Team work
If a team of people working, working in modules reduces burden on single person and everyone owns his own part.
No comments:
Post a Comment