Author Posts

September 1, 2015 at 7:52 am

Hello all,
Having a problem inserting to MSSQL server Database.

$user = ([adsisearcher]"(&(objectClass=user)(samaccountname=$owner))").FindOne().Properties
$samAccountname = $user.samaccountname
$UserPrincipalName = $user.userprincipalname
$DistinguishedName = $user.distinguishedname

$sqlCmd = $sqlConn.CreateCommand()
$sqlCmd.Parameters.AddWithValue("@user_login" ,$samAccountname)
$sqlCmd.Parameters.AddWithValue("@user_domain" ,$DOMAIN)
$sqlCmd.Parameters.AddWithValue("@distin_name" ,$DistinguishedName)
$sqlCmd.Parameters.AddWithValue("@update" ,$date)
$sqlCmd.CommandText = "INSERT INTO [dbo].[USERS] ([USER_LOGIN],[USER_DOMAIN],[DISTIN_NAME],[UPDATE],[USER_PINCIPAL]) VALUES (@user_login,@user_domain,@distin_name,@update,@user_pincipal)";
$sqlCmd.ExecuteNonQuery(); #Here gives ERROR

The error it gives is :
Exception calling "ExecuteNonQuery" with "0" argument(s): "No mapping exists from object type System.DirectoryServices.ResultPropertyValueCollection to a known managed provider native type."
At line:1 char:1
+ $sqlCmd.ExecuteNonQuery();
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException

Can anyone help me with this?


September 1, 2015 at 8:13 am

One of the values you're inserting, from your variables, is actually an object rather than a simple value. SQL doesn't know how to turn the object into a value it understands, like a string or number. Check the contents of your variables – one or more of them doesn't contain what you expected.

September 1, 2015 at 8:38 am

Tansk again Don.

[string]$samAccountname = $user.samaccountname
[string]$UserPrincipalName = $user.userprincipalname
[string]$DistinguishedName = $user.distinguishedname

Solved my problem.