1

I have a file called file.ps1 with the following code:

$varsent=$args[0]
echo $varsent

when I tried to call this ps1 file with the following command because I need execute ps1 with elevated rights:

$var = "test"
Start-Process powershell -ArgumentList '-noprofile -file "\\network path with spaces\file.ps1" $var ' -verb RunAs

Result is the word "$var" instead "test"

But with

Start-Process powershell -ArgumentList '-noprofile -file "\\network path with spaces\file.ps1" test ' -verb RunAs

result is word "test"

I think that problem is that $var is not replaced by its value when code is executed but I do not why. Please, can you help me? I must use $var.

JIOB
  • 13
  • 3

1 Answers1

3

It's because $var is not interpreted inside single quotes strings, try :

$var = "test"
Start-Process powershell -ArgumentList "-noprofile -file \\path\file.ps1 $var " -verb RunAs

With spaces :

Start-Process powershell -ArgumentList "-noprofile -file `"C:\temp\Un petit test.ps1`" $var" -verb RunAs

``" allow you to use " inside "".

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • Thanks a lot for you reply @JPBlanc. Now a new windows powershell ISE is opened and closed inmediately after enter credentials. Would be because \\path\file.ps1 include spaces and was inside single quotes? I edited my question. I also tried to add a new variable with the path, but it seems that argument list only works with double quotes :( `$var = "test" $Ps1Path = "\\network path with spaces\file.ps1" Start-Process powershell -ArgumentList "-noprofile -file $Ps1Path $var " -verb RunAs` – JIOB May 09 '20 at 10:10
  • If you've got spaces in your file path you have to keep using "", I edit my answer. – JPBlanc May 09 '20 at 10:25
  • I LOVE YOU @JPBlanc. Will you marry me? Thanks, thanks, thanks and **thanks again**!!! – JIOB May 09 '20 at 11:10