0Day Forums
Running a PS1 script using a batch file (.bat) - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: .bat & .wsf & .cmd (https://0day.red/Forum-bat-wsf-cmd)
+--- Thread: Running a PS1 script using a batch file (.bat) (/Thread-Running-a-PS1-script-using-a-batch-file-bat)



Running a PS1 script using a batch file (.bat) - mooneye506 - 07-23-2023

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\""



RE: Running a PS1 script using a batch file (.bat) - silvavnwwzsoxu - 07-23-2023

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'"



RE: Running a PS1 script using a batch file (.bat) - darceelqzdy - 07-23-2023

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%"






RE: Running a PS1 script using a batch file (.bat) - kerrycogbmkw - 07-23-2023

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:


RE: Running a PS1 script using a batch file (.bat) - Verghese308768 - 07-23-2023

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"