How to run a WinSCP from ps1 file - powershell

I have a .ps1 file with my script.
In this script I have a line with:
Start-Process "C:\activatebatch.bat"
If i execute it directly with ps1 file, everything works well. But if I set up a Windows Scheduler with ps1 as executive, the bat file doesn't start. In that bat file I have a WinSCP which sends file to server.
How could I set it to start .bat from .ps1 stored in Windows Scheduler? Or how could I execute WinSCP directly from PowerShell code?
I need it to call for WinSCP to send a file into server - and the options are stored in batch file:
"C:\Program Files (x86)\WinSCP\WinSCP.exe" user:password#IPI.PIP.IP.IP /command ^
"put C:\ss\file.txt /home/scripts/windows/" ^
"exit"

If you have a working WinSCP command line in a batch file, you may need to do few changes to make it compatible with PowerShell:
Batch file use ^ (caret) to escape a new line. PowerShell uses ` (backtick). So replace your carets to backticks.
And you obviously need to escape any characters with a special meaning in PowerShell, particularly $ (dollar sign), ` (backtick) and inner double quotes.
In your simple script, only the first point matters, so the correct command is:
& "C:\Program Files (x86)\WinSCP\WinSCP.exe" user:password#IPI.PIP.IP.IP /command `
"put C:\ss\file.txt /home/scripts/windows/" `
"exit"
Though I'd further recommend you using winscp.com instead of winscp.exe and adding /log switch to enable session logging for debugging purposes.
Also opening a session using a command-line argument is deprecated. You should use open command (and better also specify a protocol prefix – sftp://?).
& "C:\Program Files (x86)\WinSCP\WinSCP.exe" /command `
"open user:password#IPI.PIP.IP.IP" `
"put C:\ss\file.txt /home/scripts/windows/" `
"exit"
WinSCP 5.14 beta can actually generate a PowerShell – WinSCP command template for you.
Though for a better control, it's recommended to use WinSCP .NET assembly from PowerShell.

Related

Powershell Script only opens as a .txt file when run

Hi I have a script on a memory stick that I want to be able to run in cmd line before a computer has windows fully installed. (the stage just after you've connected to a network).
Cmd used to run the script.
Start > Run D:\pscript\Intune.ps1
This only opens a .txt file, while researching I've found that the reason this happens is due to security, is there anyway to override this bar changing the default file type out.
Unlike batch files (.cmd, .bat) associated with cmd.exe (the legacy Command Prompt), PowerShell's .ps1 script files are by default not directly executable from outside PowerShell.
Instead, they are treated as documents that are by default associated with either Notepad or the (obsolescent) Windows PowerShell ISE, depending on the , and invoking them therefore opens them for editing, which applies to the following contexts:
Invoking a .ps1 file from cmd.exe
Invoking a .ps1 file from Start Menu's Run dialog, as in your case (which you can invoke with WinKey+R, for instance)
Opening (double-clicking) a .ps1 file from File Explorer or Desktop.
To execute a .ps1 script from outside PowerShell, you must therefore invoke it via PowerShell's CLI, powershell.exe for Windows PowerShell, pwsh for PowerShell (Core) 7+.
In the simplest case, using Windows PowerShell and the -File parameter, as also shown by Mathias R. Jessen in a comment; see the comments below and the linked docs for additional parameters:
# Note:
# * The effective execution policy applies; use -ExecutionPolicy Bypass to bypass.
# * Profiles are loaded; use -NoProfile to suppress.
# * The console window auto-closes when the script terminates; use -NoExit
# to keep the session open.
powershell.exe -File D:\pscript\Intune.ps1
For a comprehensive overview of PowerShell's CLI, see this post.
It is possible - though not advisable - to configure your system to execute .ps1 files by default - see this answer.

How to add something to my Powershell profile?

Currently I am trying to make it so I can open up files from PowerShell directly in Sublime Text 3.
I am using this command: Set-Alias subl 'C:\Program Files\Sublime Text 2\sublime_text.exe'
This works for me initially when I type in "subl filename", but whenever I close out of PowerShell and try it again, it will not work unless I re-type the Set-Alias command. Is there any way to permanently make this possible, so I can always type in subl filename ?
Try running Get-Help about_Profiles
As a quick overview:
PowerShell will load the .ps1 file located at the $Profile path into the session when it starts. Adding Set-Alias subl 'C:\Program Files\Sublime Text 2\sublime_text.exe' to that file will set that alias every time a new PowerShell session is opened.

Running CMD command in PowerShell

I am having a bunch of issues with getting a PowerShell command to run. All it is doing is running a command that would be run in a CMD prompt window.
Here is the command:
"C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe" PCNAME
I have tried the following with no success (I have tried many iterations of this to try and get one that works. Syntax is probably all screwed up):
$TEXT = $textbox.Text #$textbox is where the user enters the PC name.
$CMDCOMMAND = "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe"
Start-Process '"$CMDCOMMAND" $TEXT'
#iex -Command ('"C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe"' $TEXT)
The command will just open SCCM remote connection window to the computer the user specifies in the text box.
Try this:
& "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe" PCNAME
To PowerShell a string "..." is just a string and PowerShell evaluates it by echoing it to the screen. To get PowerShell to execute the command whose name is in a string, you use the call operator &.
To run or convert batch files externally from PowerShell (particularly if you wish to sign all your scheduled task scripts with a certificate) I simply create a PowerShell script, e.g. deletefolders.ps1.
Input the following into the script:
cmd.exe /c "rd /s /q C:\#TEMP\test1"
cmd.exe /c "rd /s /q C:\#TEMP\test2"
cmd.exe /c "rd /s /q C:\#TEMP\test3"
*Each command needs to be put on a new line calling cmd.exe again.
This script can now be signed and run from PowerShell outputting the commands to command prompt / cmd directly.
It is a much safer way than running batch files!
One solution would be to pipe your command from PowerShell to CMD. Running the following command will pipe the notepad.exe command over to CMD, which will then open the Notepad application.
PS C:\> "notepad.exe" | cmd
Once the command has run in CMD, you will be returned to a PowerShell prompt, and can continue running your PowerShell script.
Edits
CMD's Startup Message is Shown
As mklement0 points out, this method shows CMD's startup message. If you were to copy the output using the method above into another terminal, the startup message will be copied along with it.
For those who may need this info:
I figured out that you can pretty much run a command that's in your PATH from a PS script, and it should work.
Sometimes you may have to pre-launch this command with cmd.exe /c
Examples
Calling git from a PS script
I had to repackage a git client wrapped in Chocolatey (for those who may not know, it's a package manager for Windows) which massively uses PS scripts.
I found out that, once git is in the PATH, commands like
$ca_bundle = git config --get http.sslCAInfo
will store the location of git crt file in $ca_bundle variable.
Looking for an App
Another example that is a combination of the present SO post and this SO post is the use of where command
$java_exe = cmd.exe /c where java
will store the location of java.exe file in $java_exe variable.
You must use the Invoke-Command cmdlet to launch this external program. Normally it works without an effort.
If you need more than one command you should use the Invoke-Expression cmdlet with the -scriptblock option.

Open Notepad++ from PowerShell

How can I open up a file in Notepad++ from the Powershell command line?
Inside PowerShell I can simply use the start and get general results
to open a python file with notepad++ here is what I did.
Start notepad++ ex1.py
this will start notepad++ and load the file ex1.py assuming you are in the same directory as the .py file. You can change that by adding the full path name
start notepad++ c:\users\you\desktop\files\ex1.py
Because the default path contains spaces, you have to quote the path to the exe. However because PowerShell is also a scripting language. A string by itself is simply evaluated as a string e.g.:
C:\ PS> 'Hello world'
Hello world
So you have to tell PowerShell you want to invoke the command that is named by the string. For that you use the call operator & e.g.:
C:\ PS> & 'C:\Program Files (x86)\Notepad++\notepad++.exe'
or if notepad++ is in your path:
C:\ PS> notepad++
or if you're in the same dir as the exe:
C:\ PS> .\notepad++
To open Notepad++ with and create a new empty file in the current path
start notepad++ newFile.txt
To open Notepad++ with an existing file
start notepad++ apples.txt
To specify the path and open multiple files
start notepad++ fruits/apples.txt, fruits/oranges.txt, package.json
To extrapolate on the previous answers and tie them up in a tidy bow:
If you want to open a file with spaces in the path or name:
. 'C:\Program Files (x86)\Notepad++\notepad++.exe' 'C:\Temp\File With Spaces.txt'
or
& 'C:\Program Files (x86)\Notepad++\notepad++.exe' 'C:\Temp\File With Spaces.txt'
It can also be set it as an alias:
Set-Alias -Value 'C:\Program Files (x86)\Notepad++\notepad++.exe' -Name 'NotePad'
$FileWithSpaces = 'C:\T e m p\File With Spaces.txt'
NotePad $FileWithSpaces
The top line here can be copied into (one of) your $Profile .ps1 file(s) so you don't need to keep using Set-Alias in every new PS instance.
Edit your profile and add an alias
Set-Alias -name 'npp' -value 'C:\Program Files\Notepad++\notepad++.exe'
Then:
npp c:\temp\test.txt
Edit your profile:
npp $profile
etc
I know this is an old question, but I found a bit of a workaround, quite by accident, and it is extremely straightforward. If you install and maintain Notepad++ via Chocolatey (think apt-get for Windows, but built on top of NuGet), then you get a shim that can be invoked from the command line.
cinst notepad++
And even if you already have an existing installation of Notepad, you can still "install" it from Chocolatey, and it will pull in the existing installation and maintain it.
I use Chocolatey for as much as I possibly can, because you can update everything in one fell swoop.
After that, editing things from PowerShell is a snap. Like my PowerShell profile:
notepad++ $PROFILE
Hope this helps someone, or several someones!
In my case, I wanted to start Notepad++ with a file as an argument, and open as admin. I wanted to open one of the PowerShell profiles. I had to use the following command variation:
start-process -Verb runas -filepath "C:\Program Files (x86)\Notepad++\notepad++.exe" "`"$($PROFILE.AllUsersAllHosts)`""
All the other variations didn't work, I think due to a space in the path of the file to be opened. So, you must escape the " as:
"He said `"This is fun.`""

Set up PowerShell Script for Automatic Execution

I have a few lines of PowerShell code that I would like to use as an automated script. The way I would like it to be able to work is to be able to call it using one of the following options:
One command line that opens PowerShell, executes script and closes PowerShell (this would be used for a global build-routine)
A file that I can double-click to run the above (I would use this method when manually testing components of my build process)
I have been going through PowerShell documentation online, and although I can find lots of scripts, I have been unable to find instructions on how to do what I need. Thanks for the help.
From http://blogs.msdn.com/b/jaybaz_ms/archive/2007/04/26/powershell-polyglot.aspx
If you're willing to sully your beautiful PowerShell script with a little CMD, you can use a PowerShell-CMD polyglot trick. Save your PowerShell script as a .CMD file, and put this line at the top:
#PowerShell -ExecutionPolicy Bypass -Command Invoke-Expression $('$args=#(^&{$args} %*);'+[String]::Join(';',(Get-Content '%~f0') -notmatch '^^#PowerShell.*EOF$')) & goto :EOF
If you need to support quoted arguments, there's a longer version, which also allows comments. (note the unusual CMD commenting trick of double #).
##:: This prolog allows a PowerShell script to be embedded in a .CMD file.
##:: Any non-PowerShell content must be preceeded by "##"
##setlocal
##set POWERSHELL_BAT_ARGS=%*
##if defined POWERSHELL_BAT_ARGS set POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%
##PowerShell -ExecutionPolicy Bypass -Command Invoke-Expression $('$args=#(^&{$args} %POWERSHELL_BAT_ARGS%);'+[String]::Join(';',$((Get-Content '%~f0') -notmatch '^^##'))) & goto :EOF
Save your script as a .ps1 file and launch it using powershell.exe, like this:
powershell.exe .\foo.ps1
Make sure you specify the full path to the script, and make sure you have set your execution policy level to at least "RemoteSigned" so that unsigned local scripts can be run.
Run Script Automatically From Another Script (e.g. Batch File)
As Matt Hamilton suggested, simply create your PowerShell .ps1 script and call it using:
PowerShell C:\Path\To\YourPowerShellScript.ps1
or if your batch file's working directory is the same directory that the PowerShell script is in, you can use a relative path:
PowerShell .\YourPowerShellScript.ps1
And before this will work you will need to set the PC's Execution Policy, which I show how to do down below.
Run Script Manually Method 1
You can see my blog post for more information, but essentially create your PowerShell .ps1 script file to do what you want, and then create a .cmd batch file in the same directory and use the following for the file's contents:
#ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%MyPowerShellScript.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'"
Replacing MyPowerShellScript.ps1 on the 3rd line with the file name of your PowerShell script.
This will allow you to simply double click the batch file to run your PowerShell script, and will avoid you having to change your PowerShell Execution Policy.
My blog post also shows how to run the PowerShell script as an admin if that is something you need to do.
Run Script Manually Method 2
Alternatively, if you don't want to create a batch file for each of your PowerShell scripts, you can change the default PowerShell script behavior from Edit to Run, allowing you to double-click your .ps1 files to run them.
There is an additional registry setting that you will want to modify so that you can run scripts whose file path contains spaces. I show how to do both of these things on this blog post.
With this method however, you will first need to set your execution policy to allow scripts to be ran. You only need to do this once per PC and it can be done by running this line in a PowerShell command prompt.
Start-Process PowerShell -ArgumentList 'Set-ExecutionPolicy RemoteSigned -Force' -Verb RunAs
Set-ExecutionPolicy RemoteSigned -Force is the command that actually changes the execution policy; this sets it to RemoteSigned, so you can change that to something else if you need. Also, this line will automatically run PowerShell as an admin for you, which is required in order to change the execution policy.
Source for Matt's answer.
I can get it to run by double-clicking a file by creating a batch file with the following in it:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe LocationOfPS1File
you can use this command :
powershell.exe -argument c:\scriptPath\Script.ps1