Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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 CSV未导出缺少的Active Directory对象_Powershell_Csv_Active Directory - Fatal编程技术网

PowerShell CSV未导出缺少的Active Directory对象

PowerShell CSV未导出缺少的Active Directory对象,powershell,csv,active-directory,Powershell,Csv,Active Directory,之前,我尝试制作一个powershell脚本,将文本文件中的资产标记与AD中的计算机名称进行比较,结果成功。然而,当一项资产在广告中找不到时,它只是跳过这一行,进入下一行。找不到的资产不会导出到CSV文件中 有没有办法让它在CSV文件中打印丢失的资产,以及资产标签旁边的“未找到资产”之类的消息?我尝试使用这一行,但所做的只是在行之间添加空格 if ($_.samaccountname -notlike "*$line*") {"Asset does not exist in AD"} 代码:

之前,我尝试制作一个powershell脚本,将文本文件中的资产标记与AD中的计算机名称进行比较,结果成功。然而,当一项资产在广告中找不到时,它只是跳过这一行,进入下一行。找不到的资产不会导出到CSV文件中

有没有办法让它在CSV文件中打印丢失的资产,以及资产标签旁边的“未找到资产”之类的消息?我尝试使用这一行,但所做的只是在行之间添加空格

if ($_.samaccountname -notlike "*$line*") {"Asset does not exist in AD"}
代码:

Import-Module ActiveDirectory

   [Array]$Collection = foreach($line in Get-Content H:\AssetNames.txt)  {
        Get-ADComputer -Filter * -Property Description | Where {$_.samaccountname -Like "*$line*"} | Select Name, Description
        #if ($_.samaccountname -notlike "*$line*") {"Asset does not exist in AD"}
    }

$Collection | Export-Csv C:\test.csv -NoTypeInformation

您的问题是,您正在排除
Get ADComputer
过滤器中不匹配的资产,因此您无法知道它们何时不匹配。如果对变量执行此操作,则可以检查:

Import-Module ActiveDirectory

   [Array]$Collection = foreach($line in Get-Content H:\AssetNames.txt)  {
        $Match = Get-ADComputer -Filter * -Property Description | Where {$_.samaccountname -Like "*$line*"} | Select Name, Description
        if (!$Match) {
            "Asset $($_.samaccountname) does not exist in AD" | Out-File -FilePath "ErrorLog.txt" -Append
        } else {
            Write-Output $Match
        }
    }

$Collection | Export-Csv C:\test.csv -NoTypeInformation

查询广告和导出到CSV总是出现。这两者的结合足以让你做所有你想做的事情。。。嘿,马克,很抱歉反应太晚了。刚刚尝试了此操作,但写入输出没有将丢失的资产写入CSV文件,在AD中找不到的任何资产都会被CSV文件中的空行替换。请尝试删除[array]部分。尝试了此操作后,结果仍然相同。如果我让它写入一个单独的文本文件,那么丢失的资源确实会显示在该文本文件中。