powershell - 测试路径的 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"}
}
解决方案
我不确定$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"
}
}
推荐阅读
- git - 如何在 Visual Studio Code 甚至命令行中更改 git 用户?(对 user1 的权限被拒绝,请求的 URL 返回错误:403)
- r - 为大列表的每个元素自动在大列表中创建向量的功能
- node.js - 错误阻止捆绑:使用 ecmascript 处理文件时(针对目标 web.browser):
- c# - 插入包含字符串的变量作为新列表c#的参数
- angular - 如何从角度的承诺中获取网址?
- rust - 对超出函数的局部 Arc 的引用
- android - 取消暂停通知
- python-3.x - conda 创建环境中的 conda list vs pip list 差异
- c# - 在后台工作人员中使用 SendKeys.SendWait 时,为什么我能听到 3 秒的哔声?
- java - 使用 Jackson 和 Jersey 反序列化 java.ws.rs 实体会引发异常