无法将值“ Int32”转换为类型“ System.Int32”

时间:2019-10-11 09:27:08

标签: powershell type-conversion int

我从某些文件夹中得到了一些文件大小,该值以字节为单位。我想通过除以1024将它们转换为mB,但即使它们都是int32,也无法正常工作

我尝试了很多铸造,但没有任何粘物

遍历文件夹返回一个整数

$subsize = (traverseFolder $folderName)
$i = 1024
$bytesToMb = [int]$subsizeType / $i
$fileContent = "$folderName : " + "$bytesToMb 
Add-Content "C:\Users\SolutionTeam\Documents\DBIGroupFolderSizes.txt" $fileContent

我得到的错误:

Cannot convert value "Int32" to type "System.Int32". Error: "Input string was not in a correct format."
At C:\Users\SolutionTeam\Documents\coding\powershell\getfoldersize\GetSize.ps1:103 char:9
+         $bytesToMb = [int]$subsizeType / [int]$i
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

我还在$ i和$ subsize上都使用了.GetType():

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试一下:

$folderName = "c:\Temp"
$fileContent = "$foldername : {0} MB" -f ((Get-ChildItem $folderName -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB)
Add-Content "C:\Users\SolutionTeam\Documents\DBIGroupFolderSizes.txt" $fileContent