无法转换Value - Powershell插入到sql中

时间:2015-05-27 13:31:29

标签: sql powershell

我只运行一个脚本,它将用户从AD中取出并将其信息放入数据库中。由于@符号,此脚本片段无法插入我认为的电子邮件地址。我该如何解决这个问题?

    $_.samaccountname  | Get-QADGroupMember -type user -SizeLimit 0 -DontUseDefaultIncludedProperties -IncludedProperties samaccountname,DN | Select samaccountname,FirstName,LastName,Email,Manager,Office,Title,HomeDirectory,HomeDrive,PasswordLastSet,PasswordAge,PasswordExpires,AccountIsDisabled,AccountIsLockedOut,PasswordNeverExpires,Domain,Notes,CreationDate,ModificationDate,Type,Description,DN | foreach{
    if (!$_.samaccountname){$samaccountname = ''}   ELSE{$samaccountname = $_.samaccountname}
    if (!$_.FirstName){$FirstName = ''}             ELSE{$FirstName = $_.FirstName}
    if (!$_.LastName){$LastName = ''}               ELSE{$LastName = $_.LastName}
    if (!$_.Email){$Email = ''}                     ELSE{$Email = $_.Email}
    if (!$_.Manager){$Manager = ''}                 ELSE{$Manager = $_.Manager}
    if (!$_.Office){$Office = ''}                   ELSE{$Office = $_.Office}
    if (!$_.Title){$Title = ''}                     ELSE{$Title = $_.Title}
    if (!$_.HomeDirectory){$HomeDirectory = ''}     ELSE{$HomeDirectory = $_.HomeDirectory}
    if (!$_.HomeDrive){$HomeDrive = ''}             ELSE{$HomeDrive = $_.HomeDrive}
    if (!$_.PasswordExpires){$PasswordExpires = ''} ELSE{$PasswordExpires = $_.PasswordExpires}
    if (!$_.Notes){$Notes = ''}                     ELSE{$Notes = $_.Notes}
    if (!$_.Description){$Description = ''}         ELSE{$Description = $_.Description}

    $cmd.CommandText = "INSERT dbo.Users (AccountName,FirstName,LastName,Email,Manager,Office,Title,HomeDirectory,HomeDrive,PasswordLastSet,PasswordAge,PasswordExpires,AccountIsDisabled,AccountIsLockedOut,PasswordNeverExpires,Domain,Notes,CreationDate,ModificationDate,Type,Description) VALUES ('"+$samaccountname+"','"+$FirstName+"','"+$LastName+"','"+$Email+"','"+$Manager+"','"+$Office+"','"+$Title+"','"+$HomeDirectory+"','"+$HomeDrive+"','"+$_.PasswordLastSet+"','"+$_.PasswordAge+"','"+$PasswordExpires+"','"+$_.AccountIsDisabled+"','"+$_.AccountIsLockedOut+"','"+$_.PasswordNeverExpires+"','"+$_.Domain+"','"+$Notes,+"','"+$_.CreationDate+"','"+$_.ModificationDate+"','"+$Description+"','"+$_.DN+"')"
    $cmd.ExecuteNonQuery() | out-null

我收到错误

&#34;无法转换价值&#34;&#39;,&#39;&#34;输入&#34; System.Int32&#34;。错误:&#34;输入字符串的格式不正确。&#34;在C:\ Users \ rld0072 \ Desktop \ Group-Users.ps1:28 char:9 + $ cmd.CommandText =&#34; INSERT dbo.Users(AccountName,FirstName,LastName,Ema ...&#34; < / p>

1 个答案:

答案 0 :(得分:1)

你的$ cmd.CommandText行中有一个拼写错误。 $ Notes后面有一个额外的逗号。