0
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.

Olaf
  • 4,690
  • 2
  • 15
  • 23
  • 2
    It might be just a copy and paste error here but there is a `$_` missing in front of the term `.SideIndicator` – Olaf Feb 09 '22 at 21:50
  • Besides, I don't think that your `$Insert` string will expand automatically, you will need something like [`$ExecutionContext.InvokeCommand.ExpandString($Insert)`](https://stackoverflow.com/a/56627626/1701026) (or put the [expandable string](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.2#interpretation-of-expandable-strings) inside the loop). – iRon Feb 10 '22 at 06:49

1 Answers1

0

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
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56