Hi
I have a script to create a new hard disk for the VM
How to modify my script to skip the the harddisk creation if the hardisk1 cell does not have a value(empty)!
$harddisk1 = $Data.harddisk1
$DatastoreName1 = $Data.DatastoreName1
$Name | New-HardDisk -CapacityGB $harddisk1 -Datastore $DatastoreName1 -Persistence persistent
Hello, VicMware-
You can just wrap a conditional statement around that to check that the values for harddisk1 and DatstoreName1 are non null, non empty string values. Like:
## if neither item is null or an empty string, then add the new hard disk
if ($Data.harddisk1 -and $Data.DatastoreName1) {
"Ok, will do something for $Name"
$harddisk1 = $Data.harddisk1
$DatastoreName1 = $Data.DatastoreName1
$Name | New-HardDisk -CapacityGB $harddisk1 -Datastore $DatastoreName1 -Persistence persistent
} ## end if
## else, do something else
else {"hm, maybe missing some data for VM $Name"}
Or, if you wanted to be a bit more explicit with the line that checks for null/empty, you could replace the "if" line above with:
if (![string]::IsNullOrEmpty($Data.harddisk1) -and ![string]::IsNullOrEmpty($Data.DatastoreName1)) {
...
Those should work [nearly] indentically. How do they do for you?
Hello, VicMware-
You can just wrap a conditional statement around that to check that the values for harddisk1 and DatstoreName1 are non null, non empty string values. Like:
## if neither item is null or an empty string, then add the new hard disk
if ($Data.harddisk1 -and $Data.DatastoreName1) {
"Ok, will do something for $Name"
$harddisk1 = $Data.harddisk1
$DatastoreName1 = $Data.DatastoreName1
$Name | New-HardDisk -CapacityGB $harddisk1 -Datastore $DatastoreName1 -Persistence persistent
} ## end if
## else, do something else
else {"hm, maybe missing some data for VM $Name"}
Or, if you wanted to be a bit more explicit with the line that checks for null/empty, you could replace the "if" line above with:
if (![string]::IsNullOrEmpty($Data.harddisk1) -and ![string]::IsNullOrEmpty($Data.DatastoreName1)) {
...
Those should work [nearly] indentically. How do they do for you?