3

I have this which works (triple double quotes are not a mistake; it works exactly like this)—concentrate on the Start-Process part:

Start-Process powershell -Credential $cred -WorkingDirectory "$lettre_disque" -ArgumentList '-noprofile -command &{Start-Process """D:\WD Drive Unlock.exe""" -verb runas}'

Now I'm simply trying to replace the string D:\WD Drive Unlock.exe with a variable named $chemin_fichier which contains the exact same text (D:\WD Drive Unlock.exe).

I'm trying:

"""$chemin_fichier"""

""$chemin_fichier""

"$chemin_fichier"

""""$chemin_fichier""""

Nothing works...

How do I get that variable to show up? This command is pretty confusing. What is the right combination? Do I have to escape something or?

3
  • -ArgumentList ('-noprofile -command &{Start-Process """D:\WD Drive Unlock.exe""" -verb runas}' -f $chemin_fichier) Commented Dec 14, 2017 at 18:38
  • My aim is to remove D:\WD...since the letter is variable, the correct path is stored in $chemin_fichier, how do i place $chemin_fichier so it replaces that string? Like : -ArgumentList ('-noprofile -command &{Start-Process """$chemin_fichier""" Commented Dec 14, 2017 at 18:43
  • 1
    Woops...didn't finish editing it: -ArgumentList ('-noprofile -command &{Start-Process """{0}""" -verb runas}' -f $chemin_fichier) Commented Dec 14, 2017 at 18:50

2 Answers 2

1

I'd break your command into pieces using splatting:

$chemin_fichier = 'D:\WD Drive Unlock.exe'

$ArgList = @{
    FilePath = 'powershell'
    Credential = $cred
    WorkingDirectory = $lettre_disque
    ArgumentList = @(
        '-NoProfile'
        '-Command'
        "`"Start-Process -FilePath '$chemin_fichier' -Verb runas`""
    )
}
Start-Process @ArgList

Wrapping your command in a script block was unnecessary.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, yes it was a nightmare working with the one line command. That syntax is hardcore though haha. I'll get used to it one day, MUCH appreciated.
@Rakha Yeah, working with Start-Process arguments is rough at first.
1

I use a command with parameters like this, which will replace store:Schema="abcd" with an empty string. It's critical to escape " to "" or """", and you should give it a try. Test and modify to ensure the script works:

powershell -Command "$varStr='store:Schema=""abcd""""'; $filePath='%~dp0SomeFolder\SomeFile.txt'; (gc $filePath) -replace $varStr, '' | Out-File $filePath"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.