I'm building a custom module in Powershell to factorize some code.
In the functions in the module, I use variables. However, if the caller use the same variable names, it can interfer with my module.
For example, here a small module (MyModule.psm1) :
function Get-Foo{
param(
[int]$x,
[int]$y
)
try{
$result = $x/$y
} catch{
Write-Warning "Something get wrong"
}
if($result -ne 0){
Write-Host "x/y = $result"
}
}
Export-ModuleMember -Function "Get-Foo"
And a sample script that use the module:
Import-Module "$PSScriptRoot\MyModule\MyModule.psm1" -Force
$result = 3 # some other computation
Get-Foo -x 42 -Y 0
The output is :
x/y = 3
As you can see, the caller declared a variable name that conflicts with the one in my module.
What is the best practice to avoid this behavior ?
As a requirement, I have to assume that the module's developer won't be the main script developer. Thus, the internal on the module is not supposed to be known (kinda black box)