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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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脚本的方法_Powershell - Fatal编程技术网

正在尝试找到加速此powershell脚本的方法

正在尝试找到加速此powershell脚本的方法,powershell,Powershell,我有一个很好用的脚本(输出看起来不错),但现在需要12个多小时。csv中有34220条记录,现在只有2110条记录。也许我需要先加载所有用户数据,然后与csv文件进行比较?谢谢你的帮助 import-module ActiveDirectory $CCure = Import-csv C:\Scripts\CCure\CCure-Personnel-enabled.csv ForEach ($Row in $CCure) { [string]$ID = $Row.ObjectID

我有一个很好用的脚本(输出看起来不错),但现在需要12个多小时。csv中有34220条记录,现在只有2110条记录。也许我需要先加载所有用户数据,然后与csv文件进行比较?谢谢你的帮助

import-module ActiveDirectory

$CCure = Import-csv C:\Scripts\CCure\CCure-Personnel-enabled.csv 

ForEach ($Row in $CCure) {
    [string]$ID = $Row.ObjectID
    [string]$Name = $Row.Name
    [string]$EmpID = $Row.Int5

    If ($EmpID.Trim() -ne "0") {
    $User = Get-ADUser -LDAPFilter "(&(&(&(objectclass=user)(objectcategory=person)(!userAccountControl:1.2.840.113556.1.4.803:=2))))((employeeId=*$EmpID))" -SearchBase 'DC=Enterprise,DC=mycompany,DC=org' -Properties SamAccountName,DisplayName,EmployeeId,enabled  | 
    Select @{Name="CCure ObjectID";Expression={$ID}},SamAccountName,DisplayName,@{Name="CCure Name";Expression={$Name}},EmployeeId,@{Name="CCure Int5 Row";Expression={$EmpID}},enabled | Export-csv c:\scripts\ccure\EmployeeIds4-10-2016.csv -NoTypeInformation -append

    }
    }
也许我需要先加载所有用户数据,然后与csv文件进行比较

这正是你需要做的

由于您希望通过
EmployeeId
属性将CSV中的用户关联起来,因此我建议拉出填充了
EmployeeId
的所有(已启用)用户,然后将其存储在哈希表中,其中
EmployeeId
用作键:

$ADUserTable = @{}
Get-ADUser -LDAPFilter "(&(!userAccountControl:1.2.840.113556.1.4.803:=2)(employeeId=*))' -SearchBase 'DC=Enterprise,DC=mycompany,DC=org' -Properties SamAccountName,DisplayName,EmployeeId |ForEach-Object {
    $ADUserTable[$_.EmployeeId] = $_
}
然后,当您迭代CSV中的行时,在哈希表中查找用户,而不是再次搜索AD:

$ExistingUsers = ForEach ($Row in $CCure) {
    # Import-Csv always creates string properties anyways
    $ID = $Row.ObjectID
    $Name = $Row.Name
    $EmpID = $Row.Int5.Trim()

    if ($EmpID -ne "0" -and $ADUserTable.ContainsKeys($EmpID)) 
    {
        $ADUserTable[$EmpID] |Select @{Name="CCure ObjectID";Expression={$ID}},SamAccountName,DisplayName,@{Name="CCure Name";Expression={$Name}},EmployeeId,@{Name="CCure Int5 Row";Expression={$EmpID}}
    }
}
在收集所有信息之前,不要将其导出到Csv,否则将打开、写入和关闭同一文件35000次

因此,在最后:

$ExistingUsers |Export-csv c:\scripts\ccure\EmployeeIds4-10-2016.csv -NoTypeInformation
这无疑会加快脚本的执行速度



注意:我已从
Get ADUser
Select Object
中删除了
Enabled
属性。您的LDAP筛选器已经保证只返回已启用的用户,因此我看不出将其添加到CSV中有任何价值。域中有多少帐户?AD中有超过21k个活动用户,但CCure中可能有许多不在AD中。为什么在LDAPFilter中用
*
预先添加
$EmpID
EmployeeId
是否包含的不仅仅是ID?这是个好问题。我可能是从另一个脚本中做的,只是碰巧保留了它。我可能可以删除它,但不确定它是否会加快速度?满足
(&(attribute=*value))
的搜索比满足
(&(attribute=value))
的搜索要快得多(而且在DC上的资源密集度要低得多)嗨,Mathias,这看起来很棒!但我得到一个错误方法调用失败,因为[System.Collections.Hashtable]不包含名为“ContainsKeys”的方法。第18行字符:9+if($EmpID-ne“0”-和$ADUserTable.ContainsKeys($EmpID))+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~类别信息:无效操作:(:::::]、!好的,但是在所有这些之后,输出只包含5266个匹配项?呃,这是那个系统中非常脏的数据。现在我必须找出另一种方法来验证这两个系统中谁存在,因为这种方法似乎缺乏。下一步,我将尝试使用DisplayName。再次感谢!如果(@($ADUserTable.Keys-如“*$Email”).Count-gt 0)而不是ContainsKey,则会产生部分匹配,如果这是问题所在,则不会从Active Directory获取CSV样本和一些相应的用户,我很难说DisplayName/Firstname/Lastname/EmployeeId是否是数据之间相关性的最佳键值。它基本上归结为:“我们是否有两个不同的值可以在两个数据源之间轻松地进行比较?”,然后应用我向您展示的算法:将这两个值读入内存,按键排序/组织一个值,然后遍历另一个值。有道理吗?