PowerCLI

 View Only
  • 1.  Running a powercli from powershell

    Posted Mar 23, 2013 07:06 PM

    The first script reads through a list of files and starts jobs using the second script...
    the first echo works great and write the VM name to a file but I don't get any of the other echoes and it stops processing ...
    I can run the second script by itself if I just pass a parameter to it and I will get all the echoes and the CSV file...
    Any advice?
    I start the first script (or the second one if I run it by itslef) this way:
    C:\Windows\System32\WindowsPowerShell\v1.0>powershell.exe D:\Powercli\Scripts\Storage\test2.ps1

    Two scripts: the calling script:
    #Multithreads Jobs
    "Killing existing jobs . . ."
    Get-Job | Remove-Job -Force
    "Done."

    $vms = Get-Content D:\Powercli\Scripts\Storage\ImportVMs.txt
    foreach ($vm in $vms){
       while ((Get-Job -State Running).length -ge 10)
       {
       Start-Sleep -seconds 3
       }
       Start-Job -ScriptBlock {& D:\Powercli\Scripts\Storage\WriteVMs.ps1 $using:vm } -Name $VM
       Start-Sleep -s 10
    }

    The called script - D:\Powercli\Scripts\Storage\WriteVMs.ps1
    param(
       [string]$VM
       )

    echo $vm >> "D:\Powercli\Scripts\Storage\args.txt"


    #Load PowerCLI module in PowerShell
    Add-PSSnapin VMware.VimAutomation.Core
    Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Confirm:$False

    echo snapinloaded >> "D:\Powercli\Scripts\Storage\args.txt"

    #Initialize
    $vCenterHost = "server.test.com"
    $user = "ID-Here"
    $password = "PW-Here"


    echo initialized >> "D:\Powercli\Scripts\Storage\args.txt"


    #Connect to VCenter server
    Connect-Viserver $vCenterHost -user $user -password $password

    echo connected >> "D:\Powercli\Scripts\Storage\args.txt"

    Get-VM -Name $VM | Export-Csv -NoTypeInformation -UseCulture -append -Path "D:\Powercli\Scripts\Storage\exported.csv"

    echo gotVM >> "D:\Powercli\Scripts\Storage\args.txt"



  • 2.  RE: Running a powercli from powershell

    Posted Mar 23, 2013 08:10 PM

    Isn't the $Using scope intended for workflows and inline scripts ?

    Did you try calling the script without the $Using scope on the parameter ?



  • 3.  RE: Running a powercli from powershell

    Posted Mar 24, 2013 02:20 PM

    I did a lot of research on this and found the $using parameter (since PowerShell 3.0) to be better suited than using the ArgumentList…Please note that $using parameter is working since the echo in the called script correctly echoes the VM name into the output file:

    "D:\Powercli\Scripts\Storage\args.txt"

    To answer your question, yes I did try the script without the $using and nothing past the echo works.

    Your help is much appreciated.



  • 4.  RE: Running a powercli from powershell

    Posted Mar 24, 2013 03:24 PM

    Did you try it like this

    "Killing existing jobs . . ."
    Get-Job | Remove-Job -Force
    "Done."
    $vms
    = Get-Content D:\Powercli\Scripts\Storage\ImportVMs.txt
    foreach ($vm in $vms){     while ((Get-Job -State Running).length -ge 10)     {         Start-Sleep -seconds 3
        }     Start-Job -ScriptBlock {& D:\Powercli\Scripts\Storage\WriteVMs.ps1} -ArgumentList $vm.Name -Name $VM
        Start-Sleep -s 10
    }

    That seems to work for me.



  • 5.  RE: Running a powercli from powershell

    Posted Mar 24, 2013 03:48 PM

    args.txt file (from the called script) was empty -(no VM names) and the jobs did not produce any results (CSV file was not created).



  • 6.  RE: Running a powercli from powershell

    Posted Mar 24, 2013 04:24 PM

    The followings work the same way: arg.txt is updates with the VM name but the jobs do not run:

      Start-Job -ScriptBlock {& D:\Powercli\Scripts\Storage\WriteVMs.ps1 $using:vm } -Name $VM
      Start-Job -FilePath       D:\Powercli\Scripts\Storage\WriteVMs.ps1  -ArgumentList $vm -Name $VM



  • 7.  RE: Running a powercli from powershell

    Posted Mar 24, 2013 05:15 PM

    The first PowerShell is launched by using powershell.exe xxxx.ps1

    It passes the name it reads from a file to the second PowerShell script – as evidenced by echo in the second script.

    But as soon as the second (called) script runs ADD-PSSnapin…it fails:

    #Load PowerCLI module in PowerShell

    Add-PSSnapin VMware.VimAutomation.Core



  • 8.  RE: Running a powercli from powershell

    Posted Mar 24, 2013 05:37 PM

    One more "helpful" note...I can use powershell commands in the calles scripts and they work, such as:

    write-output hello >> "D:\Powercli\Scripts\Storage\args.txt"

    but can't run Add-PSSnapin



  • 9.  RE: Running a powercli from powershell

    Posted Mar 25, 2013 02:02 PM

    For the benefit of others, I have documented how I made this process work...

    To utilize multi-threading in PowerShell:

    Script one creates/starts jobs to run script two.

    If script one (the calling script) is started using:

    C:\Windows\System32\WindowsPowerShell\v1.0>powershell.exe\Script1.ps1

    then PowerCLI/PowerShell commands will not work in script two.  For example if you have the following in script two, it will fail:

    Add-PSSnapin VMware.VimAutomation.Core

    However if you start the first script from a PowerCLI console then all will be well…

    Here is the shortcut for PowerCLI console:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -c ". \"C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\""

    And from this console enter:

    & "D:\Powercli\Scripts\Storage\ Script1.ps1"