Search code examples
powershellexport-to-excel

export-excel failing, curly brackets are wrong?


I'm very new to PowerShell scripting. I've tried to use some Google-Fu and figure out the answer to this problem myself, but it's very hard to find the answer to this specific problem. I'm hoping someone here can help, and thanks in advance!

I'm using a script by Luc Dekens that I got from his blog (http://www.lucd.info/2016/09/13/orphaned-files-revisited/). At the end of his blog, he gives a few more lines of code (using dfinke's "export-excel") that will generate an Excel spreadsheet. It is these last 6 lines of code that doesn't work for me.

I've pasted those last 6 lines of code here. Then I'll paste the error I get from ISE when I run it.

6 lines of code:

$reportName = 'C:\users\jharriso-a\documents\ps\orphan-report.xlsx'

foreach($ds in (Get-Cluster -Name MyCluster | Get-Datastore | Get-VmwOrphan | 
                Group-Object -Property {$_.Folder.Split(']')[0].TrimStart('['))){
$ds.Group | Export-Excel -Path $reportName -WorkSheetname $ds.Name -AutoSize -AutoFilter -FreezeTopRow
}

This is the error I get from ISE:

C:\users\jharriso-a\documents\ps> .\get-vmworphan.ps1 -datastore 

vmware_templates_nfs_gu1c_gaantapsvm1
Group-Object : A positional parameter cannot be found that accepts argument '['.
At C:\users\jharriso-a\documents\ps\get-vmworphan.ps1:142 char:17
+ ...             Group-Object -Property {$_.Folder.Split(']')[0].TrimStart ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Group-Object], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GroupObjectCommand

Solution

  • There is a missing '}' there. you can see a red underlined '{' after Property if you paste that line into ISE.

    I am guessing the closing bracket should be inserted just after TrimStart('['). Try that see if it fixes the error.

    foreach($ds in (Get-Cluster -Name MyCluster | Get-Datastore | Get-VmwOrphan |
                    Group-Object -Property {$_.Folder.Split(']')[0].TrimStart('[')} )){
        $ds.Group | Export-Excel -Path $reportName -WorkSheetname $ds.Name -AutoSize -AutoFilter -FreezeTopRow
    

    }