Powershell Get ChildItem行为不一致,在路径中使用变量时插入空格
我正试图从一个文件中读取一个计算机名列表,并在一组特定的目录中扫描每个文件的DLL文件。基本上,我希望它扫描电脑上每个用户的个人目录,并在..\AppData\Roaming\目录中查找任何DLL文件 这一点都不优雅,但我是PowerShell的新手:Powershell Get ChildItem行为不一致,在路径中使用变量时插入空格,powershell,get-childitem,Powershell,Get Childitem,我正试图从一个文件中读取一个计算机名列表,并在一组特定的目录中扫描每个文件的DLL文件。基本上,我希望它扫描电脑上每个用户的个人目录,并在..\AppData\Roaming\目录中查找任何DLL文件 这一点都不优雅,但我是PowerShell的新手: #runs DSQuery to get a list of machine names and export to a text file c:\temp\get_pc_list.bat $pcList = Get-Content c:\te
#runs DSQuery to get a list of machine names and export to a text file
c:\temp\get_pc_list.bat
$pcList = Get-Content c:\temp\pc_list.txt
#Removes a blank space at the end of each item in the text file
$pcList | foreach {$_.TrimEnd()} | Set-Content c:\temp\pc_list.txt
del C:\temp\file_check.txt
foreach ($computer in $pcList)
{
Get-ChildItem \\$computer\c$\users\*\AppData\Roaming\*.dll -recurse | Out-File -encoding unicode $("C:\temp\file_check.txt") -append -noclobber
}
当我运行它时,会收到一条消息“指定路径\PC101\c$\users处的对象不存在”。它似乎在“PC101”之后添加了一个空格,这是我列表中的第一项。我检查了文件,每行末尾没有多余的空格
我尝试过用双引号将路径括起来,但结果是一样的。我是否遗漏了某些内容,或者无法在路径中使用变量?为什么不使用AD GetComputer将所有PC名称拉入一个变量中 您还可以使用导出csv,因为这样对您更合适:
$Computers= Get-ADComputer -Filter * -SearchBase "ou=accounting,dc=domain,dc=com" -Properties * |
Select -ExpandProperty name
foreach ($computer in $Computers)
{
Get-ChildItem \\$computer\c$\users\*\AppData\Roaming\*.dll -recurse | Export-Csv
C:\temp\file_check -append -noclobber
}
要仅返回DLL的名称,可以将Get ChildItem行更改为:
Get-ChildItem \\$computer\c$\users\*\AppData\Roaming\*.dll -recurse | Select-Object Name | Export-Csv
C:\temp\file_check -append -noclobber
您能在
foreach
循环中发布[Regex]:Escape($computer)
的输出吗?尝试Get ChildItem\\$computer.trim()\c$\users\*\AppData\Roaming\*.dll
allready?Myguess是DSQ上ry返回的一些名称在行返回字符前带有空格(拆分PC名称的内容)。使用trim或ADComputer/QADComputer直接使用AD中的PowerShell对象将有助于…@briantist:PC101 PC102 PC103 PC104
@Paul我试过了。有个错误<代码>在“(”之后需要一个表达式。我想这是我最终要做的事情。我将进入一个没有安装ADWS的环境,所以我想我会努力做到这一点。谢谢你的建议。