Hmm Sorry I am not a good coder... just an poor admin *g*
I run this simple script (Just to test the function, I dont use a while loop etc etc)
script = delete_f_test.ps1
This is the content (exept VMNAME and VCIP):
##################################################
function remove-HD {
param($VMname, $HDname, $Delflag, $Server)
if(!$Server){
$server = $defaultVIServer
}
$vm = Get-View -Server $server -ViewType VirtualMachine -Filter @{"Name"=$VMname}
foreach($dev in $vm.Config.Hardware.Device){
if ($dev.DeviceInfo.Label -eq $HDname){
$key = $dev.Key
$name = $dev.Backing.FileName
$disktype = $dev.Backing.GetType().Name
}
}
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.deviceChange = @()
$spec.deviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDevice
$spec.deviceChange[0].device.key = $key
$spec.deviceChange[0].operation = "remove"
$vm.ReconfigVM($spec)
if ($Delflag){
$serviceInstance = get-view ServiceInstance -Server $Server
$fileMgr = Get-View $serviceInstance.Content.fileManager
$datacenter = (Get-View (Get-VM $VMname -Server $Server | Get-Datacenter).ID).get_MoRef()
$fileMgr.DeleteDatastoreFile_Task($name, $datacenter)
switch($disktype){
"VirtualDiskRawDiskMappingVer1BackingInfo" {
$ext = "-rdmp.vmdk"
}
"VirtualDiskFlatVer2BackingInfo" {
$ext = "-flat.vmdk"
}
}
$name2 = $name.substring(0,$name.length - 5) + $ext
$fileMgr.DeleteDatastoreFile($name2, $datacenter)
}
}
echo "start"
remove-HD "VNAME" "Hard Disk 3" $TRUE "VCIP"
#########################################################
And that is the error message:
[vSphere PowerCLI] C:\Programme\VMware\Infrastructure\vSphere PowerCLI\Scripts> delete_f_test.ps1
start
Method invocation failed because [System.Object[]] doesn't contain a method named 'ReconfigVM'.
At C:\Programme\VMware\Infrastructure\vSphere PowerCLI\Scripts\delete_f_test.ps1:25 char:20
+ $vm.ReconfigVM <<<< ($spec)
+ CategoryInfo : InvalidOperation: (ReconfigVM:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Type Value
---- -----
Task task-12148
You cannot call a method on a null-valued expression.
At C:\Programme\VMware\Infrastructure\vSphere PowerCLI\Scripts\delete_f_test.ps1:42 char:35
+ $name2 = $name.substring <<<< (0,$name.length - 5) + $ext
+ CategoryInfo : InvalidOperation: (substring:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "DeleteDatastoreFile" with "2" argument(s): "Invalid datastore path ''."
At C:\Programme\VMware\Infrastructure\vSphere PowerCLI\Scripts\delete_f_test.ps1:43 char:39
+ $fileMgr.DeleteDatastoreFile <<<< ($name2, $datacenter)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
[vSphere PowerCLI] C:\Programme\VMware\Infrastructure\vSphere PowerCLI\Scripts>
Can I use a Folder instead (like get-folder disk_change | get-vm) what will be the synthax then?