【发布时间】:2018-12-03 18:10:00
【问题描述】:
好吧 - 真的很挑剔,但我讨厌两次编写相同的代码(即使此时它会为我节省更多时间)
所以我正在编写一个 PowerCLI 函数,它有 2 个开关参数来查找开机或关机事件。
看
function pGet-PowerEvent {
[cmdletbinding()]
PARAM (
[parameter(ValueFromPipeline=$true,
Mandatory=$true,
Position=0)]
[string[]]
$Entity,
[parameter(ValueFromPipeline=$true,
Mandatory=$false,
Position=1)]
[switch]
$onEvent,
[parameter(ValueFromPipeline=$true,
Mandatory=$false,
Position=2)]
[Switch]
$offEvent
)
if ($onEvent) {
$EventType = "VmPoweredOnEvent"
}
if ($offEvent) {
$EventType = "VmPoweredOffEvent"
}
$entity.ForEach{write-host $_; Get-VIEvent -Entity $_ -MaxSamples([int]::MaxValue) | ?{$_ -is [vmware.vim."$EventType"] |
select createdtime, username }
}
并运行命令:
pGet-PowerEvent -Entity $vm -OnEvent
和错误:
Cannot convert the "[VMware.Vim.VmPoweredOffEvent]" value of type "System.String" to type "System.Type".
但是,当我运行此命令时 - 它会将 [vmware.vim.vmpoweredoffevent] 视为字符串,而不是类型。
但我需要变量是类型。并将变量的值作为类型。
谢谢
【问题讨论】:
-
请编辑问题并包含用于调用
pGet-PowerEvent函数的命令。
标签: powershell powercli