1 2 Previous Next 25 Replies Latest reply on Nov 9, 2018 4:23 PM by alleninbroomfield Go to original post
      • 15. Re: long datastore names truncated after Get-Datastore
        LucD Guru
        vExpertCommunity WarriorsUser Moderators

        Ok, so you are still using a version with PSSnapin.
        Up until 6.5 R1 when you installed PowerCLI it also installed a shortcut on your desktop.

        That shortcut pointed to an init script that did load the PSSnapin and modules, and configured a PowerShell prompt, where it for example changed the Window title.

        Post PowerCLI 6.5 R1, there is no more MSI file, all modules (no more PSSnapin) are installed from the PSGallery.

        See Welcome PowerCLI to the PowerShell Gallery – Install Process Updates

         

        Since PowerShell v4, you also don't have to explicitly import the PowerCLI modules anymore.

        That is done through the PowerShell module autoload feature.

        The 1st time you use a cmdlet from a module, PS will autoload that module for you.

         

        In your .ps1 script I see some pre-amble, including manipulation of the console window.

        What else is done in those preceding lines?

        From where did you get those lines?

        Are those a copy of the old init script I mentioned?

        • 16. Re: long datastore names truncated after Get-Datastore
          COS Master

          "What else is done in those preceding lines?"

           

          Here's the entire contents of my script....

           

          param([bool]$promptForCEIP = $false)

          # List of modules to be loaded
          $moduleList = @(
              "VMware.VimAutomation.Core",
              "VMware.VimAutomation.Vds",
              "VMware.VimAutomation.Cloud",
              "VMware.VimAutomation.PCloud",
              "VMware.VimAutomation.Cis.Core",
              "VMware.VimAutomation.Storage",
              "VMware.VimAutomation.HorizonView",
              "VMware.VimAutomation.HA",
              "VMware.VimAutomation.vROps",
              "VMware.VumAutomation",
              "VMware.DeployAutomation",
              "VMware.ImageBuilder",
              "VMware.VimAutomation.License"
              )

          $productName = "PowerCLI"
          $productShortName = "PowerCLI"

          $loadingActivity = "Loading $productName"
          $script:completedActivities = 0
          $script:percentComplete = 0
          $script:currentActivity = ""
          $script:totalActivities = `
             $moduleList.Count + 1

          function ReportStartOfActivity($activity) {
             $script:currentActivity = $activity
             Write-Progress -Activity $loadingActivity -CurrentOperation $script:currentActivity -PercentComplete $script:percentComplete
          }
          function ReportFinishedActivity() {
             $script:completedActivities++
             $script:percentComplete = (100.0 / $totalActivities) * $script:completedActivities
             $script:percentComplete = [Math]::Min(99, $percentComplete)
            
             Write-Progress -Activity $loadingActivity -CurrentOperation $script:currentActivity -PercentComplete $script:percentComplete
          }

          # Load modules
          function LoadModules(){
             ReportStartOfActivity "Searching for $productShortName module components..."
            
             $loaded = Get-Module -Name $moduleList -ErrorAction Ignore | % {$_.Name}
             $registered = Get-Module -Name $moduleList -ListAvailable -ErrorAction Ignore | % {$_.Name}
             $notLoaded = $registered | ? {$loaded -notcontains $_}
            
             ReportFinishedActivity
            
             foreach ($module in $registered) {
                if ($loaded -notcontains $module) {
             ReportStartOfActivity "Loading module $module"
                  
             Import-Module $module
            
             ReportFinishedActivity
                }
             }
          }

          LoadModules

          # Update PowerCLI version after snap-in load
          $powerCliFriendlyVersion = [VMware.VimAutomation.Sdk.Util10.ProductInfo]::PowerCLIFriendlyVersion
          $host.ui.RawUI.WindowTitle = $powerCliFriendlyVersion

          #write-host ""
          Connect-VIServer -Server "MYSERVERNAME"

          Get-Cluster JOMAMA | Get-Datastore | Sort-Object -Property FreeSpaceGB -Descending | Select @{N='Name';E={"{0}" -f $_.Name}},FreeSpaceGB,CapacityGB

          Disconnect-VIServer -Server "MYSERVERNAME" -Confir:$false

           

           

           

           

           

          "From where did you get those lines?"

          From the shortcut pointer on my desktop that called the file "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1". I grabbed the beginning portion that loaded the modules and it allowed me to run all the PowerCLI commands.

           

           

           

          "Are those a copy of the old init script I mentioned?"

          Maybe? (◔/‿\◔)

           

           

          Thanks

          • 17. Re: long datastore names truncated after Get-Datastore
            COS Master

            "What does $host.UI.RawUI.BufferSize show?"

             

             

            It returns this...

             

            PowerCLI C:\Temp>  $host.UI.RawUI.BufferSize

            Width Height
            ----- ------
              170      0

            • 18. Re: long datastore names truncated after Get-Datastore
              alleninbroomfield Novice

              Just open the regular PowerShell ISE, try this:

               

              Import-Module -Name VMware.PowerCLI

              Connect-VIServer -Server "MYSERVERNAME"

              Get-Cluster JOMAMA | Get-Datastore | Sort-Object -Property FreeSpaceGB -Descending | Select @{N='Name';E={"{0}" -f $_.Name}},FreeSpaceGB,CapacityGB

               

              At least it will rule out a) whether it's any of the VMware stuff overlayed onto ISE, and b) anything in your code.  It formats correctly in my PS ISE, but I'm not running any of the VMware stuff overlayed.

               

               

              • 19. Re: long datastore names truncated after Get-Datastore
                LucD Guru
                vExpertUser ModeratorsCommunity Warriors

                Yes, they are (a copy of the init script).

                 

                But I think I might have an answer to what you are seeing.

                In a script, when the first output to the console ends up as a list vs a table (the 5 or more properties rule).

                All subsequent output in that script will also be presented in the list format.


                For example when I add a line before the line with the 3 properties

                 

                Get-Datastore -Name esx* | Select Name, FreeSpaceMB, FreespaceGB, CapacityMB, CapacityGB

                 

                Get-Datastore -Name esx* | Sort-Object -Property FreeSpaceGB -Descending | Select @{N = 'Name'; E = {"{0}" -f $_.Name}}, FreeSpaceGB, CapacityGB

                 

                And now execute this as a .ps1 file, I get

                dslist2.jpg

                The 1st line is presented as a list due to the 5 properties.

                And from there on, every output is presented as a list (even when less than 5 properties).

                • 20. Re: long datastore names truncated after Get-Datastore
                  COS Master

                  I had high hopes for this one but I got this....

                  • 21. Re: long datastore names truncated after Get-Datastore
                    LucD Guru
                    vExpertUser ModeratorsCommunity Warriors

                    Btw, you can reset that default behaviour by using the Out-Default cmdlet.

                    For example

                     

                    Get-Datastore -Name esx* | Select Name, FreeSpaceMB, FreespaceGB, CapacityMB, CapacityGB | Out-Default

                    Get-Datastore -Name esx* | Sort-Object -Property FreeSpaceGB -Descending | Select @{N = 'Name'; E = {"{0}" -f $_.Name}}, FreeSpaceGB, CapacityGB

                     

                    will produce

                    dslist3.jpg

                    • 22. Re: long datastore names truncated after Get-Datastore
                      LucD Guru
                      vExpertCommunity WarriorsUser Moderators

                      There is no VMware.PowerCLI module in 6.5 R1, that only appeared in 6.5.1 (which is not the same as 6.5 R1)

                      • 23. Re: long datastore names truncated after Get-Datastore
                        alleninbroomfield Novice

                        Yep, sorry mate-- I guess we're running different versions of vSphere and the PowerCLI.

                         

                        This is craziness, LucD… good find.

                         

                        He can fix it by adding a simple pipe at the end:

                         

                        | Format-Table

                         

                        And it will make it a table again.

                         

                        Get-Datastore -Name esx* | Select Name, FreeSpaceMB, FreespaceGB, CapacityMB, CapacityGB

                        Get-Datastore -Name esx* | Sort-Object -Property FreeSpaceGB -Descending | Select @{N = 'Name'; E = {"{0}" -f $_.Name}}, FreeSpaceGB, CapacityGB | Format-Table

                        • 24. Re: long datastore names truncated after Get-Datastore
                          COS Master

                          Adding " | Out-Default" to the end fixed it across all execution methods including from within my VB .Net App.

                           

                          Thanks again!!!

                          • 25. Re: long datastore names truncated after Get-Datastore
                            alleninbroomfield Novice

                            Yes, it's like writing HTML for different browsers... you think it's perfect and somebody opens up your webpage in Opera or old IE 7 and suddenly it looks like trash.  Dealing with different execution methods can be quite a headache... yes, that was a strange issue and a good find by LucD that with 5 properties it would change how it would format subsequent output... strange.  Glad it's fixed!

                            1 2 Previous Next