WinSCP select most recent file - command-line

Looking for the WinSCP command line equivalent of selecting the most recent/latest file?
http://winscp.net/eng/docs/script_download_most_recent_file
I'm looking for a way to update my WinSCP command-line below to download the latest file:
winscp.com /command "option batch abort" "option confirm off" "open sftp://martin#example.com/ -hostkey=""ssh-rsa 2048 xx:xx:xx...""" "exit"

Use the -latest switch of the get command, as the Downloading the most recent file article shows:
get -latest /home/user/* c:\downloaded\
If you want to use it on command-line, the syntax is like:
winscp.com /command "open sftp://user:password#example.com/ -hostkey=""ssh-rsa 2048 xx:xx:xx...""" "get -latest /home/user/* c:\downloaded\" "exit"
Use WinSCP GUI to generate command-line template for you.

Related

WinSCP - Unknown command and '.log' is not recognized as an internal or external command

Context: I'm using powershell to instantiate a batch file to execute the synchronize remote command in WinSCP and pull in fresh data from a remote directory to my local directory. See screenshots below for contents of the WinSCP commands and the batch file contents.
When I try to execute I am able to successfully authenticate but I see 2 errors.
Batch file contents:
winscp.com /script=winscp_commands_ar_history.txt
/log=C:/Users/REDACTED/Desktop/AR_History_Report/winscp_log_ar_history.txt
WinSCP commands:
option batch abort
option confirm off
open sftp://REDACTED:REDACTED#sftp.REDACTED.com
-hostkey="ssh-rsa 2048 cc:ea:2e:03:96:ca:e7:c0:59:74:13:a8:XX:XX:XX:XX"
synchronize remote /export/GroupAccess/REDACTED/Cognos Reporting/CogTest
C:/Users/REDACTED/Desktop/AR_History_Report
exit
Powershell output:
Errors:
Unknown command '-hostkey=ssh-rsa 2048 cc:ea:2e:03:96:ca:e7:c0:59:74:13:a8:XX:XX:XX:XX'.
'/log' is not recognized as an internal or external command,
operable program or batch file.
Question: How do I resolve these 2 errors? I suspect the last one is related to environment variables since it seems powershell didn't recognize the WinSCP log command so I've added WinSCP to my PATH environment variable but I'm still getting the same errors. Can someone please assist? Thank you.
Both your batch file and WinSCP script have line breaks in the middle of commands. Moreover, in the synchronize command you need to wrap the path with spaces to double quotes.
The batch file should be:
winscp.com /script=winscp_commands_ar_history.txt /log=C:/Users/REDACTED/Desktop/AR_History_Report/winscp_log_ar_history.txt
And the script file should be:
option batch abort
option confirm off
open sftp://REDACTED:REDACTED#sftp.REDACTED.com -hostkey="ssh-rsa 2048 cc:ea:2e:03:96:ca:e7:c0:59:74:13:a8:XX:XX:XX:XX"
synchronize remote "/export/GroupAccess/REDACTED/Cognos Reporting/CogTest" C:/Users/REDACTED/Desktop/AR_History_Report
exit
As an aside, as #mklement0 commented, you don't need another powershell process to invoke a batch file from PowerShell - just invoke batch files directly: .\ar_trigger_history.bat

Adding switch /ini=nul to WinSCP command-line does not work

I am trying to write an automated script to upload a file to sftp using WinSCP.
The initial script below works with no error
C:\Users\anh le>"C:\Program Files (x86)\WinSCP\WinSCP.exe" /command "open sftp://username:password#sftphost/" "cd /ADHOC" "put ""C:\Temp\ldg_pnv.csv""" "close" "exit"
But when I added the /ini=nul switch, the upload didn't work and also didn't return any error.
Does anyone has any idea why adding the switch, which is recommended by the way, caused this?
C:\Users\anh le>"C:\Program Files (x86)\WinSCP\WinSCP.exe" /ini=nul /command "open sftp://username:password#sftphost/" "cd /ADHOC" "put ""C:\Temp\ldg_pnv.csv""" "close" "exit"
Not sure what are you trying to achieve, but indeed your command with /ini=nul cannot work. You are connecting to an SSH (SFTP) server and that requires a verification of the server's host key. Your script does not do that and as you use /ini=nul, WinSCP won't use (possibly) cached hostkey from your WinSCP GUI configuration.
Though, I'm sure WinSCP does "return error". You just cannot see the error printed because, you do not enable logging (/log switch), nor you use console interface (winscp.com or /console).
A full command can be like this:
C:\Users\anh le>"C:\Program Files (x86)\WinSCP\WinSCP.com" /log=C:\some\path\winscp.log /ini=nul /command "open sftp://username:password#sftphost/ -hostkey=""server_host_key""" "cd /ADHOC" "put ""C:\Temp\ldg_pnv.csv""" "close" "exit"
WinSCP GUI can generate a full command like this for you, including the -hostkey switch.

How to I remove the Powershell start text?

Powershell 6 has a Unix-style /etc/issue that mentions a link to the docs.
PowerShell v6.0.0
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
This is fine, but:
I know where the docs are
I know I launched Powershell 6
How can I remove some, or all of the message? IIRC Powershell 5 still had the copyright message so maybe I can't remove that, but getting rid of the last 3 lines would help?
Pass the -nologo option.
-NoLogo Starts the PowerShell console without displaying the copyright banner.
pwsh.exe -nologo ...other arguments...
If you are using the new Windows Terminal then:
Go to Settings:
Add the argument -nologo to the PowerShell command line:
From #sandu's answer, it could be improved as below.
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"args": ["-noLogo"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
Now you can add -nologo in Windows Terminal settings:
The easiest way would be to add Clear-Host to the top of your $profile file.
You can open powershell and input $profile, you will get Microsoft.PowerShell_profile.ps1 file's path.
open this file (create one without) by notepad and input a clear or cls command.Save and exit.
the command of this file will be executed when you open powershell
As np8 said under this answer, the way to hide the initial text in the Visual Studio Code integrated terminal is as follows:
Open the workspace settings file (./.vscode/settings.json) or the user settings file (search for Preferences: Open Settings (JSON) in the command palette, opened with F1)
Add the following inside the outer-most "layer" of curly brackets:
"terminal.integrated.shellArgs.windows": [
"-nologo"
]
What worked for me was to go to File > Preferences > Settings and type terminal.integrated.defaultProfile.windows in the search bar (you can change windows with other supported OS if you need to). There should now be only one option to choose from (that's why I included windows). Click on the Edit in settings.json link bellow it. It should now open settings.json and generate the line necessary to modify the default terminal profile. Which looks something like:
"terminal.integrated.defaultProfile.windows": "",
Above that line you can create your own profile:
"terminal.integrated.profiles.windows": {
"PowerShell -nologo": {
"source": "PowerShell",
"args": ["-nologo"]
}
},
Name the profile however you want and add whatever parameters you want. Once you're done. Set the profile name into the option that was previously generated for you. In this example it should look like:
"terminal.integrated.defaultProfile.windows": "PowerShell -nologo",
Save and close. Now when you open a terminal it should have no logo for example.
I was fix problem on the top by
There are several ways of doing it :
Creating a shortcut with value
"C:\Program Files\PowerShell\7\pwsh.exe" -nologo -NoExit -Command "Set-Location c:\Users\%username%"
If you are using Windows terminal then you can replace the source with
"commandline": "pwsh.exe -NoLogo"
Users of Chinese version can also add -nologo in Windows terminal settings:
As shown in the figure:
If you are using the new Windows Terminal in Windows 11,
Go to Settings:
Select the Windows Powershell profile from the left sidebar
Add the argument -nologo to the command line:
Save and be sure to restart your terminal to see the changes.
If you're using windows just create a profile and put the command clear inside it.
In PowerShell I'm going to open the profile with notepad:
notepad $Profile
Highly Recommended to Use
Powershell Preview: https://github.com/PowerShell/powershell/releases
Editing Context Menu
If you are using Powershell Preview, you can change context menu using regedit:
Open regedit
Go to Computer\HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShell7-previewx64
add flag -nologo to Icon
2. Editing Start Menu Shortcut
In File Explorer go to
C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
Create a shortcut with value
"C:\Program Files\PowerShell\7-preview\pwsh.exe" -nologo -NoExit -Command "Set-Location c:\\Users\\%username%"

I keep getting "Access Denied" when I try to install 7zip from the installer executatble I have downloaded

I am trying to install 7-Zip if its already not installed in my Win7 systems using a Perl script.
# Check if 7-Zip is installed or Not.
if (!(-e "C:\\Program Files\\7-Zip")){
print "\n 7-Zip is not installed. Downloading ... \n"
system("wget http://downloads.sourceforge.net/project/sevenzip/7-Zip/9.20/7z920.exe?r=&ts=1392082197&use_mirror=softlayer-dal");
print "\n Installing.. \n";
system("7z920.exe /S /D=\"C:\Program Files\7-Zip\"");
}
It is downloading the file I need (7z920.exe) to the PWD, but it doesn't install. I keep getting the command line error, "Access Denied". Even if I run the script in Administrator mode on command line.
In fact, if I simply go the system path were 7z920.exe on command line and attempt to call the .EXE, i get the same denied error. It won't install. But if I click on it from the folder, it allows me to install it?! Why is that I may double-click and install from folder, but cannot call the executable from the same path from command line and not install it?
C:\>7z920.exe /S /D="C:\Program Files\7-Zip"
Access is denied.
Can you help me? How do I install this using my script? or from a batch file?
When I run that wget command, I get a file called 7z920.exe#r=&ts=1392082197&use_mirror=softlayer-dal. So I would recommend
Use wget's -O option to force the downloaded file to have the filename you want.
wget -O 7z920.exe http://downloads.sourceforge.net/project/...
I don't know what's in your PATH, so precede your command with the current path.
system(".\\7z920.exe /S /D=...");
Be careful about how you escape special characters in Perl inside double quotes. Try one of:
system(".\\7z920.exe /S /D=\"C:\\Program Files\\7-Zip\"");
system('.\7z920.exe /S /D="C:\Program Files\7-Zip"');

How to create a file without a Command Prompt window

I'm using WinRAR SFX module to create an installation, and use its presetup option to run some preliminary tests.
Since wscript can only accept vbs file, and not the script itself, I first run "cmd /c echo {...script code...} > setup.vbs", and then I run "wscript setup.vbs". The run of the first cmd command opens a brief command window, and I would really like to avoid this. I thought of using RunDll32 to write this data, but couldn't find any suitable API to use.
Can anyone think of a way to bypass it and create a small file with a small VBScript text without opening a Command Prompt window?
Thanks a lot,
splintor
Is the script code already in a file? If so,
You can use the TYPE command to send the script to a file:
TYPE [script_file] > setup.vbs
or COPY the script file:
COPY [script_file] setup.vbs
If the script code is in the body of your cmd, you can use the START command to run the cmd without a window (/b flag):
START /B cmd /c echo {...script code...} > setup.vbs
Rather than use cmd /c echo {...script code...} > setup.vbs as a presetup step, perhaps you could package a VBscript with your install that does your preliminary tests and creates setup.vbs, and then calls setup.vbs for you. You'd have to put this in the setup portion of the WinRAR script.
You can call another VBScript from VBScript like this:
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.Run "wscript d:\setup.vbs, ,True
See this MSDN link for the syntax of the Run command.