【问题标题】:Powershell issue with Test-path测试路径的 Powershell 问题
【发布时间】:2020-01-25 03:18:21
【问题描述】:

我正在尝试查看哪些计算机具有团队文件夹。我有一个名为 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"}
 }

【问题讨论】:

  • 1) 您的 Get-Content 没有存储在变量中,因此 PowerShell 只是输出它。 2) 如果文件只包含文本行并且不是逗号分隔值文件(毕竟这就是 CSV 的含义),那么您应该将其命名为 .txt 以避免混淆。 3) 你的foreach 循环只查看当前计算机。

标签: powershell


【解决方案1】:

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

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

$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"
  }
}

【讨论】:

  • 感谢 Jawad 和@Bill_Stewart。 pc3.csv 文件包含计算机名称,我已经拥有管理员访问权限。这确实帮助我取得了必要的进展,以收集我需要禁用 Microsoft Teams 的计算机,而无需手动检查每台计算机。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
  • 2019-07-09
  • 1970-01-01
相关资源
最近更新 更多