我有一个PowerShell函数,可以将文件和文件夹从一个目录移动到另一个目录。我希望能够确保$fromFolder
或$toFolder
的值都不为空或为空。不确定如何使用两个参数。
function Move-Folders {
gci $fromFolder -Recurse | ForEach-Object { Move-Item $_.FullName $toFolder }
ii $toFolder
}
答案 0 :(得分:2)
使用验证属性声明参数:about_Functions_Advanced_Parameters
function Move-Folders {
Param
(
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]
$fromFolder,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]
$toFolder
)
gci $fromFolder -Recurse | ForEach-Object { Move-Item $_.FullName $toFolder }
ii $toFolder
}
答案 1 :(得分:0)
这应该使用强制参数:
function Move-Folders {
Param(
[Parameter(Mandatory=$true)]
$fromFolder,
[Parameter(Mandatory=$true)]
$toFolder
) #end param
gci $fromFolder -Recurse | ForEach-Object { Move-Item $_.FullName $toFolder }
ii $toFolder
}