question

SMRTaheri-1427 avatar image
1 Vote"
SMRTaheri-1427 asked IanXue-MSFT edited

how can I pass secure string parameter with invoke-expression

Hi
I want to pass parameter in another ps1 script and run this, 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

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Evgenij-Smirnov avatar image
1 Vote"
Evgenij-Smirnov answered

Since what you're calling is a PowerShell script rather than a native command, how about using Invoke-Command instead? This way you can pass arguments or utilize the Using: scope...

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

Hi,

You can use the call operator &

 & $myPath $userName $password

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.