如何为get-children命令使用变量路径

问题描述 投票:1回答:1

我对这些行有一些问题:

Invoke-Command -ComputerName $computerObject.Name -ScriptBlock {'{0:0.00}' -f ((Get-ChildItem $path  -Recurse  | Measure-Object -Property Length -sum).sum)}

我一无所获。但如果我使用而不是$pathc:\users\pa27dd7n\documents的输出是21830841828,00

$path.gettype()的回归是:

name:string      basetype: system.object

当前代码:

$computers = Get-ADComputer -SearchBase  "OU=......"  -filter "*" -Properties name,description,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion  | sort-object description 

$ADUsers   = Get-ADUser -Filter * -SearchBase "OU=........" | Where-Object {$PSItem.enabled -eq "True"} |  Sort-Object name 

我将属性添加到计算机以保持计算机的正确用户的登录:

$computers | foreach {Add-Member -InputObject $PSItem -NotePropertyName MainUserSamAccountName -NotePropertyValue ""  -Force}

来自$ADUsers我正在登录我需要的用户。

当我得到一切:

$computerObject = $computers.Get($a) - 在循环中

所以,当我登录时:

$path = "c:\users\$computerObject.MainUserSamAccountName\documents"

这样我就得到了目录用户文档的路径。

powershell get-childitem
1个回答
0
投票

如果未定义path,PowerShell将在当前目录上运行Get-ChildItem,因此如果在远程系统上运行它,它将在默认目录中执行(管理用户的C:\ windows \ system32或用户的配置文件)用户执行)。

但是我注意到你正在使用-f操作符,它往往会烧人。

我不喜欢使用字符串-format运算符,因为语法对我来说太不透明了,人们很难支持。如果您真正喜欢的是报告目录名称和所有项目的大小,我宁愿建议发送一个对象,如本示例所示。

Invoke-Command -ComputerName $computerObject.Name -ScriptBlock {
[pscustomobject]@{DirectoryName=(Get-location).Path;
Size=((Get-ChildItem  $path -Recurse | Measure-Object -Property Length -sum).sum)/1MB -as [int]}

结果

DirectoryName     Size
-------------     ----
C:\Users\Stephen 16756

我继续前进并除以1MB以减少返回的数字长度,然后再作为整数进行整理。希望这能带你走向正确的方向!

© www.soinside.com 2019 - 2024. All rights reserved.