Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 脚本在PS3中工作,但不在PS2中工作,我可以';“找不到确切的错误”;运算符'的参数错误-gt';_Powershell_Powershell 2.0_Powershell 3.0 - Fatal编程技术网

Powershell 脚本在PS3中工作,但不在PS2中工作,我可以';“找不到确切的错误”;运算符'的参数错误-gt';

Powershell 脚本在PS3中工作,但不在PS2中工作,我可以';“找不到确切的错误”;运算符'的参数错误-gt';,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,我查找大于x的文件的脚本对我和我的powershell 3.0都很好。但在我们的服务器上,我们只有powershell 2.0,我无法使它正常工作 错误如下: Bad Argument to Operator '-gt' Could not compare "3841" to "10MB". Error cannot convert "10MB" to type "System.Int64" Error: "Input string was not in a correct format."".

我查找大于x的文件的脚本对我和我的powershell 3.0都很好。但在我们的服务器上,我们只有powershell 2.0,我无法使它正常工作

错误如下:

Bad Argument to Operator '-gt' Could not compare "3841" to "10MB". Error cannot convert "10MB" to type "System.Int64" Error: "Input string was not in a correct format."".
我的脚本在这里,我试图将10MB的值改为“10”,但我想它比kb要大。那不是我想要的!有什么想法吗

Param(
  [ValidateScript({test-path -path $_ -pathtype container})]
  [string]$targetfolder,
  [string]$size
)

function getfilesbigger 
{
$colItems =  (get-childitem  "$targetfolder" -recurse | where {$_.length -gt "$size" } | tee-object -variable allfiles | measure-object -property length -sum)
$allfiles | foreach-object {write-host $_.FullName ("{0:N2}" -f ($_.Length / 1MB)) "MB" -ForegroundColor "green" }
write-host "Collected all files bigger than $size from folder $targetfolder " -foregroundcolor "darkgreen"
"Number of files: "+ $colItems.count + [Environment]::NewLine + "Size of all files "+"{0:N2}" -f ($colItems.sum / 1MB) + " MB" 
}

getfilesbigger

原因是
size
已明确定义为字符串:

Param(  ...   
[string]$size)
因此,Powershell不会像往常一样自动将10mb转换为整数10485760,而是将其用作字符串。对于工作版本,只需像这样使用int

Param(
  [ValidateScript({test-path -path $_ -pathtype container})]
  [string]$targetfolder,
  [int]$size
)
编辑:

它在我的2.0环境中运行良好。尝试添加一个
.GetType()
调用以查看发生了什么。这样,

function getfilesbigger 
{
$size.gettype()
$colItems = ...
gettype调用的结果应该是

IsPublic IsSerial Name                BaseType
-------- -------- ----                --------
True     True     Int32               System.ValueType

您得到了什么?

您是否加入了调试器?$size中有什么?啊,对不起,在size中输入“10mb”或“10GB”无法处理参数“size”上的arguemtn转换。无法将值“10MB”转换为类型“System.Int32”。错误:“输入字符串的格式不正确。”--这是我在更改为[int]后得到的结果。请删除脚本中
$size
周围的引号。引号将其转换为字符串。当我为$size输入“10”时,会得到相同的结果,但“10MB”不起作用。然后他再次收集所有文件,而不是大于的文件:/ok,我现在又回到了那个问题上:当我运行一个简单的命令,比如:get childitem directory | where{$\u.length-gt 10mb},它正在工作……但不知怎的,在函数中它不工作。当我硬编码参数时,我会处理这个问题,它是有效的。不知何故,他无法从我的批处理文件中正确加载参数。