'How do I compare users from SQL query to AD then insert them?
Import-Module ActiveDirectory
$ADUSER = Get-Aduser -Filter * -Properties SamAccountName | Select-Object SamAccountName
$SQLQUERY = Invoke-Sqlcmd -Query "SELECT username AS SamAccountName FROM test.dbo.ad" -Database "TEST" -Server "DB-1"
$Compare = Compare-Object $ADUSER $SQLQUERY -Property 'SamAccountName' -passthru | ?{.SideIndicator -Eq '=>'} | SELECT SamAccountName
$Insert = "INSERT INTO TEST.dbo.ad SELECT $user" -Database "TEST" -Server "DB-1"
Foreach ($user in $Compare){
Invoke-Sqlcmd -Query $Insert
}
I am using Powershell 5.0 and when I run this I get an error ".SideIndicator : The term '.SideIndicator' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again." What am I doing wrong? I am trying to pull AD users then a query and then compare AD to SQL and insert in.
Solution 1:[1]
As Olaf pointed out, you are missing the $_ from your Where statement on the fourth line. It should read:
$Compare = Compare-Object $ADUSER $SQLQUERY -Property 'SamAccountName' -passthru | ?{$_.SideIndicator -Eq '=>'} | SELECT SamAccountName
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TheMadTechnician |
