VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Error getting multiple folder details using variable

Hi,

I am able to get the folder details, if I pass the folder details as below.

Get-Folder = "Dept-01", "Dept-02", "Dept-03"

But If I assign the same to variable, it is not working and getting the below error. please help!!

$str = '"Dept-01", "Dept-02", "Dept-03"'

$modifiedString = $str -replace '"', ''

get-folder $modifiedString

 

Error
get-folder : 8/11/2023 5:11:41 AM Get-Folder Folder with name 'Dept-01, Dept-02, Dept-03' was not found using the specified filter(s).
At line:1 char:1
+ get-folder $modifiedString
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-Folder], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetFolder

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The get rid of te double quotes.

$modifiedString = ($str.Replace(' ','').Replace('"','')) -split ','


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try with

$modifiedString = ($str.Replace(' ','')) -split ','


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

Split is working as expected.

 

PS D:\myreports> $str = '"Dept-01","Dept-02","Dept-03"'
PS D:\myreports> $modifiedString = ($str.Replace(' ','')) -split ','
PS D:\myreports> $modifiedString
"Dept-01"
"Dept-02"
"Dept-03"

 

but when I use the variable for get-folder, i am getting error.


PS D:\myreports>
PS D:\myreports> get-folder $modifiedString

get-folder : 8/11/2023 6:55:06 AM Get-Folder Folder with name '"Dept-01"' was not found using the specified filter(s).
At line:1 char:1
+ get-folder $modifiedString
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-Folder], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetFolder

get-folder : 8/11/2023 6:55:06 AM Get-Folder Folder with name '"Dept-02"' was not found using the specified filter(s).
At line:1 char:1
+ get-folder $modifiedString
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-Folder], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetFolder

get-folder : 8/11/2023 6:55:06 AM Get-Folder Folder with name '"Dept-03"' was not found using the specified filter(s).
At line:1 char:1
+ get-folder $modifiedString
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-Folder], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetFolder

 

Here I am trying to get total CPU count from all three department folders.

(Get-Folder $modifiedString | Get-VM | Measure-Object -Property numcpu -Sum).Sum

Tags (1)
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The get rid of te double quotes.

$modifiedString = ($str.Replace(' ','').Replace('"','')) -split ','


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Perfect. Thank you very much LucD, it worked now.

0 Kudos