首页 > 解决方案 > 测试路径的 Powershell 问题

问题描述

我正在尝试查看哪些计算机具有团队文件夹。我有一个名为 pc3.csv 的 .csv 文件,其中列出了所有计算机名称。当我的脚本运行时,输出只显示计算机名称,后跟 False(如果至少缺少一个路径,这是测试路径的设计。)我希望它显示计算机名称以及它是否显示路径。这是我的代码,任何帮助将不胜感激:

[CmdletBinding()] param()
Get-Content C:\script\pc3.csv
[string[]]$Computer = $env:computername

foreach ($Computer in $Computername) {
$a = Test-Path -path '\\$computer\c$\users\*\appdata\local\microsoft\teams'
 IF (-not $a) {Write-Host "$Computer this is not true"} ELSE {Write-Host "$Computer This is true"}
 }

标签: powershell

解决方案


我不确定$computername您要迭代的值应该是什么。您应该将文本文件中的所有内容加载到变量中。该变量将包含来自您的 Get-Content 命令的每一行的数组。

一旦您拥有所有计算机的列表,并且拥有查看 c$\users* 的“管理”权限,您就可以运行以下代码段。如果您无法访问所有系统,您将无法获得真实的详细信息。

$computerList =  Get-Content C:\script\pc3.csv
foreach($computer in $computerList) {
  if (Test-Path "\\$computer\c$\users\*\appdata\local\microsoft\teams") {
    Write-Host "$computer This is true"
  }
  else {
    Write-Host "$computer This is not true"
  }
}

推荐阅读