5 Replies Latest reply on Feb 13, 2020 9:21 AM by LucD

    Simple Variables in Powercli

    Al_ Novice

      I'm trying a very simple process to write a .CSV filename using a variable:

       

      $folder = Read-Host "Please enter the folder name"

       

      Get-Folder '$folder' | Get-VM | Get-View -Property @("Name") | Select -Property Name | Export-CSV {'$folder'}.csv -NoTypeInformation -UseCulture -append

       

      I get this error:

       

      PowerCLI D:\Powershell\vCenter> .\get_vms_by_folder_export_csv.ps1

      Please enter the folder name: test

      Export-Csv : Cannot validate argument on parameter 'Path'. The argument is

      null or empty. Provide an argument that is not null or empty, and then try the

      command again.

      At D:\Powershell\vCenter\get_vms_by_folder_export_csv.ps1:3 char:99

      + ... ("Name") | Select -Property Name | Export-CSV ('$folder').csv -NoType ...

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

          + CategoryInfo          : InvalidData: (:) [Export-Csv], ParameterBindingV

         alidationException

          + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power

         Shell.Commands.ExportCsvCommand

       

      I realize this is a very easy operation, I'm just not finding examples...

      Thanks

        • 1. Re: Simple Variables in Powercli
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          Try like this

           

          $folder = Read-Host "Please enter the folder name"

          Get-Folder '$folder' | Get-VM | Get-View -Property @("Name") |

          Select -Property Name |

          Export-CSV "$folder.csv" -NoTypeInformation -UseCulture -append

           

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Simple Variables in Powercli
            Al_ Novice

            Thanks LucD.

             

            I get this error:

            PowerCLI D:\Powershell\vCenter> .\lucd2.ps1

            Please enter the folder name: test

            Get-Folder : 2/13/2020 9:44:28 AM       Get-Folder              Folder with name

            '$folder' was

            not found using the specified filter(s).

            At D:\Powershell\vCenter\lucd2.ps1:2 char:1

            + Get-Folder '$folder' | Get-VM | Get-View -Property @("Name") |

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

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

                + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimA

               utomation.ViCore.Cmdlets.Commands.GetFolder

             

             

            $folder = Read-Host "Please enter the folder name"

             

             

            Get-Folder '$folder' | Get-VM | Get-View -Property @("Name") |

             

             

            Select -Property Name |

             

             

            Export-CSV "$folder.csv" -NoTypeInformation -UseCulture -append

            • 3. Re: Simple Variables in Powercli
              LucD Guru
              vExpertUser ModeratorsCommunity Warriors

              I didn't notice you had single quotes around $folder.

               

              $folder = Read-Host "Please enter the folder name"

              Get-Folder -Name $folder | Get-VM | Get-View -Property @("Name") |

              Select -Property Name |

              Export-CSV "$folder\report.csv" -NoTypeInformation -UseCulture -append

               

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: Simple Variables in Powercli
                Al_ Novice

                Perfect, all I had to do was modify the Export-CSV "$folder.csv" to remove the \report as it was trying to create/write to non existent dir. This works.

                 

                     $folder = Read-Host "Please enter the folder name"

                 

                     Get-Folder -Name $folder | Get-VM | Get-View -Property @("Name") |

                 

                     Select -Property Name |

                 

                     Export-CSV "$folder.csv" -NoTypeInformation -UseCulture -append

                 

                Thanks!

                • 5. Re: Simple Variables in Powercli
                  LucD Guru
                  Community WarriorsvExpertUser Moderators

                  I copied the -Append switch from your original code.

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz