简体   繁体   English

dsquery中的Powershell Strip计算机名称

[英]Powershell strip computer name from dsquery

I'm trying to grab a list of all the computer names on my network and see if a directory exists on that PC in powershell, but I am having problems. 我试图获取网络上所有计算机名称的列表,并查看Powershell中该PC上是否存在目录,但是出现问题。 Can someone look this over and tell me what I'm doing wrong? 有人可以看看这个,告诉我我在做什么错吗?

$computers = dsquery computer "ou=TESTOU, dc=example, dc=com"
foreach($computer in $computers) {
    If(!(Test-Path -path "\\$computer.\C$\Program Files (x86)\Bit9\Parity Agent")) {
        Write-Host $computer
    }
    Out-File -FilePath .\result.txt
}

By default dsquery computer only returns the DistinguishedName for each computer, like so: 缺省情况下, dsquery computer仅返回每台计算机的DistinguishedName ,如下所示:

C:\>dsquery computer "ou=TESTOU,dc=example,dc=com"
"CN=Computer01,ou=TESTOU,dc=example,dc=com"
"CN=Computer02,ou=TESTOU,dc=example,dc=com"
"CN=Computer03,ou=TESTOU,dc=example,dc=com"
"CN=Computer04,ou=TESTOU,dc=example,dc=com"

So, your -path argument becomes: 因此,您的-path参数变为:

"\\CN=Computer01,ou=TESTOU,dc=example,dc=com.\C$\Program Files (x86)\Bit9\Parity Agent"

Which is no good. 不好


Use dsquery * "ou=TESTOU,dc=example,dc=com" -attr Name to get the computers name instead, and parse it using ConvertFrom-Csv : 使用dsquery * "ou=TESTOU,dc=example,dc=com" -attr Name来获取计算机名称,然后使用ConvertFrom-Csv对其进行解析:

# Retrieve computer names
$Computers = dsquery * "ou=TESTOU,dc=example,dc=com" -filter "(objectClass=computer)" -attr Name -limit 0 | ConvertFrom-Csv -Delimiter " "

# Select only the name from the output
$Computers = $Computers | Select-Object -ExpandProperty Name

# Assign any output from the foreach loop to $Results
$Results = foreach($Computer in $computers){
    $Path = '\\{0}\C$\Program Files (x86)\Bit9\Parity Agent' -f $Computer
    if(!(Test-Path -Path $Path)){
        # Don't use Write-Host, it only write text to the console
        $Computer
    }
}
# Write the computer names that failed to .\result.txt
$Results | Out-File -FilePath .\results.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM