Function 具有多个Foreach对象的函数
我的脚本有一部分不起作用。目标是,从文件夹中获取文件,根据文件名的某个方面对其进行过滤和组织,然后将其移动到一个新文件夹中,该文件夹已为其创建了新的目录。i、 e根据文件名按月份和年份组织。例如,032批准保修-克罗地亚-案例-2019 08-1419032进入目录2019,然后是08。 下一步是创建一个“全选”函数,该函数在数字01-12之间循环。这很好。现在的问题是,我想在2017-2019年之间每年循环一次。这就是我被困的地方 这是我的试用代码,不起作用:Function 具有多个Foreach对象的函数,function,powershell,foreach,Function,Powershell,Foreach,我的脚本有一部分不起作用。目标是,从文件夹中获取文件,根据文件名的某个方面对其进行过滤和组织,然后将其移动到一个新文件夹中,该文件夹已为其创建了新的目录。i、 e根据文件名按月份和年份组织。例如,032批准保修-克罗地亚-案例-2019 08-1419032进入目录2019,然后是08。 下一步是创建一个“全选”函数,该函数在数字01-12之间循环。这很好。现在的问题是,我想在2017-2019年之间每年循环一次。这就是我被困的地方 这是我的试用代码,不起作用: function DoWork
function DoWork {
Param([int]$Month) ([int]$Year)
$StrMonth = $Month.ToString("00")
echo $StrMonth.ToString("00")
$StrYear = $Year.ToString
echo $StrYearToString
$files = Get-ChildItem $destinationpath -Filter "*$StrYear $StrMonth*" -Recurse
foreach ($file in $files) {
$year = $Year.ToString()
$month = $Month.ToString()
$file.Name
$StrYear
$StrMonth
# Set Directory Path
$Directory = $targetPath + "\" + $StrYear + "\" + $StrMonth
if (!(Test-Path $Directory)) {
New-Item $directory -Type Directory
}
$file | Copy-Item -Destination $Directory -Force
}
}
if ($group1 -eq 'Select All') {
2017..2019 | ForEach-Object {
$year = $_
1..12 | ForEach-Object {DoWork($_, $year)}
}
} elseif ($group -eq 'Select All') {
1..12 | ForEach-Object {DoWork($_, $group1)}
} else {
DoWork($group, $group1)
}
我想让它循环一年,然后是所有的月份,然后是下一年等等,然后在另一边为他们制作文件夹。我不太明白。错误消息是:
DoWork : Cannot process argument transformation on parameter 'Month'. Cannot convert the "System.Object[]"
value of type "System.Object[]" to type "System.Int32".
At line:46 char:39
+ 1..12 | ForEach-Object {DoWork($_, $year)}
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [DoWork], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,DoWork
DoWork:无法对参数“Month”进行参数转换。无法转换“系统对象[]”
将“System.Object[]”类型的值转换为“System.Int32”类型。
第46行字符:39
+1..12 |每个对象{DoWork($年)}
+ ~~~~~~~~~~~
+CategoryInfo:InvalidData:(:)[DoWork],参数BindingArgumentTransformationException
+FullyQualifiedErrorId:参数ArgumentTransformationError,DoWork
函数定义中有错误 线路
function DoWork { param ([int]$Month) ([int]$Year)
应该是
function DoWork {
param ( [int] $Month, [int] $Year)
# your function code...
}
+他需要将
DoWork($\u,$year)
更改为DoWork$\u$year
Your's right;-)现在弹出此错误:找不到“ToString”的重载,参数计数为:“1”。第13行字符:1+Echo$StrMonth.ToString(“00”)+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MethodCountCouldNotFindBest@WilliamBrooker$StrMonth
已经是一个字符串,不确定您在这里想要实现什么