VMware Cloud Community
djewt1
Contributor
Contributor

Powercli workflow cmdlets not recognized

I am trying to script a downtime event for all our Linux servers.

Because our enviroment is to big for one powershell script that runs on a server by server basis I tried to make a workflow.

But everytime I run the script it fails because it cannot recognize the cmdlets from Powercli.

Any idea's on how to make the cmdlets work?

Script:

#connect to VMware

Import-module VMware.VimAutomation.Core

Connect-VIServer vcenter

#make list of all powered off Linux VM's

    $poweredoffLinuxVM = Get-VM | Where-Object { $_.PowerState -eq "PoweredOff" -and $_.ExtensionData.Config.GuestId -like "rhel*" }

    $poweredoffLinuxVM | Select-Object Name | ConvertTo-Csv -NoTypeInformation | select-Object -Skip 1 | % {$_.Replace('"','')} | set-content -Path "C:\temp\LogsLinuxPentacost\PoweroffLinuxHost.csv"

    $LinuxVMs = Get-VM | Where-Object { $_.PowerState -ne "PoweredOff" -and $_.ExtensionData.Config.GuestId -like "rhel*" }

    $LinuxVMs | Select-Object Name | ConvertTo-Csv -NoTypeInformation | select-Object -Skip 1 | % {$_.Replace('"','')} | set-content -Path "C:\temp\LogsLinuxPentacost\upLinuxservers.csv"

Workflow ShutdownLinux {

    param(

    [string]$vcenter,

    [string[]]$names,

    [string]$session

    )

    $exclude = get-content C:\temp\LogsLinuxPentacost\excludelist.txt

    $excludedclusters = get-content C:\temp\LogsLinuxPentacost\excludedclusters.txt

   

#foreach Vm that is not excluded perform action.

foreach -parallel ($name in $names){

$checklinux = InlineScript{

    Connect-VIserver -Server $Using:vcenter -Session $Using:session | Out-Null

    if ($using:exclude -contains $using:name){

    #excluded VM's print to file

    echo "$Using:name" | out-file -append C:\temp\LogsLinuxPentacost\excludedfromreboot.txt

    }

else {

        if ($Using:excludedclusters -contains (get-vm $Using:name | Get-VMHost).Parent.Name){

        echo "$Using:name" | out-file -append C:\temp\LogsLinuxPentacost\excludedfromrebootbycluster.txt

        }

        else {

        echo "$Using:name" | out-file -append C:\temp\LogsLinuxPentacost\includedinreboot.txt

            }

        }

}

$checklinux.name

}

}

$vmnames = get-content C:\temp\LogsLinuxPentacost\upLinuxservers.csv

ShutdownLinux -names $vmnames -vcenter $global:DefaultVIServer.Name -session $global:DefaultVIServer.SessionSecret

Error message:

Connect-VIserver : The term 'Connect-VIserver' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was

included, verify that the path is correct and try again.

At ShutdownLinux:41 char:41

+

    + CategoryInfo          : ObjectNotFound: (Connect-VIserver:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

    + PSComputerName        : [localhost]

get-vm : The term 'get-vm' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the

path is correct and try again.

At ShutdownLinux:41 char:41

+

    + CategoryInfo          : ObjectNotFound: (get-vm:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

    + PSComputerName        : [localhost]

Error message when import-module is added in InlineScript:

Import-Module : The specified mount name 'vmstores' is already in use.

At ShutdownLinux:42 char:42

+

    + CategoryInfo          : InvalidOperation: (:) [Import-Module], DriveException

    + FullyQualifiedErrorId : NewDriveProviderException,Microsoft.PowerShell.Commands.ImportModuleCommand

    + PSComputerName        : [localhost]

Import-Module : The specified mount name 'vis' is already in use.

At ShutdownLinux:42 char:42

+

    + CategoryInfo          : InvalidOperation: (:) [Import-Module], DriveException

    + FullyQualifiedErrorId : NewDriveProviderException,Microsoft.PowerShell.Commands.ImportModuleCommand

    + PSComputerName        : [localhost]

Reply
0 Kudos
2 Replies
frostyk
Enthusiast
Enthusiast

Have you installed PowerCLI on whatever system you are running your script from?  The powerCLI modules for Powershell need to be installed and loaded to run those commandlets you are using.  It looks like its trying to load them at the start of the script, but if you haven't installed them on that system it wont work.  You can download them from VMware:

Download VMware vSphere

Also, the Connect-VIserver line should include your vcenter name, unless your vcenter name is just "vcenter".

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

Take a look at the correct answer in WorkFlows with PowerCLI?  for a way to use workflows in PowerCLI.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos