PowerCLI

 View Only
  • 1.  Run a command on ESXi using PowerCLi

    Posted Oct 02, 2018 07:42 AM

    Hello, I just want to run a script that install a package on some of my hosts, the command is like this:

    > ./path/packagename

    Thanks,



  • 2.  RE: Run a command on ESXi using PowerCLi

    Posted Oct 02, 2018 07:51 AM

    Do you, or can you, have SSH enabled on these ESXi nodes?



  • 3.  RE: Run a command on ESXi using PowerCLi

    Posted Oct 02, 2018 07:54 AM

    Yes, it's enabled; I just want to automate the task with one command instead of logging into each one.



  • 4.  RE: Run a command on ESXi using PowerCLi

    Posted Oct 02, 2018 07:59 AM

    Unless that command is also available through for example esxcli, and thus Get-EsxCli, you will have to logon to the ESXi nodes.
    When done through a script it doesn't really matter how many lines/commands are in there I guess.

    Or perhaps I don't understand what you mean?



  • 5.  RE: Run a command on ESXi using PowerCLi

    Posted Oct 02, 2018 08:08 AM

    I'm upgrading a firmware on all of the hosts (rebooting is not required), so I need to automate this instead of visiting each host. The command is just by running the script that deploys the package.

    Is this doable via PowerCLi?



  • 6.  RE: Run a command on ESXi using PowerCLi

    Posted Oct 02, 2018 09:40 AM

    Yes, but you will need the Posh-Ssh module.

    Then you can do (replace the content of the $cmd variable with your command)

    #requires -Modules posh-ssh

    $cmd = 'date'

    $user = 'root'

    $pswd = 'VMware1!'

    $pswdSec = ConvertTo-SecureString -String $pswd -AsPlainText -Force

    $cred = New-Object System.Management.Automation.PSCredential($user,$pswdSec)

    Get-VMHost | ForEach-Object -Process {

        if((Get-VMHostService -VMHost $_).where({$_.Key -eq 'TSM-SSH'}).Running){

            $ssh = New-SSHSession -ComputerName $_.Name -Credential $cred -AcceptKey -KeepAliveInterval 5 -Verbose

            Invoke-SSHCommand -SessionId $ssh.SessionId -Command $cmd -TimeOut 30

            Remove-SSHSession -SessionId $ssh.SessionId

        }

    }