简体   繁体   English

无法运行 VCenter powershell 脚本

[英]Unable to run VCenter powershell script

I am trying to connect to VCenter to pull some performance data.我正在尝试连接到 VCenter 以提取一些性能数据。 When I execute the the script from the powershell window, I get errors:当我从 powershell window 执行脚本时,出现错误:

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?我是 powershell 的新手,我真的很感谢这方面的帮助?

Assuming you've already installed the PowerCLI snap-in .... 假设您已经安装了PowerCLI管理单元...。

You need to use Add-PSSnapIn to add the VMware vCenter snap-in for PowerShell. 您需要使用Add-PSSnapIn添加适用于PowerShell的VMware vCenter管理单元。 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. 您应该具有“开始菜单”快捷方式以启动VMware PowerShell控制台,该控制台应会自动为您添加该管理单元。

Install vmware "PowerCli" module安装 vmware“PowerCli”模块

On my laptop (winver.exe => Windows 10 Enterprise Version 21H2 ) I ran this in an admin powershell:在我的笔记本电脑上 (winver.exe => Windows 10 Enterprise Version 21H2 ) 我在管理员 powershell 中运行了这个:

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

I used -Force to suppress prompt.我用-Force来抑制提示。

And I used -AllowClobber because without it I got this error:我使用-AllowClobber因为没有它我得到了这个错误:

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将 REQUIRES 语句添加到脚本以获得更好的错误消息

In newer PowerShell versions you can do this:在较新的 PowerShell 版本中,您可以这样做:

Add this line to the top of our script ("Get-Vms.ps" in this example):将此行添加到我们脚本的顶部(本例中为“Get-Vms.ps”):

#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延伸阅读: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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