Link to home
Start Free TrialLog in
Avatar of SiHodgy007
SiHodgy007Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Passing Credentials into a command line

Powershell Help

I would like to use powershell credential manage to prompt for the username and password and pass it straight through to a plink command, preferable in a one line powershell command as am calling from excel.

Cmd = "plink -ssh amln -l UserName -pw Password command"

Thanks
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

$username= Read-host "What's your username?";
$password = Read-host "What's your password?";
if ($username -ne "" -or $password -ne "") 
{
    $Cmd = "plink -ssh amln -l $($username) -pw $($password) command";
    Write-Host $Cmd;
}

Open in new window

Avatar of oBdA
oBdA

Note that if plink.exe is not in the Path, you'll need to specify the full path to the exe.
The script does its best to handle password with problematic characters in it, but some combinations might still cause problems.
powershell.exe -Command "Get-Credential -Message 'Plink Authentication' | % {& plink.exe -ssh amln -l $_.UserName -pw ('\"' + $_.GetNetworkCredential().Password + '\"') command}"

Open in new window

Avatar of SiHodgy007

ASKER

This is good but I can't run it from a batch file as errors
Don't put that into a batch file; you wanted to run it straight from Excel, so do so.
If you paste it straight into a command prompt, it should work just fine.
I needs to be run from a command shell as that is how the excel VBA pulls it in the macro, if I paste it straight in it just highlight is red meaning lots of syntax errors.
Then save this as Whatever.cmd:
@PowerShell.exe -Command "Invoke-Expression -Command ((Get-Content -Path '%~f0' | Select-Object -Skip 2) -join [environment]::NewLine)"
@exit /b %Errorlevel%
## Powershell only below this line!
$Credential = Get-Credential -Message 'Plink Authentication'
& C:\Temp\echoargs.exe -ssh amln -l $Credential.UserName -pw ('"' + $Credential.GetNetworkCredential().Password + '"') command

Open in new window


Or use it like this in VBA; it currently keeps the window open so you can see potential errors. Remove the
; Read-Host 'Enter to close'
to close the window once done.
Sub Button1_Click()
    x = Shell("powershell.exe -Command ""Get-Credential -Message 'plink' | % {& plink.exe -ssh amln -l $_.UserName -pw ('\""' + $_.GetNetworkCredential().Password + '\""') command}; Read-Host 'Enter to close'""", vbNormalFocus)
End Sub

Open in new window

Hi

When running the vba command it fails when passing the message had to use ''Plink'' double single quotes. When I pass the credential now it returns a username with \ before it so  "\user".
Powershell 2.0? From 2009? Really? Powershell 5.0 was released and still supports Windows 7 / Server 2008 R2.
Sub Button1_Click()
    x = Shell("powershell.exe -Command ""Get-Credential | % {& echoargs.exe -ssh amln -l $_.UserName.Trim('\') -pw ('\""' + $_.GetNetworkCredential().Password + '\""') command}; Read-Host 'Enter to close'""", vbNormalFocus)
End Sub

Open in new window

Yep just using the tools I have been given
Ok that works great, now I need to read the return back into Excel, I would normally add .StdOut.ReadAll to the end and I would have the result assigned to a variable. Any idea?

As currently the value x quickly returns a number before the plink script finishes
Its returning the processor id
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your commitment in resolving this issue.