3 Replies Latest reply on Jan 12, 2013 7:58 PM by Manoj NameToUpdate

    Running Powershell and PowerCLI scripts with nexec

    Mike Reider

      Hello, Ive been trying to get my PowerCLI scripts to run in order to generate a list of all VMs in my vCenter


      I can get a basic powershell script to run with NSH script job, the syntax looks like this,


      nexec -i -e cmd /c "echo .|powershell C:\Temp\test_powershell.ps1"


      this is a basic powershell script that generates a text file, nexec kicks it off with no problem.


      When I try to run a PowerCLI script, which is also a ps1 script with VM cmdlets, I can't get it to run via nexec.


      nexec -i -e cmd /c "echo .|powershell -PSConsoleFile C:\Program Files/(x86)\VMware\Infrastructure\vSphere/PowerCLI\vim.psc1 -command C:\Temp\test_powercli.ps1"


      I can run this PowerCLI script locally on the target via a .bat file, like this,


      @echo off


      %SystemRoot%\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -command "&{c:\Temp\test_powercli.ps1}"


      Does anyone have examples of running PowerCLI via nexec?

        • 1. Re: Running Powershell and PowerCLI scripts with nexec
          Mike Reider

          we found a workaround to this issue,


          we are now simply running a powershell script and adding a PowerCLI snap in into the ps script,



          powershell script "test_powershell.ps1"



          Set-ExecutionPolicy RemoteSigned

          Add-PSSnapIn -Name VMWare.VimAutomation.Core

          & "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"


          ## vcp.txt stores vCenter login password

          $vcp = [IO.File]::ReadAllText("C:\Temp\vcp.txt")


          #VM target stored in hostname.txt

          $hostname = [IO.File]::ReadAllText("C:\Temp\hostname.txt")

          Connect-VIServer -Server localhost -WarningAction SilentlyContinue -Protocol https -User Administrator -Password $vcp


          #Get powerstate of selected VM and store in a csv file

          Get-VM -Name $hostname | Select Powerstate | Export-CSV "C:\Temp\power.csv" -NoTypeInformation



          To run this script, place the powershell script above on the vCenter, then run the following as an NSH script job targeted to the vCenter


          nexec -i -e cmd /c "echo .|powershell C:\Temp\test_powershell.ps1"

          • 2. Re: Running Powershell and PowerCLI scripts with nexec
            Joe Piotrowski

            Thanks for posting this Mike.

            • 3. Re: Running Powershell and PowerCLI scripts with nexec


                Running PowerShell / or Powershell based scripts through nexec face this problem because of a limitation in Powershell. Following text which is part of documentation, will solve the problem.

                In nutshell, you need to provide flag "-InputFormat none" to Powershell while invoking it from nexec.



              powershell scripts when invoked from nsh using nexec command or from an extended object with remote execution

              tend to hang; waiting for an input (or atleast an "enter" keystroke) from user even when none is required.

              This is seen always with PowerShell v1 and at times with PowerShell v2 as well.

              The problem is owing to a bug with powershell when it is invoked using windows APIs as is seen in a problem documented here:




              1. Upgrade to Powershell v2 and

              2. Use the powershell flag: "-InputFormat none" when invoking powershell for a script.