0

I want to pass parameter in another ps1 file, but I have problem to send parameter

my code:

#loginWindow.ps1

 $userName=$var_txt_username.Text
   [SecureString] $password=ConvertTo-SecureString $var_txt_password.Password -AsPlainText -Force
   $myPath="$path\MainWindow.ps1"
   $window.Close()
   Invoke-Expression "$myPath $userName $password"

 #MainWindow.ps1
 param(
 [string]$userName ,
 [secureString]$password
 )

and my error :

Cannot process argument transformation on parameter 'password'. Cannot convert the "System.Security.SecureString" value of type"System.String" to type "System.Security.SecureString".

Thank you for your help

smrt 98
  • 3
  • 5

1 Answers1

0

The problem here is with Invoke-Expression, a cmdlet you should always try to avoid. See this

You can run your MainWindow script either by dot-sourcing it:

. $myPath $userName $password

or by using the call operator

& $myPath $userName $password
Theo
  • 57,719
  • 8
  • 24
  • 41