如何编写PowerShell函数来获取目录?
使用PowerShell我可以使用以下命令获取目录:
Get-ChildItem -Path $path -Include "obj" -Recurse | `
Where-Object { $_.PSIsContainer }
我更喜欢编写一个函数,因此命令更具可读性。例如:
Get-Directories -Path "Projects" -Include "obj" -Recurse
除了优雅地处理-Recurse
之外,以下功能正是如此:
Function Get-Directories([string] $path, [string] $include, [boolean] $recurse)
{
if ($recurse)
{
Get-ChildItem -Path $path -Include $include -Recurse | `
Where-Object { $_.PSIsContainer }
}
else
{
Get-ChildItem -Path $path -Include $include | `
Where-Object { $_.PSIsContainer }
}
}
如何从我的Get-Directories功能中删除if
语句,或者这是更好的方法吗?
没有找到相关结果
已邀请:
3 个回复
徐百晴墓斜
这是有效的,因为-Recurse:$ false同样没有-Recurse。
绊伦欧
开关进行烘烤:
壬驴拿歪竞
请注意有关动态参数的限制。现在,当您导入PSCX时,请执行以下操作:
现在你可以这样做: