简体   繁体   中英

Escaping bash code sequence inside a powershell script

I need to connect from PowerShell to a Linux machine, get some folders that match a name and afterwards delete them (if you're asking why, cleanup for a test environment).

To accomplish this, I'm using the SSH.NET library (details here ) and my script looks like this so far:

New-SshSession -ComputerName UbuntuMachine -Username root -Password test1234
Invoke-SshCommand -InvokeOnAll -Command {\
        cd /dev;\
        shopt -s nullglob;\
        backupsToDelete=(Nostalgia*);\
        printf "%s\n" "${backupsToDelete[@]}";\
        for i in "${backupsToDelete[@]}"\
        do\
            printf $i\
        done}

Everything works fine until I reach the point where I need to loop through my backupsToDelete array. It seems that for some reason, PowerShell is treating the for loop as it's own statement and not a bash one, everything resulting in an error:

At C:\Users\Administrator\Desktop\CleanupLinux.ps1:7 char:6
+         for i in "${backupsToDelete[@]}"\
+            ~
Missing opening '(' after keyword 'for'.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword

Is there any way to tell PowerShell to not execute these kind of statements as it's own? Or maybe a different approach?

$command = "@
{\
cd /dev;\
shopt -s nullglob;\
backupsToDelete=(Nostalgia*);\
printf "%s\n" "${backupsToDelete[@]}";\
for i in "${backupsToDelete[@]}"\
do\
   printf $i\
done}
@"

New-SshSession -ComputerName UbuntuMachine -Username root -Password test1234
Invoke-SshCommand -InvokeOnAll -Command $command

try like so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM