我正在编写此函数来询问用户他们想要使用哪个VM,他们必须从他们自己的Hyper-V列表中进行选择。之后,它会询问我是否要打开或关闭它。当我选择它时,我可以让所选的VM打开或关闭。但是,当我运行该功能时,它将尝试通过Hyper-V中的所有VM。我怎样才能获得它以便它可以打开或关闭我想要的VM?
(我还将返回并添加一个选项以保存VM,如果它已打开。)
Function get-ORS {
param ($R, $O, $S, $name, $off, $on)
$name= Read-Host "What VM would you like to turn on use?"
$IO= Read-Host "Would you like to turn on/off your VM?: [On] [Off]"
$R= Get-VM | where {$_.State -eq 'Running'}
$O= Get-VM | where {$_.State -eq 'Off'}
$S= Get-VM | Where {$_.State -eq 'saved'}
$Off= Get-VM | where {$_.state -eq 'Running'} | Stop-VM
$on= Get-VM | where {$_.State -eq 'Off'} | Start-VM
if ($IO -eq $on) {
try
{
Start-VM -Name $name
}
catch
{
echo ("Could not start" + $name)
}
}
elseif ($O){
try
{
Start-VM -Name $name
echo ("Starting "+ $name)
}
catch
{
echo "Could not start the VM."
}
}
elseif ($R){
try
{
echo "Your VM is currently running."
}
catch
{
echo "Something went wrong."
}
}
elseif ($S) {
try
{
Start-VM -Name $name
echo ("Starting your from a saved state " + $name)
}
catch
{
echo "Could not start the saved VM."
}
}
if ($IO -eq $off){
try
{
Stop-VM -Name $name
}
catch
{
echo ("Could not turn off " + $name)
}
}
}
这个函数/脚本有很多问题。特别是关于逻辑(用例)和构建这方面的努力方面。这真的是一个弗兰肯斯坦,这意味着,也许,你是从不同的来源复制和粘贴。格式化也很难理解。
你真的需要一次退出这个用例的方法,以确保你走的是正确的道路。
你有params,然后更多提示相同的信息。 if语句中的变量不在param或prompt部分中,因此它始终为空。
你特意要求所有虚拟机,......
$R = Get-VM | where {$_.State -eq 'Running'}
$O = Get-VM | where {$_.State -eq 'Off'}
$S = Get-VM | Where {$_.State -eq 'saved'}
$Off = Get-VM | where {$_.state -eq 'Running'} | Stop-VM
$on = Get-VM | where {$_.State -eq 'Off'} | Start-VM
...但你说你只想要一个,但在你的提示中你要求两个。
$name = Read-Host "What VM would you like to turn on use?"
$IO = Read-Host "Would you like to turn on/off your VM?: [On] [Off]"
你也有这些作为参数,所以这只是奇怪的。只在param块中输入你想要传递或提示的内容。
为什么你有一个pram块,然后有一堆读主机块?
这是多余的。您也可以在Param块中执行Read-Host。因此,您可以一次性传递所有参数,或者如果忘记了,则会出现提示。
param
(
$name = (Read-Host "What VM would you like to turn on use?"),
$IO = (Read-Host "Would you like to turn on/off your VM?: [On] [Off]")
)
$R = Get-VM | where {$_.State -eq 'Running'}
$O = Get-VM | where {$_.State -eq 'Off'}
$S = Get-VM | Where {$_.State -eq 'saved'}
$Off = Get-VM | where {$_.state -eq 'Running'} | Stop-VM
$on = Get-VM | where {$_.State -eq 'Off'} | Start-VM
您还可以使用验证集来确保用户只能选择一个选项,然后再键入它。
您还可以显示用于选择的VM列表,方法是使用动态参数集或使用Out-GridView供用户选择。
你有一个非常长的if语句,它只是switch语句的候选者,使它更简洁。
您将在if语句中传递所有客人名称。为什么,当你说你只想要一个。
这是因为这对你来说是全新的。这很好,但是在进入之前值得花一些快速的免费在线课程。如果是这样的话,请参阅这些讨论和问答。
查看这些资源。 PowerShell Resources
Helping out someone with PowerShell
还有这个...
恕我直言,你应该采用不同的方式来使它更简洁,更容易理解,扩展,维护和排除故障。
你所拥有的东西真的可以简化为现在你所展示的东西。同样,有很多方法可以做X或Y,这只是一种方式。
Function Get-Ors
{
[cmdletbinding()]
[Alias('ors')]
param
(
)
$VMName = ( Get-VM |
Select-Object -Property Name, State, Status |
Out-GridView -OutputMode Single -Title 'Select a target VM you want to use.')
$IO = ( 'On','Off'|
Out-GridView -OutputMode Single -Title 'Select On or Off to start or stop the target VM.')
switch ($IO)
{
On {
"Attempting to start $($VMName.Name)"
Start-VM -Name $VMName
}
Off {
"Attempting to stop $($VMName.Name)"
Stop-VM -Name $VMName.Name
}
default {Write-Warning -Message "$($VMName.Name) state could not be determined"}
}
}
如果对请求无效,PowerShell将根据当前状态自动生成响应。
如果您不关心内置的Out-GridView,您可以使用Show-Command cmdlet构造和使用messagebox或WinForm或WPF或使用PowerShell其他PoorMan的GUI方法。穷人的GUI方法无法自定义。