简体   繁体   English

Powershell - 将 SQL 与 AD 进行比较

[英]Powershell - Comparing SQL to AD

I need to compare one list to the other and create a third list of every employee ID found in AD, but not in SQL.我需要将一个列表与另一个列表进行比较,并为在 AD 中找到的每个员工 ID 创建第三个列表,而不是在 SQL 中。 I have two commands that work to pull the data from each.我有两个命令可以从每个命令中提取数据。 I'm struggling to create a command that will combine these in a way to do what I want.我正在努力创建一个命令,它将以某种方式结合这些来做我想做的事。

One connects to a SQL database and is pulling any current employees from there.一个连接到 SQL 数据库,并从那里拉出所有当前员工。

$sqlpeeps = Invoke-Sqlcmd -ServerInstance '192.168.1.1' -Database 'DATABASE' 
-Query "SELECT * FROM [COMPANY].[dbo].[employee] WHERE EmployeeStatus in 
('A', 'S', 'L')"

The other command is grabbing all of our active AD accounts.另一个命令是获取我们所有的活动 AD 帐户。

$adpeeps = get-aduser -filter * -searchbase 
"OU=Users,OU=Logins,DC=COMPANY,DC=COM" -properties * 

I think what I need is some sort of foreach loop, but I can't seem to find a way to say "not in" with powershell, so I am having trouble writing it.我认为我需要的是某种 foreach 循环,但我似乎找不到用 powershell 说“不在”的方法,所以我在编写它时遇到了麻烦。

$adpeeps | foreach ($_.EmployeeID in $sqlpeeps) <do nothing?> else {out-file 
"C:\users\user\Desktop\here.txt"}

If it makes helping me with this easier, there is one column in the SQL data called FILE# which directly correlates to an AD attribute, EmployeeID.如果它使我更容易解决这个问题,SQL 数据中有一个名为 FILE# 的列,它与 AD 属性 EmployeeID 直接相关。 Is there an easy way to cut out all extraneous data so I am only using these two columns for comparison?有没有一种简单的方法可以删除所有无关的数据,所以我只使用这两列进行比较?

Ideally, the script needs to find AD accounts that were deleted out of our SQL table-- in other words, a list to hand off for manual deletion.理想情况下,该脚本需要找到从我们的 SQL 表中删除的 AD 帐户 - 换句话说,要手动删除的列表。

i assume that $sqlpeeps has a column EmployeeID (it it really is called "FILE#" you can use the AS key in your query like described here )我假设$sqlpeeps有一列EmployeeID (它实际上被称为“FILE#”,您可以在查询中使用AS键,如此所述)

# iterate through $adpeeps and process each user as $aduser 
foreach($aduser in $adpeeps) {
    # test if the sql column EmployeeID does not contain the employeeid of the ad user
    if($sqlpeeps.EmplyeeID -notcontains $aduser.employeeid) {
        Out-File -Path "C:\users\user\Desktop\here.txt" -Append
    }
}

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

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