在执行请求时限制Active Directory连接的数量

时间:2015-12-03 13:19:40

标签: powershell active-directory

我正在尝试使用脚本检查备份文件夹,以查找链接到不在Active Directory中的用户或计算机的用户。

# Gets the list of folders on the backup Server
$FolderList = Get-ChildItem $Server | select name

foreach ($Folder in $FolderList){

    # On each folder, gets the computer name and the user shortname (the backup folder are built using %COMPUTERNAME%-%USERNAME%)
    $Splited = $Folder.Name.Split("-")
    $ComputerName = $Splited[0]
    $ShortName = $Splited[1]
    $LastWrittenDate = Get-Item "$($Server)\$($Folder.Name)" | Select-Object LastWriteTime

    # Checks if the username exists in the Active Directory
    try{
        Get-ADUser $ShortName | Out-Null
    } catch {
        # If it doesn't exists, write in the user output file
        Add-Content "$($outputFile)_user.txt" "$($ShortName); $($LastWrittenDate.LastWriteTime)"
    }


    # Checks if the computer name exists in the Active Directory and the computer is not in the Active Directory
    try {
        Get-ADComputer $ComputerName | Out-Null
    } catch {
        # If it doesn't exists, write in the computer output file
        Add-Content "$($outputFile)_computers.txt" "$($ComputerName); $($LastWrittenDate.LastWriteTime)"
    }
}

我的问题是,当我提出检查用户/计算机是否在AD中的请求时,它会打开与AD的新连接而不关闭它(当日志文件爆炸时检测到问题)。

有没有办法只打开一个AD会话并在该会话期间调用我的所有请求?

1 个答案:

答案 0 :(得分:0)

我对我的脚本做了一些修改,监控日志没有显示批量登录请求。

$FolderList = Get-ChildItem $Server | select name
# I log onto the AD and execute my requests
cd AD:
foreach ($Folder in $FolderList){

然后,当我在一台服务器上完成后,我将继续使用我的D驱动器并在下一个驱动器上重新启动。 我必须在每次连接到服务器之间登录和注销,因为连接到AD后我无法访问我的服务器(我可以访问本地驱动器并编写输出文件)。

现在我只对每个服务器的AD发出一次登录请求,而不是每个文件一个。