Here is my powershell statement with associated error. Can't figure out what I am doing wrong.
PS H:\> Get-Content C:\temp\groups.txt | ForEach-Object {
$Group = $_
$Results = Get-ADGroupMember $Group |
Select-Object @{Name="Group Name";Expression={$Group}},name
} | out-host
$conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=LSI-1; Initial Catalog=databasename; Integrated Security=SSPI")
$conn.Open()
$insert_stmt = "INSERT INTO ADGroupMembers('$Results')"
$cmd = $conn.CreateCommand()
$cmd.CommandText = $insert_stmt
$cmd.ExecuteNonQuery()
$conn.Close()
Exception calling "ExecuteNonQuery" with "0" argument(s): "Incorrect syntax near ')'."
At line:11 char:1
+ $cmd.ExecuteNonQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException
Any assistance would be greatly appreciated. Also I am planning on running this script once a day and would probably like to drop and re-add the table with the new data. Insight into this would also be beneficial
$conn = New-Object System.Data.SqlClient.SqlC
$conn.Open()
$cmd = $conn.CreateCommand()
#delete existing contents of this table (enter sqlDB Name below)
$cmd.CommandText = "truncate table " + $dbname+ ".ADGroupMembers"
$cmd.ExecuteNonQuery()
#import new/current data
$content = Get-Content c:\temp\groups.txt
foreach ($Group in $content)
{
$Results = Get-ADGroupMember $Group
Select-Object @{Name="GroupName";Express
foreach ($name in $Results)
{
$insert_stmt = "INSERT INTO ADGroupMembers VALUES('"+ $group +"', '"+ $name.name +"')"
write $insert_stmt
$cmd.CommandText = $insert_stmt
$cmd.ExecuteNonQuery()
}
}
$conn.Close()