0

我对 powershell 非常陌生,并且编写了一个脚本,该脚本将转到 Windows 域中的每台计算机并获取用户配置文件的大小。我在下面的powershell脚本上尝试过

$profileDir = "\\$computerName\c$\users\userProfile"
$fullProfile = (Get-ChildItem $profileDir -recurse -force | Measure-Object -property length -sum)

但在某些计算机上,它给出了以下错误。问题似乎是配置文件上的某些目录路径很长并且get-ChildItem失败

Get-ChildItem : The specified path, file name, or both are too long. The fully
qualified file name must be less than 260 characters, and the directory name mu
st be less than 248 characters.
At C:\scripts\powershell\profiles.ps1:56 char:30
+ $fullProfile = (Get-ChildItem <<<<  $profileDir -recurse -force | Measure-Obj
ect -property length -sum)
    + CategoryInfo          : ReadError: (\\computerName...nt.IE5\RQT4K4JU:St
   ring) [Get-ChildItem], PathTooLongException
    + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChil
   dItemCommand
##########################################################################################################################

此时我尝试使用du.exe来自 SysInternals 的 (diskusage) 工作正常,但我不知道如何将输出du转换为变量。我的脚本上有以下内容

$dirSize = .\du.exe -c c:\temp |convertFrom-csv | select-object DirectorySize
write-host $dirSize

输出是

PS C:\scripts\powershell> .\profiles.ps1

@{DirectorySize=661531}

我希望输出是什么样的

PS C:\scripts\powershell> .\profiles.ps1

661531
4

1 回答 1

1

你所拥有的是一个哈希表。您需要引用该哈希表中的项目来获取它的值。代替:

Write-Host $dirSize

尝试:

$dirSize['DirectorySize']
于 2014-11-06T03:10:07.347 回答