我有一个PowerShell脚本,可以抓取远程计算机上当前登录的用户并获取其本地配置文件C:\ users \ USERNAME \ documents,pictures,music等的总大小。我已确认远程计算机在线并且用户已登录。当脚本正在运行时,从它获取文件夹大小的意义上来说,它会返回以下消息:
Get-WmiObject : Invalid parameter At C:\Users\administrator\Desktop\get_folder_size.ps1:4 char:9 + $user = Get-WmiObject Win32_ComputerSystem -ComputerName $system | Se ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand You cannot call a method on a null-valued expression. At C:\Users\administrator\Desktop\get_folder_size.ps1:5 char:1 + $pos = $user.IndexOf("\") + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At C:\Users\administrator\Desktop\get_folder_size.ps1:6 char:1 + $user = $user.Substring($pos+1) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
我很困惑,因为如果我正确阅读邮件,它应该无法获取用户名,因此无法获得正确的文件夹和大小。
以下是我正在运行的代码
foreach ($system in Get-Content "C:\net_view.txt")
{
$colItems = 0
$user = Get-WmiObject Win32_ComputerSystem -ComputerName $system |
Select-Object -ExpandProperty UserName
$pos = $user.IndexOf("\")
$user = $user.Substring($pos+1)
$path = $system +"\c$\users\" + $user
$colItems = (Get-ChildItem $path -Recurse -ErrorAction SilentlyContinue |
Measure-Object -property length -sum)
"{0:N2}" -f ($colItems.sum / 1MB) + " MB"
Add-Content C:\users\administrator\desktop\profile_size.txt ($colItems.sum / 1MB)
Add-Content C:\users\administrator\desktop\profile_size.txt $system
}