VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to exit from the script after validation

Hi,

I am unable to exit or continue from script after validation

Please help

$vms = Read-Host "Enter VM Name "

if ($vms | where $_.'VM PowerState' -eq 'PoweredOn') {Exit}

Write-Host "Cannot Continue When VM is Powered On" -ForegroundColor Red

else

{

Write-Host

$newsizeGB = Read-Host "Enter New Disk Size In GB "

}

Error

Where-Object : Cannot validate argument on parameter 'Property'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At D:\myreports\vm1.ps1:146 char:18

+ if ($vms | where $_.'VM PowerState' -eq 'PoweredOn') {Exit}

+                  ~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Where-Object], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.WhereObjectCommand

Cannot Continue When VM is Powered On

else : The term 'else' 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 D:\myreports\vm1.ps1:148 char:1

+ else

+ ~~~~

    + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this.
You better replace the exit by a break, the exit will end your PowerShell session

$vmName = Read-Host "Enter VM Name "

$vm = Get-VM -Name $vmName

if ($vm.PowerState -eq 'PoweredOn') {

   Write-Host "Cannot Continue When VM is Powered On" -ForegroundColor Red

   break

}

else {

   Write-Host

   $newsizeGB = Read-Host "Enter New Disk Size In GB "

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this.
You better replace the exit by a break, the exit will end your PowerShell session

$vmName = Read-Host "Enter VM Name "

$vm = Get-VM -Name $vmName

if ($vm.PowerState -eq 'PoweredOn') {

   Write-Host "Cannot Continue When VM is Powered On" -ForegroundColor Red

   break

}

else {

   Write-Host

   $newsizeGB = Read-Host "Enter New Disk Size In GB "

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much Smiley Happy

0 Kudos