Hey all
I am having problems with a function I've tried to create which does not seem to be working properly and was hoping some one will be able to explain why ???? The section in red does not seem to be doing what I am wanting. That is, I would like to change the string object into a 'VirtalmachineImpl' object but it does not seem to be working. The verbose statements on either side keep showin the aboject to be a string one. I'm kinda sure that if this would work the rest of my function will work, its just that this $VM variable does NOT seem to allow me to chnage it from a string value into a VirtualMachineImpl object ?!?!? If some one could explain why that would be great.
As usual many thanks in advance
Munster
Function Kill-VM {
[Cmdletbinding(SupportsShouldProcess=$true,
ConfirmImpact='Med')]
PARAM(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
Position=0)]
[ValidateNotNullOrEmpty()]
[String]$VM,
[Parameter(Mandatory=$true,
HelpMessage = "KillType can ONLY be SOFT,HARD or FORCE",
Position=1)]
[ValidateSet('soft','hard','force')]
[String]$KillType
)
BEGIN{
Write-Verbose $VM.GetType().Name
If ($VM.GetType().Name -eq "string"){
Try {
Write-Verbose "The Host is $VM.host"
$VM = Get-VM $VM -ErrorAction Stop
Write-Verbose "The Host is $($VM.host)"
}
Catch [Exception]{
Write-Warning "$VM is NOT a VM"
continue
}
}
Elseif ($VM -isnot [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl]){
Write-Warning "You did not pass in a String or a VM object"
continue
}
}
PROCESS{
Try{
#$VM = Get-VM $VM
If ($VM.PowerState -eq "PoweredOff") {
Write-Warning "$($VM.Name) is already Powered Off" }
Else {
Write-Verbose "The Host is $VM.host"
#$esxcli = Get-EsxCli -VMHost ($VM.Host)
#$WorldID = ($esxcli.vm.process.list() | Where {$_.DisplayName -eq $VM.Name}).WorldID
#$result = $esxcli.vm.process.kill($KillType, $WorldID)
#if ($result -eq "true"){
#Write-Output "$($VM.Name) killed via a $KillType kill" }
#Else { $result }
}
}
Catch [Exception]{ THROW "Unable to get VM details, please confirm the VM exists." }
}
END{ }
}