plink in PowerShell to run commands on Linux machine (SuSE)

1.2k views Asked by At

I am trying to automate the process of adding extra storage in a linux machine. I'm using plink in PowerShell installed on my Windows machine.

Below is the code:

$plinkpath = "C:\Users\mydrive\Modules\plink.exe"
if (Test-Path $plinkpath) {
    Set-Alias plink $plinkpath
} else {
    throw "Plink.exe is reqruied"
}
$passw = "linuxadmin$123"
$commands = @(
    "sudo su;",
    "pvcreate /dev/sde;",
    "vgcreate test_vog /dev/sde",
    "lvcreate -l 100%FREE -n test_lev test_vog;",
    "mkfs.ext3 /dev/test_vog/test_lev;",
    "mkdir /azurenew;",
    "echo ""/dev/test_vog/test_lev /azurenew/ ext3 defaults 1 1"" >> /etc/fstab;",
    "mount /azurenew/;" 
)

Approach 1: Using .ppk file

plink -ssh -i "C:\Users\amurthy\Documents\WindowsPowerShell\Modules\sshprivate.ppk" [email protected] $commands

In the above situation PowerShell hangs and no response on the console. Not sure what's happening.

Approach 2: using direct log in

plink -P "22" -v "[email protected]" -pw "linuxadmin$123" $commands

Here, I get below response on console

Using username "linuxadmin". Sent password Password authentication failed

I do not understand why the passoword authentication failed though I am able to login using putty.exe with that password.

Can anyone please help me here to solve my above automation problem? If you have any better solution altogether really welcome.

1

There are 1 answers

5
Ansgar Wiechers On

The password login attempt fails because you defined the password in a double-quoted string. PowerShell tries to expand the (undefined) variable $123 in linuxadmin$123, so you're actually passing just linuxadmin as the password. You could use a single-quoted string to avoid this, but public key authentication (your first approach) is the better approach anyway, so I recommend sticking with that.

I'm not sure why your first approach causes the console to hang, though. If the key were password-protected you should be prompted for the password. There's a semicolon missing at the end of "vgcreate test_vog /dev/sde", but if that caused an issue I'd expect to see an error message.

Try running plink with the parameter -v (verbose messages) to get a better picture of what's going on.