malabelle
Enthusiast
Enthusiast

PowerCLI getting args from batch file problem

Jump to solution

Hi,

I'm stuck.

I'm trying to create a .cmd file to delete a VMDK file.

ex: delete.cmd viserver vm "[TEST_Sequence_Dev] SEQDEV9/SEQDEV9_1.vmdk"

delete.cmd

@echo off
REM VCENTER=%1
REM VM=%2=Nom de la VM
REM HardDisk=%3=Chemin et Nom du disque

powershell.exe -command "&'C:\Users\malabelle\Desktop\tests\delete-vmdk\delete-vmdk.ps1' %1 %2 %3"

delete-vmdk.ps1

Add-PSSnapin "Vmware.VimAutomation.Core"

$Vcenter = $args[0]
Connect-VIserver $Vcenter | Out-Null

$VM = $args[1]
$HardDisk = $args[2]
$status = (get-VM $VM).PowerState

    if($status -eq "PoweredOff")
        {Get-HardDisk -VM $VM | Where {$_.Filename -eq $HardDisk} | Remove-HardDisk -DeletePermanently -Confirm:$false}
    else {"Veuillez fermer la machine pour procéder"}

When I run it manually:

Get-HardDisk -VM SEQDEV9 | Where {$_.Filename -eq "[TEST_Sequence_Dev] SEQDEV9/SEQDEV9_1.vmdk"} | Remove-HardDisk -DeletePermanently -Confirm:$false

it works.

When I run it with the batch file, it connects to the viserver, take 3 seconds, and nothing (return to the prompt)

Any Idea?

vExpert '16, VCAP-DCA, VCAP-DCD
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

The problem is caused by the blank in the vmdk filename.

Try calling your cmd like this (single quotes inside the double qoutes for the third argument).

delete.cmd viserver vm "'[TEST_Sequence_Dev] SEQDEV9/SEQDEV9_1.vmdk'"


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

View solution in original post

0 Kudos
5 Replies
RvdNieuwendijk
Leadership
Leadership

Are you running the batch file with an account that has sufficient rights on the vCenter server?

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
malabelle
Enthusiast
Enthusiast

Yes I run it with admin rights.

I just tried modifiyng the ps1 to add -user -password, same thing.

thanks

vExpert '16, VCAP-DCA, VCAP-DCD
0 Kudos
malabelle
Enthusiast
Enthusiast

I just tried to specify the filename directly in the ps1 and it worked... So the problem is in the command line... Do you have an idea? thanks

Add-PSSnapin "Vmware.VimAutomation.Core"

$Vcenter = $args[0]
Connect-VIserver $Vcenter | Out-Null

$VM = $args[1]
$HardDisk = $args[2]
$status = (get-VM $VM).PowerState

    if($status -eq "PoweredOff")
        {Get-HardDisk -VM $VM | Where {$_.Filename -eq "[TEST_Sequence_Dev] SEQDEV9/SEQDEV9_1.vmdk"} | Remove-HardDisk -DeletePermanently -Confirm:$false}
       
    else {"Veuillez fermer la machine pour procéder"}

vExpert '16, VCAP-DCA, VCAP-DCD
0 Kudos
LucD
Leadership
Leadership

The problem is caused by the blank in the vmdk filename.

Try calling your cmd like this (single quotes inside the double qoutes for the third argument).

delete.cmd viserver vm "'[TEST_Sequence_Dev] SEQDEV9/SEQDEV9_1.vmdk'"


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

View solution in original post

0 Kudos
malabelle
Enthusiast
Enthusiast

You're right lol

Thanks Luc

vExpert '16, VCAP-DCA, VCAP-DCD
0 Kudos