Automation

 View Only
  • 1.  Remove-Datastore from all Hosts

    Posted Mar 24, 2009 05:22 PM

    Hi Guys,

    I'm hoping someone can help, i can't work out why this mini-script isn't working! Basically, i just want to remove a Datastore from all ESX Hosts in Virtualcenter and am trying to do this by;

    $dsname = "ukdc228001:vmvol1 - Restore 01.09"

    1. Script .....

    Get-VC UKVIR09100

    Get-VMHost | Remove-Datastore -Datastore (get-datastore -Name $dsname)

    The first time i ran the script it deleted from the first host then failed. retrying it now brings up;

    (default is "Y"):a

    Remove-Datastore : 24/03/2009 17:20:32 Remove-Datastore 0FE195B3-76C4-475

    4-9D41-94013BE2DCD0 A specified parameter was not correct.

    At C:\Powershell\Add or Remove A Datastore\removedatastorefromallhosts.ps1:16 c

    har:30

    + Get-VMHost | Remove-Datastore <<<< -Datastore (get-datastore -Name $dsname)

    I'm assuming this is because the store no longer exists on the first host and so times out but not sure how to resolve this, maybe need some error (if exists) type checking but am a newbie!!!

    Any help would be greatly appreciated as have several stores to remove from 60 hosts!!

    Cheers,

    Sean.



  • 2.  RE: Remove-Datastore from all Hosts

    Posted Mar 25, 2009 03:24 AM

    Sean,

    You might want to toss out some information on what exactly you're trying to do.

    If you're deleting a datastore that is visible to more than one host, then yes, the second host will error out since it hasn't rescanned.

    If you're like most SAN / NFS / iSCSI type setups, then you have the same datastore on all hosts in a cluster. If that is the case, then you just need to grab one host per cluster and remove the datastore, then refresh the storage on the hosts in the cluster.

    Otherwise if you have your storage setup differently, or with local datastores that would change it quite a bit.

    if you need to rescan, Get-Vmhoststorage -RescanAllHBA will be your friend.



  • 3.  RE: Remove-Datastore from all Hosts

    Posted Mar 25, 2009 08:23 AM

    Morning!

    Sure, basically, i have a number of NFS datastores added to all my hosts (e.g. "ukdc228001:vmvol1 Restore 01.03"). They are there because of a Filer problem we had recently and are restored datastores. We have been using SVMotion to move all the VM's back out of them to their correct homes. We have now done that and simply need to remove the datastores from the hosts as they are no longer needed. We could obviously do this manually but have 5 Datastores to remove from 60 hosts!

    The other thing to throw into the mix is that the datastores don't exist on every single host as at the time they were only added to the hosts that needed them (approx. 70% of them).

    What i'm trying to do is remove these datastores from the hosts via a script. There are no VM's in any of these stores any more but i suspect that the script i mentioned above (if you can call it a script!) is failing because it's coming across a host that doesn't have the datastore and producing an error which causes the script to fail.

    What i think i need to do is modify the script above to check for the existence of the datastore per host then remove it if it does exist. I'm currently using VI Toolkit v1, haven't moved over to 1.5 yet as our build is based on v1 and am worried i may break it by upgrading and haven't had a chance to test yet...

    As i say, any help would be greatly appreciated as i don't fancy the old Virtual Center, Config, remove datastore x 100's of Datastores :smileyhappy:

    Many thanks,

    Sean.



  • 4.  RE: Remove-Datastore from all Hosts

    Posted Mar 25, 2009 08:24 AM

    Hi,

    I guess you are trying to remove an NFS datastore from all hosts that it is attached to? Yes the error "A specified parameter is not correct" you see is because the storage does not exist on one of the hosts. Noramaly this should be a non-terminating error and the operation should continue for the rest of the ESX objects on the pipe but in the current version of the toolkit there is a bug and the operation stops. Here is a workaround that I've tried and it should workout for you:

    Get-VmHost | Foreach-Object {Remove-Datastore -Datastore (Get-Datastore -Name $dsname) -VmHost $_ }

    This will throw errors(warnings) for the hosts on which $dsname datastore is not attached but the operation will continue and the datastore will be removed from the rest. If yot don't want to see the errors in the console just change the ErrorActionPreference global variable to $ErrorActionPreference = SilentlyContinue

    Hope this will help

    \Yavor



  • 5.  RE: Remove-Datastore from all Hosts

    Posted Mar 25, 2009 08:40 AM

    Wow, easy when you know how eh!?

    That's fantastic, thanks very much for that!!

    I also added -Confirm:$false to the line making;

    Get-VmHost | Foreach-Object {Remove-Datastore -Confirm:$false -Datastore (Get-Datastore -Name $dsname) -VmHost $_ }

    As it was prompting me to confirm for each host.

    Thanks again, your a lifesaver :smileyhappy:

    Sean.



  • 6.  RE: Remove-Datastore from all Hosts

    Posted Mar 25, 2009 08:40 AM

    Wow, easy when you know how eh!?

    That's fantastic, thanks very much for that!!

    I also added -Confirm:$false to the line making;

    Get-VmHost | Foreach-Object {Remove-Datastore -Confirm:$false -Datastore (Get-Datastore -Name $dsname) -VmHost $_ }

    As it was prompting me to confirm for each host.

    Thanks again, your a lifesaver :smileyhappy:

    Sean.



  • 7.  RE: Remove-Datastore from all Hosts

    Posted Mar 25, 2009 01:58 PM

    You could add an If statement in there to only run the command if it sees that datastore.

    And add that -confirm:$false like you said.

    Get-VmHost | Foreach-Object {if(Get-Datastore -Vmhost $_ | where {$_.Name -eq $dsname}) {Remove-Datastore -Datastore (Get-Datastore -Name $dsname) -VmHost $_ }}