Al_
Enthusiast
Enthusiast

Simple Variables in Powercli

Jump to solution

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

0 Kudos
1 Solution

Accepted Solutions
Al_
Enthusiast
Enthusiast

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!

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Al_
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Al_
Enthusiast
Enthusiast

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!

View solution in original post

0 Kudos
LucD
Leadership
Leadership

I copied the -Append switch from your original code.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos