2014-10-29 88 views
0
$getusers = Get-ChildItem \\pc-name\c$\users\ | Select-Object Fullname 

我正在运行该行以获取所有登录到电脑的用户。获取路径,然后将其转换为字符串?

然后我检查每个文档文件夹的文件。我认为它会这样简单:

foreach ($user in $getusers) { 
Get-ChildItem "$user\documents" 
} 

但似乎我必须将$ getusers转换为字符串?有人可以帮助并解释需要做什么吗?我认为它简单,我只是没有得到。

+1

发布您在尝试此操作时得到的输出... – GodEater 2014-10-29 13:54:50

回答

1
$dirs = Get-ChildItem \\pc-name\c$\users\ | Select-Object FullName | Where-Object {!($_.psiscontainer)} | foreach {$_.FullName} 

这结束了工作。我能弄明白。

+2

Putting | Where-Object {!($ _。psiscontainer)} |在'Select-Object'之后的foreach {$ _ FullName}'是误导和冗余的。 '$ _。psiscontainer'将为空,因为您除了'FullName'之外移除了前面的'select'的所有属性。另外,由于您已经选择了'FullName',因此您不需要使用'ForEach'再次输出。 – Matt 2014-10-29 14:33:59

1

如果有人在那里找到这个搜索帮助,我想添加我认为的实际问题。考虑以下行:

$getusers = Get-ChildItem \\pc-name\c$\users\ | Select-Object Fullname 

这将返回fullname s的对象。

FullName                  
--------                             
\\localhost\c$\users\jpilot              
\\localhost\c$\users\matt            
\\localhost\c$\users\misapps             
\\localhost\c$\users\mm 

问题是$getusersSystem.Object[]拥有全名NoteProperty而不是System.String[]作为循环将被期待。我应该在以下

$getusers = Get-ChildItem \\pc-name\c$\users\ | Select-Object -ExpandProperty Fullname 

做现在$getusers将包含字符串

\\localhost\c$\users\jpilot              
\\localhost\c$\users\matt            
\\localhost\c$\users\misapps             
\\localhost\c$\users\mm 

这将使脚本函数的其余部分如预期的数组。

相关问题