Script Help: VM-> data store Associations

Hello world

Our VM names usually end with a number (WEB1, WEB2, etc.) and our data store names also end in a number. For redundancy of the application, we use warehouses of data whose names ending with numbers for the storage needed by VMs whose names ending in even numbers and data warehouses with names ending with odd numbers for the storage needed by VMs whose names ending with odd numbers. In the physical world, odd numbered data warehouses and the presenters are physically separated from those even.

I tried to create a PowerCLI script to report incompatibilities (people make mistakes) so that I can know if any VM storage on the 'wrong' type of data store (same VM on odd storage or vice versa).

I'm really green with PowerShell and PowerCLI and suspect that there is a way to use the Get - ViewType VirtualMachine view, but have failed so far.

In my environment most VMs use more than one data store, so the script needs to check each store data used by each virtual computer for a mismatch and I want to run the report against my whole environment (a single VC server).

If anyone knows something similar that has already been done or can point me in the right direction, I would really appreciate it.

-Matt

The attached script uses a regular expression to extract the number of the guestname and the datastorename.

Then it uses the modulus of the division by 2 to check if it's a curious (1) or (0) number the same.

When you compare these between the client and the data store, you will find differences.

____________

Blog: LucD notes

Twitter: lucd22

Tags: VMware

Similar Questions

  • Disassemble the help of data store, modify the existing function

    I would like to change the below function to pass a single host settings and a data store single instead of doing all THE hosts connected to the lun.  I would like to be able to specify the news.

    Function Remove Datastore { }

    [CmdletBinding()]

    Param

    (

    [Parameter (ValueFromPipeline

    = $true )]

    $Datastore

    )

    Process of

    {

    if (-not $Datastore) {

    Write-Host "No data not defined as an input store"

    Output

    }

    Foreach ($ds in $Datastore) {

    $hostviewDSDiskName = $ds. ExtensionData.Info.vmfs.extent [0]. DISKNAME

    if ($ds. ExtensionData.Host) { }

    $attachedHosts = $ds. ExtensionData.Host

    Foreach ()$VMHost en $attachedHosts) {

    $hostview = Get-View $VMHost. Key

    $StorageSys = Get-View $HostView. ConfigManager.StorageSystem

    Write-Host 'Disassembly $($DS.) VMFS Datastore. The name) host $($hostview ). «' «Name)...»»»

    $StorageSys . UnmountVmfsVolume ($DS.) ExtensionData.Info.vmfs.uuid);

    }

    }

    }

    }

    I tried to change it to this, but don't think I have it correct.  I would run like this preference:

    $esx = get-vmhost hostname

    Disassemble-Datastore - VMHost $esx - data datastorename store

    Function

    Remove Datastore {

    [CmdletBinding()]

    param (

    [

    [ VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl ]$VMHost

    [

    string ]$Datastore

    )

    Process of {

    if (-not $Datastore) {

    Write-Host "No data not defined as an input store"

    Output

    }

    $hostview = Get-View $VMHost. Key

    $StorageSys = Get-View $HostView. ConfigManager.StorageSystem

    #Write - Host "disassembly $($DS.) VMFS Datastore The name) host $($hostview.) (Name)... »

    $StorageSys . UnmountVmfsVolume ($Datastore.) ExtensionData.Info.vmfs.uuid);

    }

    }

    Try it like this

    function Unmount-Datastore {
      [CmdletBinding()]
      param(
        [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,    [string]$Datastore  )
    
      process {
        if (-not $Datastore) {
          Write-Host "No Datastore defined as input"      exit    }
    
        $ds = Get-Datastore -Name $Datastore    $hostview = Get-View $VMHost    $StorageSys = Get-View $HostView.ConfigManager.StorageSystem    #Write-Host "Unmounting VMFS Datastore $($DS.Name) from host $($hostview.Name)..."
        $StorageSys.UnmountVmfsVolume($ds.ExtensionData.Info.vmfs.uuid)  }
    }
    

    Given that the data store is defined as [string] I suppose you pass the datastorename.

    So you'll need to get the data in $ds store object

  • Script to make an inventory of data store

    Experts, I need help in creating a script for the data store information in my vcenter. Just the basic output, capacity, free space and space put into service.

    Thanks in advance.

    Redirect you the results to the Export-Csv cmdlet

    Get-Datastore | Select Name,
        @{N="CapacityGB";E={[Math]::Round($_.CapacityMB/1KB,2)}},
        @{N="FreeSpaceGB";E={[Math]::Round($_.FreeSpaceMB/1KB,2)}},
        @{N="ProvisionedGB";E={[Math]::Round(($_.CapacityMB - $_.FreeSpaceMB + $_.Extensiondata.Summary.Uncommitted/1MB)/1KB,2)}} |
    Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
    
  • How adding script. VMX to store data (NFS) to the virtual Center

    Hi guys,.

    I hope that someone much better understand the scripting languages I know a simple way to do this?

    Basically, here's the scenario...

    We use data warehouses NFS running on Netapp file servers to accommodate all our virtual machines. Normally this works very well, but we had a few incidents recently with the spin that force us to move to our synchronous backups. Basically how it works is that each data store, say for example Datastore1 works normally on Filer1. This data store is regularly "synchronous" on Filer2. This snapmirror is a copy of the data store readonly. If we have a problem with Filer1, it is possible to reconfigure Filer2 to make writable snapmirror backup, effectively allowing us to see the virtual machine.

    Currently, in this case, in order to get the back of the virtual machine, to remove all the vm to Virtual Center inventory, then add the synchronous data store to all guests in Virtual Center, then browse the data store and manually add back all the vm individually by the usual right-click right/add to the inventory on each virtual machine.

    As I say we have recently had a problem and had to call it for the storages of 10 data housing almost 250 vm was therefore, as you can imagine quite a slow intensive task and work!

    What I tried to work, that's how I could this power CLI script? I saw LuCD excellent research Add .vmx script and looks a lot like what I would like to but can't work out how to change it for my purposes.

    What id' ideally like to go would be a script that I can specify an instance of Virtual Center, store data and Cluster as variables (say vcname$ = "ukvir0001", cluster$ = $ 'Datacentre1' and dsname = "datastore1") and then the script read the data store and add no matter what .vmx, it finds in the specified Cluster.

    I read with interest the new feature in PowerCLI4 data store provider and which resembles something that can provide an easy way to do it I can't work out how!

    Any help would be much appreciated as I have the daunting task of having to move back almost 250 vm to Filer1 now that give it has been fixed and do not have the luxury of a maintenance window several hours to do it manually!

    Thank you very much

    Sean.

    After a few PMs between Sean and myself, we arrived with a working version.

    Note that this latest version of the script has been tested in a vSphere against an ESXi v4 server environment.

  • Data stores appearing not

    When I try and creae a new pool of desktops and move on the part of storage it only shows a data store on the 11 that I have.  And that shows he does not have enough space to this topic.  I reread the HBA, and yet they don't show up.  They show in vcenter but it does not the Manager.  Any help is appreciated.

    Perry

    Hi Perry

    1. try you to provision VMs SVI or full clones VMs?

    2. in the case of full clone VMs, you have a snapshot taken of the model, and if it, there see you the data store associated with this snapshot?

  • Rename a virtual computer in the data store

    We have an active VM (let's call it VMabcd) and it has exactly the same thing in the data store associated with her... labeled VMDK and VMX files also named VMabcd.   We would like to change the name.   I know that I can rename a virtual computer in vCenter directory but I can also do it in the data store?  What of its vmdk and vmx files?

    to do this if you have vCenter, the simplest method is to make a Storage vMotion of the guest in question.  This will rename all that behind that same as the vCenter object files.  Or you can turn off the virtual machine and making a cold migration to a new data store.

  • Help with script from beginner to the data store and hard drives

    Hello

    I am quite new to scripting, and recently, I created a monstrosity below.

    The problem is with the results. I have no idea why in the results I get the name of the virtual machine and its data store over and over again until all disks are highlighted.

    Any help will be very very much appreciated.

    The script:

    $raport = @)

    Foreach ($i in (Get-Content "Y:\vms.txt"))

    {

    $rekord = «»

    $vm = get-vm-name '$i '.

    $dysk1 = $vm | Get-hard drive | where {$_.} Name - eq 'Disk 1'}

    $datastore1 = $dysk1.filename.split("]") [0].split("[") [1]

    $disks = $vm | Get-hard drive

    {foreach ($disk in $disks)

    $hd = «»

    $disksize = ($disk. CapacityGB)

    $hd += $disksize

    {$rekord += $vm.name + ";" + $datastore1 + ";" + $hd}

    $raport += $rekord

    }

    $raport | out-file 'Y:\dyski.csv '.

    The results in csv:

    VM1Name; datastoreName; 60; VM1Name:datastoreName; 50; VM1Name; datastoreName; 40

    VM2Name; datastoreName; 45; VM2Name; datastoreName; 40

    The results of dream in csv:

    VM1Name; datastoreName; 60; 50 40

    VM2Name; datastoreName; 45 40

    etc.

    Try like this

    $raport = @)

    Foreach ($i in (Get-Content "Y:\vms.txt"))

    {

    $rekord = «»

    $vm = get-vm-name '$i '.

    $disk = $vm | Get-hard drive

    $datastore = ($disk | where {$_.}) Name - eq "disk 1" hard}).filename.split ("]") [0].split("[") [1].

    $rekord = "$($vm.)". The name); $($datastore); $(($disk | sélectionnez-ExpandProperty CapacityGB)-join ' |') »

    $raport += $rekord

    }

    $raport | out-file 'Y:\dyski.csv '.

  • Help with the data store migration script

    Hey everybody,

    I am currently responsible for the migration of disks in new storage. Each virtual computer might have multiple disks on different data warehouses. The script I'm working on that will check the VM for the disks located on a certain store of data, then if necessary to migrate the disk to the new data store. However, I'm lost. I hope I'm at least on the right track. Any help is very appreciated.

    Thank you!

    $csv = import-Csv-Path "e:\scripts\hunter\vantage_qadev.csv".

    foreach ($ent in ($csv))

    {

    $ds = get-Datastore ' QADEV_OS_HUS127 * ' | Sort descending FreeSpaceGB | Select-Object - 1 first

    $vmname = get-VM-name $ent.vmnames

    $vmds = $vmname | Get-hard drive | Select file name

    If ($vmds - eq "* QADEVOS_VSP986 *") {Get - VM $ent.vmnames |} Get-hard drive | Moving-hard drive - Datastore $ds}

    Else {Write-Host "Migration without any preconditions"}

    }

    That's what I did to make it work. The fact it's supposed to I would say...

    $csv = Import-Csv -way "e:\scripts\hunter\vantage_prod.csv".

    foreach ($ent in ($csv))

    {

    $ds = Get-Datastore ' DST_PROD_OS_HUS127 * ' | Sorting FreeSpaceGB -down | Select-Object -First 1

    Foreach ($HardDisk in (Get-VM -name $ent.vmnames |)) (( Get - hard drive))

    {

    $vm = Get-VM -name $ent.vmnames

    $hds = Get - hard drive -VM $vm

    $lunname = Get-VM -name $ent.vmnames | Get - hard drive | Select-Object -Property @{N = "Datastore"; E = {$harddisk. FileName.Split("]") [0]. TrimStart("[")}}

    If ($lunname - like "* DM_PROD_OS_VSP986 *") {Get-VM -name $ent.vmnames |} Get - hard drive | Moving - hard drive { -Data store $ds -confirm: $false}

    else {Write-Host $ent.vmnames ' not migrated '}

    }

    }

    What is a better way to do it?

  • Data store to the way policy script host

    Hi all

    I use this little script to identify the host LUN political path and status, but hoped someone could help to change the LUN list with a naa starting with naa.600500* and rather than list the lunName data store name?

    $lunpathinfo = @()
    foreach ($vmhost in get-cluster "CLUSTER" | get-vmhost) {
    $hostview
    = to seen $vmhost. id
    $hostview
    . config . StorageDevice . multipathinfo . lun | % { `
    $lunname = $_.id
    $lunpolicy = $_.policy.policy
    $_.path | % {
    $pathstate = $_.pathstate
    $lunpathinfo += ' | Select @{name = "Hostname"; expression = {$vmhost.name}}.
    @{name = "LunName"; expression = {$lunname}},
    @{name = "LunPolicy"; expression = {$lunpolicy}},
    @{name = "PathState"; expression = {$pathstate}}
    }
    }
    }
    $lunpathinfo | Export-csv c:host_luns.csv

    Hello, nicholas1982-

    You can also retrieve the CanonicalName of LUN, so as to then be able filter on them.  You can do filtering in the script foreach block, or, probably a more reusable way, return the CanonicalName as property and then filter on this subject.  So, as:

    &{foreach ($vmhost in Get-Cluster "CLUSTER" | Get-VMHost) {    ## the .Net View of this host, with just a select set of properties    $hostview = Get-View $vmhost.id -Property Config.Storagedevice.Multipathinfo.Lun,Config.Storagedevice.ScsiLun    $hostview.Config.Storagedevice.Multipathinfo.Lun | % {        ## the Lun "key" of this HostMultipathInfoLogicalUnit, to be used to get the corresponding ScsiLun of the same Key, so as to get the CanonicalName of the LUN        $strThisMPInfoLun_LunKey = $_.Lun        $lunname=$_.Id        $lunpolicy=$_.Policy.Policy        $_.path | % {            New-Object -Type PSObject -Property @{                Hostname = $vmhost.Name                LunName = $lunname                LunPolicy = $lunpolicy                PathState = $_.Pathstate                ## the CanonicalName of the ScsiLun that corresponds to this HostMultipathInfoLogicalUnit                LunCanonicalName = ($hostview.Config.Storagedevice.ScsiLun | ?{$_.Key -eq $strThisMPInfoLun_LunKey}).CanonicalName            } ## end new-object        }    }}} | ?{$_.LunCanonicalName -like "naa.600500*"} | Export-Csv -NoTypeInformation -UseCulture  c:\Temp\host_luns.csv
    

    As well as the new property, I changed things a bit:

    1. past the exit the ScriptBlock objects, for more later use/handling/filtering/etc.
    2. update the call to Get-View to retrieve only the specified properties (the properties actually used and necessary), for the love of speed

    How to make this good for you?

  • Need help pulling information data store multiple vCenters VM

    Thanks to the magic of Google and trial and error, I was able to develop the script below. The metric of the final I want to put the name of the data store the virtual computer is on and I can't for the life of understand me how do.

    $myCol = @)

    $TimeStamp = get-Date-format YYYY - MM - DD

    Game-PowerCLIConfiguration - multiple DefaultVIServerMode - confirm: $true | Out-Null

    $vcenters = @)

    "Server1."

    "Server2."

    "server3"

    "server4.

    );

    Connect-VIServer-Server $vcenters - username username-password password

    ForEach ($Cluster Get-cluster)

    {

    ForEach ($vmhost in ($cluster |)) Get - VM))

    {

    $VMView = $VMhost | Get-View

    $VMSummary = "" | Select Date VMName, NOMCLUSTER, MemorySizeGB, LogicalCPUs, ProvisionedSpaceGB, ResourcePool, DataStore

    $VMSummary.Date = $timestamp

    $VMSummary.VMName = $VMhost.Name

    $VMSummary.ClusterName = $Cluster.Name

    $VMSummary.MemorySizeGB = [math]: round ($VMHost.memorymb/1024, 2)

    $VMSummary.LogicalCPUs = $VMHost.numcpu

    $VMSummary.ResourcePool = $VMHost.ResourcePool

    $VMSummary.ProvisionedSpaceGB = [math]: round ($vmhost. ProvisionedSpaceGB, 2)

    $myCol += $VMSummary

    }

    }

    $myCol | Sort VMName | Export-CSV C:\VM-$timestamp.csv

    Any help would be appreciated.

    Thank you!

    Looks like you may be using an old PowerCLI version.

    Make one

    Get-PowerCliVersion

    What did he say?

    Upgrade to 5.5 R1

  • Script to list all the warehouses of data in a cluster, the virtual machines on the data store and the host of the virtual machine is on

    We have warehouses of data which are seen by more than one cluster. I need a script that will display:

    all stores of data seen by a cluster

    the virtual machines on the data store

    the host of the virtual machine is on

    the cluster the host is in

    The output I want is

    name of the store of data, the VM name, host name, the name of the Cluster

    any help would be appreciated

    Pretty sure this will do what you need:

    $report = @()
    Foreach($cluster in Get-Cluster){
        $datastores = $cluster | Get-VMHost | Get-Datastore
        foreach($datastore in $datastores){
        $vms = $datastore | Get-VM
        If ($vms.count -ge 1){
            foreach($VM in $vms){
            $object = New-Object -TypeName PSObject -Property @{
                  Datastore = $datastore.Name
                  VM = $VM.Name
                  HostName = $VM.VMhost
                  Cluster = $cluster
                }
            $report += $object
            }
    
        }
        }
    }
    $report | Export-Csv C:\Temp\VMs.csv -NoTypeInformation -UseCulture
    

    If you only want to shared data warehouses you can then use

    $datastores = $cluster | Get-VMHost | Get-Datastore | Where {$_.Extensiondata.Summary.MultipleHostAccess -eq $True}
    

    Hope this helps

  • script to create the folder on the data store please

    Hi all!

    I'm having difficulties writing a powercli script to create a file on a data store... we have a mixture of guests ESX 3.5, 4.0 and 4.1, using data storage mounted via NFS.

    For work I do, I need to:

    (1) create a folder on a data store (check that the folder does not already exist).

    2) navigate to the folder

    (3) and check if a particular file exists in this folder.

    The problem is that the context of my current position makes the code does not. the get-location method returns "C:\temp\ESX", being the record that I run the test script to. I try to use set-card with various settings to try to change the location of a particular data store, but it does not yet work-get-location returns "C:\temp\ESX".

    I think that the code for step 3) above is ok, but I can't get any code to work for) 1 and 2).

    can someone please?

    Thank you

    Ross.

    You must use the vimdatastore provider to access your data warehouses.

    It is documented in vSphere PowerCLI Administration Guide, which is installed on the PC where you installled PowerCLI.

    You can use the 'normal' cmdlets to navigate, test and create folders.

    For example:

    CD vmstore:

    LS # will show the upper level

    CD MyDatastore\MyFolder # navigate with the alias "cd".

    test-path MonAutreDossier # check the existence of a folder or file

    mkdir test # create a folder

    I hope this helps

  • PowerCli Help: VMs must rely on a data store

    Hi guys,.

    Need help to write a script where I need a number of virtual machines on a data store. This script has a few columns as well.

    I use below but I get no output if VM count is less than 2

    $TotalVM = (get - VM - DataStore $datastore) .count

    I know that there are a few scripts of a liner but I write this script for HTML output, and I also tried

    $TotalVM = get-Datastore Sharepoint_Datastore2 | Select Name, @{N = "TotalVM"; E={@($_ | Get - VM). County}} | Select totalvm

    This gives me the output voltage @{TotalVM = 1}

    I just need the County of VM on a particluar data store, any help would be appreciated.

    Thanks in advance

    You can get the number of virtual machines in a data store with:

    $TotalVM = (Get-Datastore $Datastore | Get-VM | Measure-Object).Count
    

    Best regards, Robert

  • Script to get the ID of the new LUN to create the data store

    Forgive the question from a novice - I'm not a developer at all.

    I am trying to write a script that will create a data store on a new iSCSI LUN - the problem is that I have already several other LUNS with the warehouses of data on it, so after I refresh the HBA, basically need to understand is that the new LUN and create a store of data on it.

    I made all sorts of googling and found scripts to create a new store database, but only in the first store of data on a new iSCSI target.

    Any help would be appreciated.

    Thank you!

    You can try the first script of Arnim in list HBA WWPN and LUN using Powershell

  • Cannot remove the data store in esxi 3.5 - can help you

    OK - I have a problem. I messed up and now I'm stuck.

    I was "apparently" in the middle of a converter working, and I deleted the comments of esxi3.5, while the converter still worked.

    I selected the rafale and made a removal of the disc that worked, however the structure of associated file (windows 2000 Server) for this guest has not been deleted, so I chose my data store (datastore1) and - do you have a store of navigation data and when I try to delete the folder associated with the guest, I deleted I get the following error :

    "General failure caused by file" (with a "/" on the next line) and all I can do at this point is to click OK box to acknowledge the error and I can't delete the folder. I can't rename it either.

    I don't know how to get rid of it. I tried to create a new client with the same name as the folder and delete the comments again, but that does not work either.

    I'm really stuck - I'm afraid to go into maintenance mode because I have no idea what I'm going to be confronted here since I never used the maintenance mode.

    any help or ideas would be really appreciated.

    You can try to do a refresh of the data store. The folder structure is not re read very often. In the data store Navigator, click the folder root on the left side and click on the Refresh button.

Maybe you are looking for