[英]Unable to run VCenter powershell script
我正在尝试连接到 VCenter 以提取一些性能数据。 当我从 powershell window 执行脚本时,出现错误:
这是我的脚本:
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
这些是错误:
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
我是 powershell 的新手,我真的很感谢这方面的帮助?
假设您已经安装了PowerCLI管理单元...。
您需要使用Add-PSSnapIn
添加适用于PowerShell的VMware vCenter管理单元。 最后我检查了一下,它仍然使用旧的管理单元模型而不是新的模块结构。
您应该具有“开始菜单”快捷方式以启动VMware PowerShell控制台,该控制台应会自动为您添加该管理单元。
在我的笔记本电脑上 (winver.exe => Windows 10 Enterprise Version 21H2 ) 我在管理员 powershell 中运行了这个:
PS C:\> Install-Module -Name VMware.VimAutomation.Core -verbose -Force -AllowClobber
我用-Force
来抑制提示。
我使用-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.
延伸阅读:
在较新的 PowerShell 版本中,您可以这样做:
将此行添加到我们脚本的顶部(本例中为“Get-Vms.ps”):
#Requires -Modules VMware.VimAutomation.Core
然后,如果您尝试在未安装该模块的情况下运行脚本,您不会收到一堆奇怪的错误消息,而是收到一条简单明了的错误消息:
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
延伸阅读: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.