Forum Discussion

John_Dodo's avatar
John_Dodo
Brass Contributor
Oct 19, 2022
Solved

string not properly interpreted when calling a script

 Hello,

 

 

in a script I have to call psexec to execute a remote script on a remote target (I could probably do an invoke... but I didn't manage to make this work yet so for the moment I need to do it this way).

...
.\psexec.exe "\\$targetserver" -accepteula powershell '"$pathtomyscript"'+$local_rep+'\'+$filename+'" "C:\_Scripts\_Logs"'
...

 

When I run this no variable is interpreted. You can notice than the parameters block of the call to powershell is encapsulated in single quotes ' ' . You can also notice I tried to insert the variables in two different ways.

If I simply select this : '"$pathtomyscript "view-pool-gbl" "noVM" "noref" "'+$local_rep+'\'+$filename+'" "C:\_Scripts\_Logs"' then powershell interpretes it properly and resolvs the variables, but as soon as it's part of the call to psexec then it's not interpreted.

 

I have tried many other ways such as $var1+"text" etc but nothing seems to work.

 

Does anyone know how to make this work ?

 

Thank you.

 

O

 

 

  • John_Dodo 

     

    I'm a little unclear on what the command should look like but the following three variables will be resolved within your existing script block, i.e. on the host doing the calling:

     

    • $targetserver
    • $local_rep
    • $filename

     

    The fourth "variable", $pathtomyscript, will not be resolved caller-side. Instead, it will be passed into remotely-initiated PowerShell call where that remote PowerShell session will try to parse $pathtomyscript - which will fail (or more correctly, it will resolve to $null.)

     

    If you have defined $pathtomyscript within the caller-side script block then there's multiple ways of dealing with the string parsing, but perhaps the closest to what you already have would look like this:

     

    .\psexec.exe "\\$targetserver" -accepteula powershell "`"$pathtomyscript$local_rep\$filename`"" "C:\_Scripts\_Logs"

     

    Cheers,

    Lain

2 Replies

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    John_Dodo 

     

    I'm a little unclear on what the command should look like but the following three variables will be resolved within your existing script block, i.e. on the host doing the calling:

     

    • $targetserver
    • $local_rep
    • $filename

     

    The fourth "variable", $pathtomyscript, will not be resolved caller-side. Instead, it will be passed into remotely-initiated PowerShell call where that remote PowerShell session will try to parse $pathtomyscript - which will fail (or more correctly, it will resolve to $null.)

     

    If you have defined $pathtomyscript within the caller-side script block then there's multiple ways of dealing with the string parsing, but perhaps the closest to what you already have would look like this:

     

    .\psexec.exe "\\$targetserver" -accepteula powershell "`"$pathtomyscript$local_rep\$filename`"" "C:\_Scripts\_Logs"

     

    Cheers,

    Lain

    • John_Dodo's avatar
      John_Dodo
      Brass Contributor

      LainRobertson thank you so much ! It works thanks to you.

       

      Next step is to get rid of psexec but for the moment it will help a lot.

Resources