The more forums i look on, the more confused i get, and i've found very similar posts, but not exactly what i'm looking for, i'm a powershell newbie, can someone tell me the best way of being able to write this in powershell.
I can get the program to launch but it's passing the parameters and the syntax i'm struggling with. To make it easier to read i have removed any formatting from the $programArgs command so i'm hoping it's just putting in the correct synatax around it.
This is what i have so far:-
$program = "C:\Program Files (x86)\PuTTY\psftp.exe"
$programArgs = "-pw 1234 -P 10023 -i D:\sftp\Keys\mykey.ppk bigG#Dmydomain.co.uk -b d:\sftp\Scripts\GetAll.txt"
Invoke-Command -ScriptBlock { & $program $programArgs }
Should be able to just use Invoke-Expression "$program $programArgs".
To Clarify this is the command i used to launch psftp and pass various parameters:-
Start-Process -FilePath "c:\Program Files (x86)\PuTTY\psftp.exe" -ArgumentList "-pw Password123 -P 10023 -i D:\Keys\pub.ppk username#username.com -b d:\Scripts\GetAll.txt" -Wait -NoNewWindow
Related
I'm trying to run the following executable with parameters from Powershell:
wgrib2.exe test_t.grib -if "^(1|2):" -grid_def -else -s -lola 0:360:1 -90:181:1 1x1_t.grib grib -endif
So far I've tried several different methods but none have worked. Initially using cmd.exe:
$wg= test_t.grib -if "^(1|2):" -grid_def -else -s -lola 0:360:1 -90:181:1 1x1_t.grib grib -endif
$jobregrid = Start-Job { cmd.exe /c F:; chdir F:\nwp\global\grib; wgrib2.exe $using:wg" }
Wait-Job $jobregrid
Receive-Job $jobregrid
$jobregrid = $null
Also tried Invoke-Expression and Start-Process.
The command works when run directly from a DOS prompt or from a batch file. The problem seems to be that I don't understand PowerShell syntax sufficiently to deal with "^(1|2):" section. I have tried using the escape character ` but I still can't get it to work.
Help please?
Brian
I am still learning PowerShell and the Windows run line seems to make it even harder
Question: How can I do this directly from the run line (if possible an admin powershell) but I can deal with clicking yes after the the download... it just slows down the process
wget 'https://MYSERVER/MYFILE.MSI' -O PROGRAM.msi; start PROGRAM.msi /qn
This works great when powershell is already open as admin, also works when powershell is open as normal user, but I have to wait for the program to be downloaded to click yes instead of clicking yes to the admin powershell and let the rest autoinstall.
I tried
Powershell -Command 'wget...
but not working
Point of note: wget in PoSH is an alias
Get-Alias -Name wget
CommandType Name
Alias wget -> Invoke-WebRequest
... and with the way you are doing this, you are using wget.exe, not the above.
So, you can use wget.exe, but you have to specify the full UNC to wget.exe if it is not in your system path. That .exe is a must.
Or you need to remove the aliases
Remove-Item Alias:WGet
To download files from the web, look at the Invoke-WebRequest examples
Get-Help -Name 'Invoke-WebRequest' -Examples
Or write your own function using .Net, someting like the below and put it in your PoSH user profile
Function New-ToolDownloadInstall ($url)
{
# Set the webclient
$webclient = New-Object System.Net.WebClient
# Extract the filename from the URL and Download
$filename = [System.IO.Path]::GetFileName($url)
$file = "$env:USERPROFILE\Downloads\$filename"
$webclient.DownloadFile($url,$file)
# Remove the web ADS
Unblock-File -Path $file
# Install the file
Start-Process $file -NoNewWindow -wait
}
# Use the function
New-ToolDownloadInstall -url 'https://download.microsoft.com/download/5/0/1/5017D39B-8E29-48C8-91A8-8D0E4968E6D4/en/msoidcli_64.msi'
Here is another example
Download files from websites programatically via powershell
https://gallery.technet.microsoft.com/scriptcenter/files-from-websites-4a181ff3
Also, some DOS level command have to be called this way, if you are in the PowerSHell_ISE.exe.
Start-Process "$PSHOME\powershell.exe" -ArgumentList "-NoExit","-Command &{ wget.exe 'https://MYSERVER/MYFILE.MSI' -O PROGRAM.msi; start PROGRAM.msi /qn }"
See more details here:
https://blogs.technet.microsoft.com/josebda/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters
I want to use PowerShell to connect to a PuTTY "saved session" and then specify a file that contains some batch commands. Using CMD this would look like
d:\putty\psftp 'Saved Session Name' -b d:\location.txt.
I think that the PS equivalent should look like
Start-Process d:\putty\psftp.exe 'Saved Session Name'
(and then a call to pass a 'get' script) i.e. cd Outgoing get <date>.txt
However, I get the following error:
a positional parameter cannot be found that accepts the argument
How can I accomplish this using PowerShell?
All you need is plink:
plink 'Saved Session Name'
You don't necessarily need Start-Process.
What happens when you try to run d:\putty\psftp.exe 'Saved Session Name' -b d:\location.txt from Powershell? The first thing I do is try it exactly like I'd run it from the command line.
The biggest catch is if you have spaces in path names. You might need to use quotes and the call operator (ampersand): &"d:\putty\psftp.exe" 'Saved Session Name' -b "d:\location.txt".
If you do need to use Start-Process, you can do this:
Start-Process -FilePath "d:\putty\psftp.exe" `
-ArgumentList "'Saved Session Name' -b d:\location.txt" -Wait
Or like this:
Start-Process -FilePath "d:\putty\psftp.exe" `
-ArgumentList 'Saved Session Name', '-b', "d:\location.txt" -Wait
Note that the argument list in the first is a single string with every argument in it, and in the second it is an array of strings with one string for every argument. Everything needs to be in the same order that they'd be on the command line, and it's not uncommon for it to be a bit flaky. Usually one method or the other works better, but it depends on the application you're calling. Usually with quotes and spaces in path names because you're going through multiple levels of escaping depending on the program you're calling (noticing a theme?).
I added the -Wait parameter to my code above because, by default, Start-Process continues to the next line without waiting since it actually spawns a separate process. -Wait forces Powershell to, well, wait, which is what people usually want in a non-interactive script.
See Get-Help about_Operators or Get-Help "call operator" for more topics for help with the call operator. See Get-Help Start-Process for help with that.
Adding the below Technet Wiki link which contains various ways to run executables in PowerShell.
PowerShell: Running Executables
Try this:
$Path = "d:\putty\psftp.exe"
$Prm1 = 'Saved Session Name'
$Prm2 = "-b"
$Prm3 = "d:\location.txt"
&$Path $Prm1 $Prm2 $Prm3
I am trying to invoke a command-line .exe tool (x264 to convert some videos) and print its output in PowerShell host.
There are a lot of parameters that need to be passed to the x264. So far I couldn't run it with the Invoke-Item cmdlet so I tried
[diagnostics.process]::start($com, $args).WaitForExit()
This works fine but it opens the old cmd window to show the output. Just out of curiosity I was wondering how can I show the output in the host.
I might be completely off, no PowerShell guru, but can't you just run the following?
$args = #("list", "of", "args", "here")
& x264.exe $args
When the cmd window opens, does it show the output? If so, maybe this can help.
Start-Process c:\x264.exe -ArgumentList $args -Wait -NoNewWindow
My script stores a bunch of External application commands from an app like so
$app1 = get-command "C:\Where\Command\Located.exe"
Then I call it with
& $app1 -S -P 9000
(where -S -P and 9000 are parameters as I'd pass them in a cmd.exe shell). Up to here everything works perfectly. However, I want to run this as a background task and here's where I begin running into trouble.
Start-Job -ScriptBlock { & $app1 -S -P 9000 }
fails with "The expression after '&' in a pipeline element produced an invalid object." I've searched google for about two days and everything I've tried seems to be for naught. Enclosing parameters in #() and trying to splat them out, Invoke-Expression, Invoke-Command, Invoke-Item (still unclear as to what these are all doing). I've also tried converting the whole command to a string and then calling Invoke-Expression within the start job but it doesn't seem to be working either. So this question is resources to understand when all these things are appropriate to use and why it doesn't work as soon as I pass it to a background job.
Try passing the $app1 variable to the script block:
Start-Job -ScriptBlock {param($app1) & $app1 -S -P 9000 } -ArgumentList $app1
Note that instead of
$app1 = get-command "C:\Where\Command\Located.exe"
you can also just have
$app1 = "C:\Where\Command\Located.exe"