export-excel 失败,大括号错误?
export-excel failing, curly brackets are wrong?
我对 PowerShell 脚本编写还很陌生。我尝试使用一些 Google-Fu 并自己找出这个问题的答案,但是很难找到这个特定问题的答案。我希望这里有人可以提供帮助,在此先感谢!
我正在使用从 Luc Dekens 的博客 (http://www.lucd.info/2016/09/13/orphaned-files-revisited/) 获得的脚本。在他的博客末尾,他给出了更多代码行(使用 dfinke 的 "export-excel"),这些代码将生成一个 Excel 电子表格。这最后 6 行代码对我不起作用。
我已将最后 6 行代码粘贴到此处。然后我会粘贴我在 运行 时从 ISE 得到的错误。
6 行代码:
$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
}
这是我从 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
此处缺少一个“}”。如果将该行粘贴到 ISE 中,您可以在 属性 后看到带红色下划线的“{”。
我猜应该在 TrimStart('[') 之后插入右括号。试试看它是否修复了错误。
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
}
我对 PowerShell 脚本编写还很陌生。我尝试使用一些 Google-Fu 并自己找出这个问题的答案,但是很难找到这个特定问题的答案。我希望这里有人可以提供帮助,在此先感谢!
我正在使用从 Luc Dekens 的博客 (http://www.lucd.info/2016/09/13/orphaned-files-revisited/) 获得的脚本。在他的博客末尾,他给出了更多代码行(使用 dfinke 的 "export-excel"),这些代码将生成一个 Excel 电子表格。这最后 6 行代码对我不起作用。
我已将最后 6 行代码粘贴到此处。然后我会粘贴我在 运行 时从 ISE 得到的错误。
6 行代码:
$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
}
这是我从 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
此处缺少一个“}”。如果将该行粘贴到 ISE 中,您可以在 属性 后看到带红色下划线的“{”。
我猜应该在 TrimStart('[') 之后插入右括号。试试看它是否修复了错误。
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
}