I need help fixing my batch file in execution to powershell - powershell

I'm trying to call these powershell scripts in a batch file, as follows:
Powershell.exe -executionpolicy remotesigned -File d:\1.ps1
Powershell.exe -executionpolicy remotesigned -File d:\2.ps1
Powershell.exe -executionpolicy remotesigned -File d:\3.ps1
Powershell.exe -executionpolicy remotesigned -File d:\4.ps1
I run that batch file and is giving me the following error for 2.ps1,3.ps1 and4.ps1
the argument d:\2.ps1 for the parameter -file doesnt exist. Provide the path of access to the file d:\2.ps1 (2.3.4) existent as argument for the parameter -file"
The files are in the drives where i am calling them, 3 and 4 fails to execute because are connected to script 2.ps1.
I dont know what is the problem in calling 2.ps1, after 1.ps1 because it is the same commnd, if I execute the files manually, they run just fine.

Try calling it like this:
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -File "d:\1.ps1"

Related

How to pass arguments to PowerShell script executed as administrator from batch script?

I have a PowerShell script that needs to be executed using Windows' Batch Script as Administrator. The script is running without arguments, however when I pass arguments, the PowerShell window pops up and closes instantly. Here is what I have tried.
Batch script
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile -
ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1" -FolderPath \"test"' -Verb
RunAs"
Powershell
param
(
[string]$FolderPath ='D:\batch_scripting'
)
echo $FolderPath
pause
I will add further functionalities in these scripts later. But I have to figure out this first.
The script can be executed if -FolderPath argument is not passed.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile -
ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1"' -Verb
RunAs"
I have also gone through following questions but this does not work for me.
I found the solution, although it's weird.
When you execute the powershell script as Administrator, trailing "/" must be added to the path of the script.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell 'NoProfile - ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1\"' -Verb RunAs"
It works fine now.

Executing PowerShell from a Window command batch script using the Start-Process command with named parameters

I am attempting to execute a Powershell Start-Process command with a named parameter list from a Windows command script. I tried including the named parameters;(-productname, -platform, -version, -exepath, -exe, -installMode) in the ArgumentList, however, they when the PowerShell process starts it appears to fail to see them. The command I am using is below:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""setadminstartandcreateshortcut.ps1"" -productname %PRODUCTNAME% -platform %PLATFORM% -version %VERSION% -exepath %TARGET% -exe %PRODUCTNAME%.exe -installMode %INSTALLMODE%' -Verb RunAs}"

The argument 'install-sshed.ps1' to the -File parameter does not exist | powershell

I am adding key to windows 7 x64 using powershell
So far i have tried the below command using powershell
powershell -executionpolicy bypass -file install-sshed.ps1
I got error like this:
The argument 'install-sshed.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter
What i am doing wrong?
It seems like your PowerShell session is started in a different folder than your PowerShell script. Try to add the full path to your PowerShell script:
powershell -executionpolicy bypass -file "c:\scripts\install-sshed.ps1"
Alternatively, change to the directory where the *.ps1 file is located first:
cd c:\scripts
powershell -executionpolicy bypass -file install-sshed.ps1
In addition to the above answer, I'd like to add that you can also use relative paths.
For instance, to run from current directory
powershell -execution policy unrestricted -file ".\test.ps1"
Or to run from nested folder:
powershell -execution policy unrestricted -file ".\config\test.ps1"
I will also recommend to add the following parameter: -noprofile
This will make sure no user profiles will be loaded, it is a best practice to do that when running scripts.
You can find an interesting read here
Additionaly to other answers.
It might be you've downloaded OpenSSH-Win32_Symbols.zip or OpenSSH-Win64_Symbols.zip
- these are NOT required binaries.
You might try to download OpenSSH-Win32.zip or OpenSSH-Win64.zip version respectively.
Also it suggested that you should cd into C:\Program Files\OpenSSH but archive is in a subfolder and I had to extract it to the parent OpenSSH folder.
Extra: common issue with permissions described here by asterikx, you might find useful to reinstall openssh following these steps if something is not working properly (assuming the downloaded archive is located at D:\Downlaods):
powershell.exe -ExecutionPolicy Bypass -File 'C:\Program Files\OpenSSH\uninstall-sshd.ps1'
del 'C:\Program Files\OpenSSH\'
Expand-Archive D:\Downloads\OpenSSH-Win64.zip -DestinationPath 'C:\Program Files\OpenSSH'
copy 'C:\Program Files\OpenSSH\OpenSSH-Win64\*' 'C:\Program Files\OpenSSH\'
del 'C:\Program Files\OpenSSH\OpenSSH-Win64\'
powershell.exe -ExecutionPolicy Bypass -File 'C:\Program Files\OpenSSH\install-sshd.ps1'
powershell.exe -ExecutionPolicy Bypass -File 'C:\Program Files\OpenSSH\install-sshd.ps1'
powershell.exe -ExecutionPolicy Bypass -File 'C:\Program Files\OpenSSH\FixHostFilePermissions.ps1'
powershell.exe -ExecutionPolicy Bypass -File 'C:\Program Files\OpenSSH\FixUserFilePermissions.ps1'
Set-Service sshd -StartupType Automatic
Set-Service ssh-agent -StartupType Automatic

pass deluge torrentname to powershell via bat script

I've install deluge and would like to use the execute plugin to send a pushover message via powershell
set torrentname=%2
powershell.exe -ExecutionPolicy Unrestricted -File C:\Pushover-DelugeNotify.ps1 %torrentname%
I've created the above yet for some reason it doesn't work I see a cmd window flash up and close, yet no message comes through.
I've tried
powershell.exe -ExecutionPolicy Unrestricted -File C:\Pushover-DelugeNotify.ps1 "Test"
which works no problem.
whay am I doing wrong?
If you don't have other arguments on your batch, you should use "set torrentname=%1"
set torrentname=%1
powershell.exe -ExecutionPolicy Unrestricted -File C:\Pushover-DelugeNotify.ps1 %torrentname%

Syntax error when starting powershell from bat file

I want to start a powershell script with RunAs from a bat file. This works.
#echo
SET "InstallerFolder=\\dc01\e\script"
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%InstallerFolder%\Script.ps1""' -Verb RunAs}";
But if i add:
-RedirectStandardOutput ""%InstallerFolder%\node.txt""
It breaks.
So the line looks like this:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-RedirectStandardOutput ""%InstallerFolder%\node.txt"" -NoProfile -ExecutionPolicy Bypass -File ""%InstallerFolder%\TSM Client Install Script.ps1""' -Verb RunAs}";
And resuslts in an powershell error which is gone so fast i can't see it.
Probably syntax?
Help much appreciated!
Thanks.
You get an error because powershell.exe does not have a -RedirectStandardOuptut parameter. (See Technet).
Also your syntax is way off (but since i dont see any reason to start powershell to start powershell again i wont bother explaining the syntax errors).
If you want to use RunAs from the cmd use it directly. For more info see Technet (again).
Also you can redirect output in Batch Files with > or >> if you want to append.