I saw a solution to cache a server host key by adding the command just below on the top of my script before running the other Plink tasks.
& "echo y | C:\Program Files\PuTTY\plink.exe" -ssh -batch -i $PrivateKeyPath $username "exit" 2>&1
Error message:
The term 'echoy| C:\ProgramFiles\PuTTY\plink.exe' is not recognized as the name of a cmdlet,...
So the spaces prior the pipe symbol are removed and therefore the command is not recognized.
My other attempt is to use Start-Process but I do not know how I am able to prepend echo y | to the plink.exe path.
Start-Process -FilePath 'C:\Program Files\PuTTY\plink.exe' -Argumentlist "-ssh -batch -i $PrivateKeyPath $username $command 2>&1"
Is there an option to prepend something to the -FilePath?
Do not blindly answer "y" to Plink host key verification prompt. You lose a protection against man-in-the-middle attacks.
You should use the -hostkey switch with your host key fingerprint.
Similarly for pscp: Using echo y as an automated response to a pcp hostkey prompt
& "echo y | C:\Program Files\PuTTY\plink.exe" makes echo y | part of the path to the executable.
Change this:
& "echo y | C:\Program Files\PuTTY\plink.exe" -ssh -batch -i $PrivateKeyPath $username "exit" 2>&1
into this:
echo 'y' | & 'C:\Program Files\PuTTY\plink.exe' -ssh -batch -i $PrivateKeyPath $username "exit" 2>&1
Related
I'm trying to get the root folder of a connected android device with powershell, when I try the standard command for this in the cmd it just works as expected and returns the path.
CMD
adb -s <DeviceId> shell echo $EXTERNAL_STORAGE
When I try to replicate this command in powershell I get nothing not even an error.
PowerShell
Invoke-Expression -Command "adb -s <DeviceId> shell echo $EXTERNAL_STORAGE" | Out-String
I've searched for some solutions and have found a way to make it work in powershell but I was wondering if their is an other way to get the same result without calling cmd.exe in powershell
Working PowerShell but seems doggy
(cmd.exe /c adb -s $Id shell echo `$EXTERNAL_STORAGE) | Out-String
Just specify the command as-is.
$Result = & adb -s $Id shell echo `$EXTERNAL_STORAGE
Write-Host $Result
Maybe:
$Result = & adb "-s $Id shell echo `$EXTERNAL_STORAGE" | Out-String
I have this code:
powershell -command "& { (New-Object Net.WebClient).DownloadFile('linkToMyFile.file', 'C:\my.file') }"
it's for download file.
When I execute it in cmd on remote server - everything is ok.
But when I want to execute this code from my computer on remote server using paexec, I have some troubles with escape characters.
Command in my CMD:
psexec.exe \\remoteServer.0.1 -u username -p password -dbg -lo D:\PsExec.log cmd /c "powershell -command "& { (New-Object Net.WebClient).DownloadFile('linkToMyFile.file', 'C:\my.file') }""
I try to use ^ symbol, but the same error;
Code using ^ symbol for double-quotes:
psexec.exe \\remoteServer.0.1 -u username -p password -dbg -lo D:\PsExec.log cmd /c "powershell -command ^"& { (New-Object Net.WebClient).DownloadFile('linkToMyFile.file', 'C:\my.file') }^""
Also, I tried to use \ (like in PHP) for escape, but have the same result.
Help with this or give advice how I can remotely download a file using the command line.
Unfortunately CMD uses different escape characters depending on what is escaped and where. There is no single one escape character that would be used everywhere.
In most cases the next character is escaped by prepending it with a caret (^), e.g. in for /f loops:
for /f "tokens=1" %%a in ('type file.txt ^| find "something"') do ...
But sometimes characters are escaped by doubling them, e.g. percent characters (%) in batch scripts:
#echo off
echo %%DATE%%=%DATE%
Sometimes you may even need need to put in other escape characters (like backslashes) because you need to escape something not for CMD, but for the command the string is being passed to:
mountvol | findstr /r \\\\
Your particular scenario shouldn't require additional quotes or escaping, though. Just run the PowerShell commandline directly, without cmd /c:
paexec.exe \\remoteServer.0.1 -u username -p password powershell -command "&{...}"
Is it possible to use powershell the complete way? If so, you could try the following:
New-PsDrive -Name X -Root \\127.0.0.1\c$ -PsProvider FileSystem -Credential (Get-Credential)
Copy-Item -Path X:\RequestedFile.txt -Destination C:\Temp
Remove-PsDrive -Name X
If your destination is a http address you could perform following actions:
Invoke-WebRequest -Uri http://some.uri -Method Get -OutFile C:\temp\myfile -Credential (Get-Credential)
Hope that helps
Instead Of this:
psexec.exe \\remoteServer.0.1 -u username -p password -dbg -lo D:\PsExec.log cmd /c "powershell -command "& { (New-Object Net.WebClient).DownloadFile('linkToMyFile.file', 'C:\my.file') }""
DO this:
psexec.exe \\remoteServer.0.1 -u 'username' -p 'password' powershell.exe -Command "& {(New-Object System.Net.WebClient).DownloadFile('linkToMyFile.file', 'C:\my.file')}"
This should do your work.
Hope it helps.
I am trying to build tool which would renew expired certs and I am strugling with invoking certreq command:
this one is working:
Invoke-Command -ComputerName $Srvname -ScriptBlock {Set-Location -Path 'D:\'; $Out = cmd /c 2>&1 'certreq -q -f -new D:\cert\request_new.inf D:\cert\certreq.csr'}
but that one does not
Invoke-Command -ComputerName $Srvname -ScriptBlock {Set-Location -Path 'D:\'; $Out = cmd /c 2>&1 'certreq -f -attrib "CertificateTemplate:WebServer" -submit D:\cert\certreq.csr D:\cert\request.cer'}
I think its because I need to accept the prompt from cert authority and that prompt does not show.
The script works localy when executing:
certreq -f -attrib "CertificateTemplate:WebServer" -submit D:\cert\certreq.csr D:\cert\request.cer
Do you have any ideas?
I have tried psexec but that didnt work either. :S
From the certreq.exe documentation:
-q Use silent mode; suppress all interactive prompts.
I'm executing a command using Plink through a Perl file from a Windows machine.
system("cmd /c c:\\plink.exe -batch -ssh -l $user_name # $host_name -pw $pwd start_http");
Execution is hanging. When I execute the same command from command prompt, Plink is not returning to command prompt.
Tried using & at the end of the command but no use. And I don't want to redirect output to any log file.
Whereas "stop" command is working fine
system("cmd /c c:\\plink.exe -batch -ssh -l $user_name # $host_name -pw $pwd stop_http");
stdout/err must be detached from the terminal.
So change command to
system("cmd /c c:\\plink.exe -batch -ssh -l $user_name # $host_name -pw $pwd start_http /dev/null 2>&1 &");
I run the script from the file in Jenkins. Command:
psexec -i -s cmd.exe /c echo . | powershell.exe -file "c:\Program Files (x86)\Applications\Jenkins\jobs\Deploy\workspace\Deploy\script.ps1"
How can I pass a value for the "build" variable?
Please, help me
Thanks
You would simply follow the path with your arguments. Such as:
psexec -i -s cmd.exe /c echo . | powershell.exe -file "c:\Program Files (x86)\Applications\Jenkins\jobs\Deploy\workspace\Deploy\script.ps1" '-build 1.0'
You can see the command line syntax for PowerShell.exe here. It shows:
PowerShell[.exe]
[-File <FilePath> [<Args>]]
If that does not work for you, you can try and dot source the script in a -command scriptblock, such as:
psexec -i -s cmd.exe /c echo . | powershell.exe -command {. "c:\Program Files (x86)\Applications\Jenkins\jobs\Deploy\workspace\Deploy\script.ps1" -build 1}
I made mistake in the script. I forgot to insert the following code:
Param(
[int32]$build=0
)
As TheMadTechnician said, the command to run the script is:
psexec -i -s cmd.exe /c echo . | powershell.exe -file "c:\Program Files (x86)\Applications\Jenkins\jobs\Deploy\workspace\Deploy\script.ps1" -build %BUILD_NUMBER%