SiHodgy007
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
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
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.
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}"
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.
If you paste it straight into a command prompt, it should work just fine.
ASKER
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:
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.
@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
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
ASKER
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".
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
ASKER
Yep just using the tools I have been given
ASKER
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
As currently the value x quickly returns a number before the plink script finishes
ASKER
Its returning the processor id
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your commitment in resolving this issue.
Open in new window