8 Replies Latest reply on Nov 13, 2018 10:31 AM by emcclure

    Get-Datacenter show results if more than 1 datacenter, else just move forward with script

    emcclure Enthusiast

      I'm trying to figure out if something is possible or not with Get-Datacenter.  I deal with a few different vCenters and some have more than one datacenter.  I'm trying to make my scripts generic enough so if someone is an admin and logs into a vCenter with multiple datacenters they can choose which one.  But if a user can only see a particular datacenter that they're assigned to then it only shows that one and moves forward automatically with the script.  So something like:

       

      Get-Datacenter | sort Name | Format-Table

       

      if result of get-datacenter is only 1 then proceed with script

       

      else read-host "choose a datacenter"

       

      Then from there the script moves forward with whatever, like getting a VM folder, host, etc. with the appropriate datacenter.

       

      Possible or just a mess?

        • 1. Re: Get-Datacenter show results if more than 1 datacenter, else just move forward with script
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          Something like this?

           

          $dc = Get-Datacenter

           

          if($dc.Count -gt 1){

            Write-Host "Choose a datacenter"

          }

          • 2. Re: Get-Datacenter show results if more than 1 datacenter, else just move forward with script
            emcclure Enthusiast

            Hmm doesn't seem to be working for me.  If I try this on a vCenter with one datacenter it still prompts me.  Even with a vCenter with multiple datacenters it doesn't even list them.

            • 3. Re: Get-Datacenter show results if more than 1 datacenter, else just move forward with script
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              You could use the logic from the other thread where you asked for a menu.

              And could you show what this says when you run it?

               

              $dc = Get-Datacenter

               


              if ($dc.Count -gt 1) {

                Write-Host "I found $($dc.Count) datacenters"

                Write-Host "Choose a datacenter"

                # Use the logic from the other thread to display the datacenters (they are in variable $dc)

              }

              • 4. Re: Get-Datacenter show results if more than 1 datacenter, else just move forward with script
                emcclure Enthusiast

                Hi LucD,

                 

                So I just briefly added the code into a script and when I ran it against a vCenter that has only 1 datacenter it told me that it found 5 datacenters.  It was a new PowerCLI window and no other windows were open and I was only connected to the 1 vCenter server.

                 

                I'm assuming you're talking about this thread? Is it possible to have a number selection instead of typing out a response when doing Get-<whatever>?

                 

                Here's what I get for the error if I put $dc = Get-Datacenter | sort Name | Format-Table

                 

                Cannot index into a null array.
                At C:\Users\emcclure\Desktop\GenScripts\GenSeriallyMigrateVMs.ps1:37 char:23
                +    Write-Host "$_ - $($entities[$_ - 1].Name)"
                +                       ~~~~~~~~~~~~~~~~~~~~~~
                    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
                    + FullyQualifiedErrorId : NullArray

                1 -
                Cannot index into a null array.
                At C:\Users\emcclure\Desktop\GenScripts\GenSeriallyMigrateVMs.ps1:37 char:23
                +    Write-Host "$_ - $($entities[$_ - 1].Name)"
                +                       ~~~~~~~~~~~~~~~~~~~~~~
                    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
                    + FullyQualifiedErrorId : NullArray

                0 -

                 

                If I don't put the sort Name | Format-Table I just get it where it skips to farther down in the script saying this:

                 

                Get-Datacenter : 11/13/2018 9:37:32 AM  Get-Datacenter          Datacenter with name '$dc' was not found using the specified
                filter(s).
                At C:\Users\emcclure\Desktop\GenScripts\GenSeriallyMigrateVMs.ps1:53 char:16
                + $mydatastore = Get-Datacenter '$dc' | Get-Datastore | sort Name | Sel ...
                +                ~~~~~~~~~~~~~~~~~~~~
                    + CategoryInfo          : ObjectNotFound: (:) [Get-Datacenter], VimException
                    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDat
                   acenter

                Get-Datastore : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that
                is not null or empty, and then try the command again.
                At C:\Users\emcclure\Desktop\GenScripts\GenSeriallyMigrateVMs.ps1:55 char:45
                + $destinationdatastore = Get-Datastore -Name $mydatastore #If you want ...
                +                                             ~~~~~~~~~~~~
                    + CategoryInfo          : InvalidData: (:) [Get-Datastore], ParameterBindingValidationException
                    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDatasto
                   re

                 

                 

                I'm guessing I probably copied the wrong part of the code into the script.  Here's what I have in there:

                 

                $dc = Get-Datacenter

                if ($dc.Count -gt 1) {

                  Write-Host "I found $($dc.Count) datacenters"

                  Write-Host "Choose a datacenter"

                  # Use the logic from the other thread to display the datacenters (they are in variable $dc)

                  1..($entities.Count) | %{

                   Write-Host "$_ - $($entities[$_ - 1].Name)"

                   }

                   $answer = 0

                   while (1..($entities.Count) -notcontains $answer){

                   $answer = Read-Host -Prompt "Select a folder (1-$($entities.Count))"

                   }

                }

                • 5. Re: Get-Datacenter show results if more than 1 datacenter, else just move forward with script
                  LucD Guru
                  vExpertCommunity WarriorsUser Moderators

                  The code is ok, but you should be using the $dc variable

                   

                  $dc = Get-Datacenter

                   

                  if ($dc.Count -gt 1) {

                    Write-Host "I found $($dc.Count) datacenters"

                    Write-Host "Choose a datacenter"

                    # Use the logic from the other thread to display the datacenters (they are in variable $dc)

                    1..($dc.Count) | %{

                      Write-Host "$_ - $($dc[$_ - 1].Name)"

                    }

                    $answer = 0

                    while (1..($dc.Count) -notcontains $answer){

                      $answer = Read-Host -Prompt "Select a folder (1-$($dc.Count))"

                    }

                  }

                   

                  • 6. Re: Get-Datacenter show results if more than 1 datacenter, else just move forward with script
                    emcclure Enthusiast

                    Hi LucD,

                     

                    Thanks for that.  Ok a major improvement, but still seeing some stuff.  If I connect to a vCenter with multiple datacenters I get prompted to select the datacenter which is great.  However in the script I'm testing it in it seems to give it an error.  Basically I get down to these lines in my script:

                     

                    mydatastore = Get-Datacenter -Name $dc.Name | Get-Datastore | sort Name | Select @{N="Cluster";E={$cluster.Name}},Name,CapacityGB,FreespaceGB,@{N='UsedSpace';E={$_.FreeSpaceGB/$_.CapacityGB*100}} | Out-Gridview -Title 'Select a destination datastore' -OutPutMode Single #Lists NFS datastores and shows size

                    $destinationdatastore = Get-Datastore -Name $mydatastore #If you want to specify a different datastore just change the name here, or you can modify the line to get the datastores in the datacenter
                    $selectedvms = Get-Datacenter -Name $dc.Name | Get-VM | sort Name | Out-GridView -Title 'Select the VMs you wish to migrate' -OutputMode Multiple #Opens a new window allowing you to select multiple VM's

                     

                    And it throws me this error:

                     

                    Get-Datastore : 11/13/2018 10:04:49 AM  Get-Datastore           Datastore with name '@{Cluster=; Name=perf; CapacityGB=4096;

                    FreeSpaceGB=232.845703125; UsedSpace=5.684709548950195312500}' was not found using the specified filter(s).

                    At C:\Users\emcclure\Desktop\GenScripts\GenSeriallyMigrateVMs.ps1:55 char:25

                    + $destinationdatastore = Get-Datastore -Name $mydatastore #If you want ...

                    +                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                        + CategoryInfo          : ObjectNotFound: (:) [Get-Datastore], VimException

                        + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDat

                       astore

                     

                    Not sure why as when I didn't have this datacenter change in the script it worked fine.

                     

                    Now if I exit the script and reconnect to a vCenter that has only 1 datacenter it lists the other datacenters from the other vCenter.  So instead of seeing just the one or moving on in the script I'm forced to choose between 20 datacenters, 19 of which belong to the other vCenter.  I realize I can add a disconnect-viserver at the end of the script, but if someone does a ctrl+c then they're still connected to that original vCenter it seems.

                     

                    It appears that even if I do a disconnect-viserver * from all vCenters and try to use the same PowerCLI session and try to connect to the vCenter with only 1 datacenter it still lists the ones from the other vCenter.

                     

                    When I do connect to just the vCenter with one the script does automatically move me forward, but I still get the same error that I got at the top in regards to the datastore.  Something else I need to add to the code here or remove something?

                    • 7. Re: Get-Datacenter show results if more than 1 datacenter, else just move forward with script
                      LucD Guru
                      User ModeratorsCommunity WarriorsvExpert

                      The cause of the error is that you are not passing a datastore object to the Get-Datastore cmdlet with the $mydatastore variable, but the result of a Select-Object.

                      You can fix that by just using the Name property.

                      Something like this

                       

                       

                      $mydatastore = Get-Datacenter -Name $dc.Name |

                          Get-Datastore | sort Name |

                          Select @{N="Cluster";E={$cluster.Name}},Name,CapacityGB,FreespaceGB,

                              @{N='UsedSpace';E={$_.FreeSpaceGB/$_.CapacityGB*100}} |

                              Out-Gridview -Title 'Select a destination datastore' -OutPutMode Single #Lists NFS datastores and shows size

                       

                      $destinationdatastore = Get-Datastore -Name $mydatastore.Name #If you want to specify a different datastore just change the name here, or you can modify the line to get the datastores in the datacenter

                       

                      The other problems seems to be due to variables that still contain content from a previous run.
                      To confirm that, you could try to stop/start your PowerShell/PowerCLI session in between runs.

                       

                      I would need to have a look at your complete script to see why that could/would happen.

                      • 8. Re: Get-Datacenter show results if more than 1 datacenter, else just move forward with script
                        emcclure Enthusiast

                        Hi LucD,

                         

                        Working like a charm now.  Thanks.