Running a PS1 script using a batch file (.bat) - powershell

Currently the following is my path for launching the VMware vSphere PowerCLI command prompt. I wish to run my sample.ps1 script automatically using a batch file. How can I incoporate sample.ps1 into this path and create a batch file?
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\""

If you are working with PowerShell 2.0, you can use the -file parameter of PowerShell.exe
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -file "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
If you are working with PowerShell 1.0, you can use -command parameter this way
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -command "& 'C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'"

echo off
Title,Report Script &color 9e
for /f "usebackq delims=$" %%a in (`cd`) do (
set SCRIPTDIR=%%a
)
(Set ScriptFile=%SCRIPTDIR%\Report.ps1)
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\";%ScriptFile%"

You can use this to launch arbitrary .ps1 scripts via .bat files by calling the bat file like your ps1. Then extract the name of file in batch and call powershell with it.
For a ready to use solution, use the following Gist: https://gist.github.com/JonasGroeger/10417237

I saw this code in another page, I test it in a W2012 R2 and it runs.
I hope it work:
C:\>powershell "C:\>1\file.ps1"

Related

How to open Powershell with cmd and use a .ps1 file

I try to open a ps1 powershell file thats updates a gitrepo
i have tryed
powershell -ExecutionPolicy Bypass -Command "& '%USERPROFILE%\folder1\update.ps1'
this is working but when it comes to a requirements.txt promt writes error of not found that file i think powershell is not inside this folder directly so it cant find that file what is needed
if i make this
cd %USERPROFILE%\ & REM First change to the batch file folder
echo Points as at = %time% %date%> updatebing_log.txt & REM create a simple logfile with time and end result of the batch run
echo ============================folder1=========================== >> updatebing_log.txt
cd folder1\
start powershell
cd %USERPROFILE%\folder1
powershell -ExecutionPolicy Bypass -Command "& '%USERPROFILE%\folder1\update.ps1'
echo Waiting seconds
timeout /t 10 /nobreak > NUL
it is working
but it opens a cmd (bash file) promt and a powershell then it cd to folder
powershell do nothing then
inside cmd i see that the ps1 file is executed and alsow works with the requirements.txt
As per my comment. Here's what your code is doing. Thus the response you're seeing.
# Run this in cmd.exe
cd %USERPROFILE%\ & REM First change to the batch file folder
echo Points as at = %time% %date%> updatebing_log.txt & REM create a simple logfile with time and end result of the batch run
echo ============================folder1=========================== >> updatebing_log.txt
cd folder1\
# Start a PowerShell interactive session
start powershell
cd %USERPROFILE%\folder1
# Start a new PowerShell session from cmd.exe and run this code
powershell -ExecutionPolicy Bypass -Command "& "$env:USERPROFILE\folder1\update.ps1" # note the profile change I made for you.
# Run this in cmd.exe on Powerhell exit
echo Waiting seconds
timeout /t 10 /nobreak > NUL
If that is not the flow you were after, then you need to rethink this.
i made this at work it looks likke it works but dont know i try to show the working directory but it want show it only shows were the ps1 call file is
i made a test.bat
powershell -ExecutionPolicy Bypass -Command "& '%USERPROFILE%\Brewd\updatebrewdall.ps1'"
and updatecall.ps1
cd "folder1\"
powershell -ExecutionPolicy Bypass -Command "& '.\update.ps1'"
Start-Sleep -Seconds 10
# now back to previous directory
cd ..\
cd "folder2\"
"$([Environment]::CurrentDirectory)\$ComputerName"
powershell -ExecutionPolicy Bypass -Command "& '.\update.ps1'"
Start-Sleep -Seconds 10
# now back to previous directory
cd ..\
cd "folder3\"
"$([Environment]::CurrentDirectory)\$ComputerName"
powershell -ExecutionPolicy Bypass -Command "& '.\update.ps1'"
Start-Sleep -Seconds 10

batch file to open two (or more) powershells in two (or more) specific folders

I am trying to do something really basic in a batch script, but it is not working.
I want to open two PowerShell windows each with a different current working directory.
I am using the following script
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User1"
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User2"
Unfortunately it opens only the first window.
For sure it is only a syntax error but I could not find a solution yet.
As it happens often: I posted the question after researching a lot and then I immediately found the answer!
This is what you should write in your batch file:
START C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User1"
START C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User2"
I hope my answer will help someone in the same situation.
The Start command already has a 'working directory', option, /D.
#Start /D "C:\Users\User1" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit
#Start /D "C:\Users\User2" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit
You could of course do it with a simple For loop:
#For %%G In ("C:\Users\User1" "C:\Users\User2") Do #Start /D "%%~G" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit
start C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User1"
start C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User2"
You just need to add "start" at the beginning

How to run powershell inside BAT file from the same directory

I have a batch script to run powershell, how to set path to ps1 file if this file is in the same folder as executing BAT file? I use this but not working.
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""./Reboot.ps1""' -Verb RunAs}";
Read call /? and Links relative to the Batch Script:
You can get the pathname of the batch script itself with %0,
parameter extensions can be applied to this so %~dp0 will return the
Drive and Path to the batch script.
Use … -File ""%~dp0Reboot.ps1"" … (note that %~dp0 includes a trailing backslash!)
With ./Reboot.ps you address the current working directory. What you need is the directory of the BAT file. Use %~d0%~p0/Reboot.ps instead.
An alternative could be to temporary set the working directory to the BAT's directory:
pushd %~d0%~p%"
rem run script with ./Reboot.ps1
popd
Obviously, if your script "Reboot.ps1" actually reboots the PC, you can skip the popd.

Can call headless from cmd but not from powershell

In dos when I paste this command it works:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.com --screenshot=c:\test\google.png --headless --hide-scrollbars --window-size=1920,1080 --disable-gpu &
When I do the same in Powershell it doesn't. I guess my syntax not right ?
You have to use Start-Process in Powershell and parse the Arguments:
Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -ArgumentList "https://google.com","--screenshot=c:\test\google.png","--headless","--hide-scrollbars","--window-size=1920,1080","--disable-gpu"

running watch file in to schedule

Have a requirement of using batch file in Cognos to upload the output in SharePoint by using Windows batch file we are scheduling.
Here is my script:
#ECHO OFF
IF EXIST "D:\Dev\close report name-en-in" Call Powershell.exe -executionpolicy remotesigned -File 'D:\Dev\close\pwrshell.ps1'
GOTO END
:END
PowerShell script is used for uploading the script from source.
After running batch file the output is not uploaded to SharePoint (PowerShell script works fine when running alone).
Not sure if this works or not, but there is a simple error in your script.
Call Powershell.exe -executionpolicy remotesigned -File 'D:\Dev\close\pwrshell.ps1'
should be
Powershell.exe -executionpolicy remotesigned -File 'D:\Dev\close\pwrshell.ps1'
Here's some better ways to end the file.
exit /b [errorlevel]
This stops the script, and optionally set an errorlevel.
goto :eof
We don't need :EOF for this to work, goto :eof works standalone.