我刚刚开始学习一些 PowerShell,并迈出了第一步。我正在接受创建目录的培训。
我写了下面的代码,它可以运行,但我想问一下如何进一步简化它,同时帮助我继续学习和扩展我的 PowerShell 技能?
$branch1 = (@('Fox','Wolf','Cat','Cow','Snake','Elephant') | ForEach-Object { New-Item (Join-Path 'C:\tree\Animals\' $_) -ItemType Directory -force })
$branch2 = (@('Chair','Cupboard','Sofa','Table','Bed','Dresser') | ForEach-Object { New-Item (Join-Path 'C:\tree\Furniture\' $_) -ItemType Directory -force })
$branch3 = (@('Mercury','Venus','Earth','Mars','Jupiter','Saturn') | ForEach-Object { New-Item (Join-Path 'C:\tree\Planets\' $_) -ItemType Directory -force })
$FolderTree = $branch1 + $branch2 + $branch3
if ($FolderTree) { Write-Host "All sorted out."}
答案1
潜在的简化调整
将数组数据类型明确设置为顶部的变量。
创建一个功能并在其中构建
New-Item
命令,以接受执行时传递给函数调用的每个值的两个参数,以动态构建其要创建的路径。最后,
ForEach-Object
循环遍历每个数组,在函数调用中明确定义第一个参数,并让第二个参数成为每个$_
占位符的数组的迭代值(例如_Main "animals" $_;
)。
笔记:有些人可能也会考虑使用%
别名ForEach-Object
简化,因此我在下面的示例 PowerShell 中使用了它,因为您特别询问了简化。
电源外壳
$animals = 'Fox','Wolf','Cat','Cow','Snake','Elephant';
$furniture = 'Chair','Cupboard','Sofa','Table','Bed','Dresser';
$planets = 'Mercury','Venus','Earth','Mars','Jupiter','Saturn';
Function _Main (){
New-Item "C:\tree\$($args[0])\$($args[1])" -ItemType Directory -Force;
};
$animals | % { _Main "animals" $_; };
$furniture | % { _Main "furniture" $_; };
$planets | % { _Main "planets" $_; };
PowerShell(不同变体)
$animals = 'Fox','Wolf','Cat','Cow','Snake','Elephant'; $furniture = 'Chair','Cupboard','Sofa','Table','Bed','Dresser'; $planets = 'Mercury','Venus','Earth','Mars','Jupiter','Saturn'; Function _Main (){ New-Item "$($args[0])" -ItemType Directory -Force; }; $animals | % { _Main "C:\tree\animals\$_"; }; $furniture | % { _Main "C:\tree\furniture\$_"; }; $planets | % { _Main "C:\tree\planets\$_"; };
支持资源
-
从最基本的角度来说,函数只是 function 关键字,后跟函数名称,然后是一对花括号内的某些代码。
function Add-Numbers { $args[0] + $args[1] } PS C:\> Add-Numbers 5 10 15
-
标准别名对于 Foreach 对象:'
%
' 符号,ForEach
答案2
这是@Vomit IT - Chunky Mess Style 的另一种有用方法。
此方法使用哈希表并验证错误New-Item
。它在结构、动态和可读性方面得到了简化。而不是在一行/更少的代码方面。
$basePath = 'C:\tree\'
# a hashtable with a key and value pair
# the key or value could also be a path, see example 'Food\Vegetables' below
$directoryTree = @{
# Key = Value(s)
Animals = 'Fox', 'Wolf', 'Cat', 'Cow', 'Snake', 'Elephant'
Furniture = 'Chair', 'Cupboard', 'Sofa', 'Table', 'Bed', 'Dresser'
Planets = 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn'
'Food\Fruits' = 'Berries\Banana', 'Berries\Watermelon', 'Apple', 'Pineapple'
}
# reset error indicator of New-Item
$newItemCommandError = $null
# loop through the keys. Always use ".psbase." to avoid the hashtable property "Keys" gets overriden by a key named "Keys"
foreach ($directory in $directoryTree.psbase.Keys) {
# loop through the values of current key
foreach ($subDirectory in $directoryTree[$directory]) {
# build full path by concatenating Join-Path via Pipe(s).
# can be simplified as of PS7+: "Join-Path $basePath $directory $subDirectory"
$fullPath = Join-Path $basePath $directory | Join-Path -ChildPath $subDirectory
# ErrorVariable +'newItemCommandError' acts as an indicator for errors
$null = New-Item $fullPath -ItemType Directory -Force -ErrorVariable +'newItemCommandError'
}
}
if (-not $newItemCommandError) {
Write-Host 'All sorted out.'
}
相同的代码,没有注释和结果:
$basePath = 'C:\tree\'
$directoryTree = @{
Animals = 'Fox', 'Wolf', 'Cat', 'Cow', 'Snake', 'Elephant'
Furniture = 'Chair', 'Cupboard', 'Sofa', 'Table', 'Bed', 'Dresser'
Planets = 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn'
}
$newItemCommandError = $null
foreach ($directory in $directoryTree.psbase.Keys) {
foreach ($subDirectory in $directoryTree[$directory]) {
$fullPath = Join-Path $basePath $directory | Join-Path -ChildPath $subDirectory
$null = New-Item $fullPath -ItemType Directory -Force -ErrorVariable +'newItemCommandError'
}
}
if (-not $newItemCommandError) {
Write-Host 'All sorted out.'
}