Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Currently this is my path for launching 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\""
share|improve this question

2 Answers 2

up vote 2 down vote accepted

What you would want to do is to replace the end of command where it calls out to Initialize-PowerCLIEnvironment.ps1 and replace that with the path to your sample.ps1 file.

You will almost certainly need to have the first line of your sample file be the original reference to Initialize-PowerCLIEnvironment.ps1 such that the initialization code (most likely function/cmdlet definitions) is run prior to any of your code that may depend on it.

For example, the contents of your sample.ps1 file would look something like this:

# source vSphere PowerCLI Environment first
. "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
# custom code follows
$me = "Hello, vSphere and PowerCLI!!!"
share|improve this answer

echo off

Title,Sample Script &color 9e

for /f "usebackq delims=$" %%a in (cd) do ( set SCRIPTDIR=%%a )

(Set ScriptFile=%SCRIPTDIR%\Sample.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%"

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.