Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell Active Directory与文本文件的比较_Powershell - Fatal编程技术网

PowerShell Active Directory与文本文件的比较

PowerShell Active Directory与文本文件的比较,powershell,Powershell,我试图制作一个脚本,将文本文件中的资产标签列表与AD中的计算机名称进行比较,并生成描述。稍后会将其导出到CSV。但到目前为止,虽然代码确实有效,但它给出了以下错误消息。在广告中,我们的电脑以L或D开头,表示它是笔记本电脑还是台式机,但我们收到的列表中没有L或D,这就是为什么你看到我把“L”+“D”放在前面。有更好的方法吗 代码: 错误: 这可能是一种更有效的方法,但以下方法有效: Import-Module ActiveDirectory foreach ($line in Ge

我试图制作一个脚本,将文本文件中的资产标签列表与AD中的计算机名称进行比较,并生成描述。稍后会将其导出到CSV。但到目前为止,虽然代码确实有效,但它给出了以下错误消息。在广告中,我们的电脑以L或D开头,表示它是笔记本电脑还是台式机,但我们收到的列表中没有L或D,这就是为什么你看到我把“L”+“D”放在前面。有更好的方法吗

代码:

错误:


这可能是一种更有效的方法,但以下方法有效:

   Import-Module ActiveDirectory

    foreach ($line in Get-Content ComputerNames.txt)  {
        Get-ADComputer -Filter * -Property Description | Where {$_.samaccountname -Like "*$line"} | select Description
    }

对于
computernames.txt
对象中的每一行,它都会找到类似于
$line
变量的AD对象,然后选择该对象的描述慢速位将是到AD的网络链接,如果可能,您真的只想做一次。除非你在广告中有大量的计算机,否则最好把所有的计算机都拉下来,然后在本地与文本文件进行比较

另外,如果您从广告中获取信息,不要带来任何超出您需要的信息,网络流量和内存开销都会被浪费,因此只需添加描述,而不是属性*

Import-Module ActiveDirectory

# AD query which will get all computers with names starting D or L
$ADFilter = "Name -like 'D*' -or Name -like 'L*'"
$ADComputers = Get-ADComputer -filter $ADFilter -Properties Description | Select Name, Description

$NamesFromFile = Get-Content ComputerNames.Txt

# Filter the AD Computers where the name without the first character is
# mentioned in the file
$ADComputers | Where-Object { $_.Name.SubString(1) -in $NamesFromFile } | Export-Csv -NoTypeInformation out.csv

如果手动检查对象是否存在,则根本找不到对象“LD7MWQ12”?不,它不存在,因为资产D7MWQ12实际上以D开头(全名DD7MWQ12)。有没有一种方法可以忽略错误消息,或者干脆绕过它,搜索以字母D或L开头的资产?要绕过错误,您可以使用
   Import-Module ActiveDirectory

    foreach ($line in Get-Content ComputerNames.txt)  {
        Get-ADComputer -Filter * -Property Description | Where {$_.samaccountname -Like "*$line"} | select Description
    }
Import-Module ActiveDirectory

# AD query which will get all computers with names starting D or L
$ADFilter = "Name -like 'D*' -or Name -like 'L*'"
$ADComputers = Get-ADComputer -filter $ADFilter -Properties Description | Select Name, Description

$NamesFromFile = Get-Content ComputerNames.Txt

# Filter the AD Computers where the name without the first character is
# mentioned in the file
$ADComputers | Where-Object { $_.Name.SubString(1) -in $NamesFromFile } | Export-Csv -NoTypeInformation out.csv