我试图运行一个循环来检查桌面上特定访问数据库的计算机列表。我在检查诸如Users \ Default之类的路径时已经让它工作了,但实际上我需要它来检查当前登录用户的桌面。这是可能的还是我需要一个特定用户名列表来配合计算机名称的.txt?
$computers=Get-Content C:\Users\zrasner\Documents\project_files\powershell_computernames\computernames.txt
foreach ($computer in $computers) {
$path = Test-Path "\\$computer\c$\Users\Default\VAST.accdb\"
If ($path -eq $true) { Write-Host "($computer) VAST is present in Default Users Folder"}
If ($path -eq $False) { Write-Host "($computer)VAST File Not Found"}
}
同样,我想将检查目的地更改为相关远程计算机的桌面。
答案 0 :(得分:0)
下面代码中的第一个$user
行获取用户名。 $user = ($user).Split('\')[-1]
行按反斜杠拆分并返回拆分的最后一部分。这意味着它将从域\用户名或用户名中捕获用户名。
$computers=Get-Content C:\Users\zrasner\Documents\project_files\powershell_computernames\computernames.txt
foreach ($computer in $computers) {
$path = $false
$user = Get-WmiObject –ComputerName $computer –Class Win32_ComputerSystem | Select-Object -ExpandProperty UserName
$user = ($user).Split('\')[-1]
$path = Test-Path "\\$computer\c$\Users\Default\VAST.accdb"
If ($path) { Write-Host "($computer) VAST is present in Default Users Folder"}
$path = Test-Path "\\$computer\c$\Users\$user\VAST.accdb"
If ($path) { Write-Host "($computer) VAST is present in currently logged on user ($user)'s Folder"}
If ($path -eq $False) { Write-Host "($computer)VAST File Not Found"}
}