我有一个自动化Windows USMT备份的脚本,但我遇到了Powershell 2.0的问题。基本上,我有一个脚本参数,它需要一个正整数,并且验证在Powershell 3.0+中有效,但在Windows7中附带的2.0中不起作用。
参数编码:
[CmdletBinding()]
Param (
[ValidateScript({
if( -Not ($_ | Test-Path) ){
throw "File or folder does not exist"
}
if($_ | Test-Path -PathType Leaf){
throw "The Path argument must be a folder. file paths are not allowed."
}
if( -not (($_ | Get-ChildItem | Measure-Object).Count -eq 0) ) {
throw "The Folder '$_' Has Content/Files! USMT will not run against a non-empty backup folder!!"
}
return $true
})]
[System.IO.FileInfo]$BackupPath,
[switch]$OfflineUSBDock,
[ValidateRange(1, [int]::MaxValue)][int]$UEL
)错误:

所以我的问题是,我如何解决这个问题,以便在Powershell 2.0中正常工作?目标是让$UEL参数仅接受正整数。
发布于 2018-09-28 00:47:34
下面是评论中的两个变通方法(感谢@TheIncorrigible的建议):
[ValidateScript({
if ($_ -eq 0) {
throw "UEL requires a positive integer greater then 0!"
}
return $true
})]
[uint32]$UEL或
[ValidateRange(1, 2147483647)][int]$UEL虽然不像使用[int]::MaxValue那样干净,但它可以完成工作。
https://stackoverflow.com/questions/52541080
复制相似问题