I have imported a custom powershell module(.pms1 file extension) in a powershell script(.ps1 file extension), but I am getting a the term is not recognised as the name of a cmdlet, function, script file, or operable program. Check the spelling of name, or if a path was included, verify that the path is correct and try again
error.
What could I be missing?
SetUpServer.ps1(powershell script):
$CurrentDirectory=Get-Location
Import-Module -Name $CurrentDirectory\RemoveAddAppPool.psm1
RemoveAddAppPool 'AppPoolName'
RemoveAddAppPool.psm1(PowerShell module)
Function RemoveAddAppPool
{
[CmdletBinding()]
param
(
[String] $AppPoolName
)
{
Import-Module webadministration
Import-Module ActiveDirectory
..................
}
}
Export-ModuleMember -Function * -Alias * -Variable *
Update 1: Complete error message:
Call to AddUpdateAppPool module(line 110):
CodePudding user response:
In the end, I just went ahead with creating a powershell script(instead of modularing it) and calling it from the main powershell script.
& "$PSScriptRoot\RemoveAppPool.ps1" -AppPoolName 'SomeAppPoolName'
& "$PSScriptRoot\AddAppPool.ps1" -AppPoolName 'SomeAppPoolName'
Thank you @mklement0 for your inputs.
CodePudding user response:
Basically put your file RemoveAddAppPool.psm1
inside a folder called RemoveAddAppPool
. Then import just the folder: Import-Module -Name $CurrentDirectory\RemoveAddAppPool
You can see an example in the docs
Alternatively, you can (and I personally) rely on powershell to find my module via $env:PSModulePath
- to install a module just copy your file into a folder thats included in the PSModulePath
list.
Below script illustrates the bare minimum you need to do to make a module work in this way. You can adapt this to your needs.
#change this:
$ModuleName = 'SomeNewModule';
#grab first folder in module path - usually inside your users home dir
$OutputDir = ($env:PSModulePath).Split(';')[0]
Write-Host "Using $OutputDir";
$ModuleDir = "$($outputDir)\$($ModuleName)"
$ModuleFilename = "$($ModuleDir )\$($ModuleName).psm1"
New-Item -ItemType Directory $ModuleDir
#change this - copy or download your own psm1 file here instead...
Set-Content -Path $ModuleFilename -Value "function New-Something(){Write-Host `"Im a modules function`"}`nExport-ModuleMember -Function New-Something;"
write-Host "Created $ModuleFilename";
# import the module by just name - powershell looks for directory inside each $env:PSModulePath folder
Import-Module SomeNewModule;
#Call your function
New-Something
(Delete the folder SomeNewModule
and its contents inside $OutputDir to "uninstall" the SomeNewModule
module)
Obviously dont use Set-Content
- id suggest either Move-Item/Copy-Item if the file is already on the server, or downloading it with Invoke-WebRequest
if its on a web server etc. e.g.
Invoke-WebRequest -Uri https://example.com/SomeNewModule.psm1.txt -OutFile "$($ModuleDir)\$($ModuleName).psm1" -UseBasicParsing