I have got a powershell script file which contains the function below. its called my_functions.ps1
function Get-My-PlainText()
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)][System.Security.SecureString]$SecureString
)
BEGIN { }
PROCESS
{
$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString);
try
{
return [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr);
}
finally
{
[Runtime.InteropServices.Marshal]::FreeBSTR($bstr);
}
}
END { }
}
I have got another script (called scriptB.ps1) that is supposed to call this function file before using the functions referenced by the file
scriptB.ps1 calls the function file by running the following inline within the file.
powershell .\my_functions.ps1
Import-Module .\my_functions.ps1
I get the error below.
The term 'Get-My-Plaintext' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is
| correct and try again.
Currently this behaviour happens on powershell terminal running powershell v 7.45, and the only way to fix it is to copy the function and paste it on the terminal.
Is there a way to resolve this problem and avoid the need to copy and paste the functions, its only this function that has this issue.
Thanks in advance.
pwsh.exe. Usingpowershellwill invoke Windows PowerShell; probably 5.1.()after function name while including aparam (...)section. You are defining both 0 parameters and one parameter at the same time.. .\my_functions.ps1, the commandGet-Command -Name Get-My-PlainTextshows that it is loaded .Import-Module .\SomeScript.ps1- while ill-advised for conceptual reasons alone, given that a.ps1file is just a script, not a module - is in effect the same as. .\SomeScript.ps1(dot-sourcing), which is what you need. (Adding-Verboseto theImport-Modulecall shows this.) However, a side effect of usingImport-Moduleis that a pseudo-moduleSomeScriptis then reported byGet-Module. Also, repeating theImport-Modulecall in the same session is a quiet no-op - even if the script's content has changed; reloading requires-Force.Get-My-PlainTextshould beGet-MyPlainText; a simpler version of your function body would be[pscredential]::new('unused', $SecureString).GetNetworkCredential().Password