简体   繁体   中英

Unable to run VCenter powershell script

I am trying to connect to VCenter to pull some performance data. When I execute the the script from the powershell window, I get errors:

This is my script:

Connect-VIServer "vcenter.server.com" -User user123 -Password testpassword
$allvms = @()
$allhosts = @()
$hosts = Get-VMHost
$vms = Get-Vm

foreach($vmHost in $hosts){
  $hoststat = "" | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
  $hoststat.HostName = $vmHost.name

  $statcpu = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -stat cpu.usage.average
  $statmem = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -stat mem.usage.average

  $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
  $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum

  $hoststat.CPUMax = $cpu.Maximum
  $hoststat.CPUAvg = $cpu.Average
  $hoststat.CPUMin = $cpu.Minimum
  $hoststat.MemMax = $mem.Maximum
  $hoststat.MemAvg = $mem.Average
  $hoststat.MemMin = $mem.Minimum
  $allhosts += $hoststat
}
$allhosts | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "c:\output\Hosts.csv" -noTypeInformation

foreach($vm in $vms){
  $vmstat = "" | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
  $vmstat.VmName = $vm.name

  $statcpu = Get-Stat -Entity ($vm)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -stat cpu.usage.average
  $statmem = Get-Stat -Entity ($vm)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10-stat mem.usage.average

  $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
  $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum

  $vmstat.CPUMax = $cpu.Maximum
  $vmstat.CPUAvg = $cpu.Average
  $vmstat.CPUMin = $cpu.Minimum
  $vmstat.MemMax = $mem.Maximum
  $vmstat.MemAvg = $mem.Average
  $vmstat.MemMin = $mem.Minimum
  $allvms += $vmstat
}
$allvms | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "c:\output\VMs.csv" -noTypeInformation

These are the error:

The term 'Connect-VIServer' is not recognized as the name of a cmdlet, function, script file, or operable program. Chec
k the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\vm.ps1:1 char:17
+ Connect-VIServer <<<<  dc1prhsvspvc-01 -User haquem -Password Basketball1
    + CategoryInfo          : ObjectNotFound: (Connect-VIServer:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'Get-VMHost' 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 C:\scripts\vm.ps1:4 char:20
+ $hosts = Get-VMHost <<<<
    + CategoryInfo          : ObjectNotFound: (Get-VMHost:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'Get-Vm' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spel
ling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\vm.ps1:5 char:14
+ $vms = Get-Vm <<<<
    + CategoryInfo          : ObjectNotFound: (Get-Vm:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'Get-Stat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the sp
elling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\vm.ps1:11 char:22
+   $statcpu = Get-Stat <<<<  -Entity ($vmHost)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -stat cpu
.usage.average
    + CategoryInfo          : ObjectNotFound: (Get-Stat:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'Get-Stat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the sp
elling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\vm.ps1:12 char:22
+   $statmem = Get-Stat <<<<  -Entity ($vmHost)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -stat mem
.usage.average
    + CategoryInfo          : ObjectNotFound: (Get-Stat:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I am very new to powershell, I really appreciate any help with this?

Assuming you've already installed the PowerCLI snap-in ....

You need to use Add-PSSnapIn to add the VMware vCenter snap-in for PowerShell. Last I checked, it still uses the old snap-in model instead of the newer module structure.

You should have a Start Menu shortcut to launch the VMware PowerShell console, which should automatically add the snap-in for you.

Install vmware "PowerCli" module

On my laptop (winver.exe => Windows 10 Enterprise Version 21H2 ) I ran this in an admin powershell:

PS C:\> Install-Module -Name VMware.VimAutomation.Core -verbose -Force -AllowClobber

I used -Force to suppress prompt.

And I used -AllowClobber because without it I got this error:

PackageManagement\Install-Package : The following commands are already available on this system:'Get-Cluster,New-Cluster,Remove-Cluster'. This module 'VMware.VimAutomation.Core' may override the existing commands. If you still want to install this module
'VMware.VimAutomation.Core', use -AllowClobber parameter.

Further reading:

Add REQUIRES statement to script for nicer error messages

In newer PowerShell versions you can do this:

Add this line to the top of our script ("Get-Vms.ps" in this example):

#Requires -Modules VMware.VimAutomation.Core

Then if you try to run the script without having that module installed, you don't get a bunch of weird error messages, but instead get a single straight-forward error message:

PS C:\> .\Get-VMs.ps1
.\Get-VMs.ps1 : The script 'Get-VMs.ps1' cannot be run because the following modules that are specified by the "#requires" statements of the script are missing: VMware.VimAutomation.Core.
At line:1 char:1
+ .\Get-VMs.ps1
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (Get-VMs.ps1:String) [], ScriptRequiresException
    + FullyQualifiedErrorId : ScriptRequiresMissingModules

Further reading: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM