Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am updating some powershell code to install Database Tables/stored procedures on a 64 bit machine. I keep getting an error when I try to assign the login type. Here is my current code.

$login = new-Object Microsoft.SqlServer.Management.SMO.Login($server, $user);
    if ($server.Logins.Contains($login.Name) -eq $false)
    {
        $login.LoginType = [Microsoft.SqlServer.Management.SMO.LoginType]::WindowsUser;
        $login.Create();
        UTF-Write-Host "Login Created on $database" -type info
    }

and the error is the following;

Error During User Operations Exception setting "LoginType": "Cannot convert value "WindowsUser" to type "Microsoft.SqlServer.Management.Smo.LoginType". Error: "Invalid cast from 'Microsoft.SqlServer.Management.Smo.LoginType' to 'Microsoft.SqlServer.Management.Smo.LoginType'.""

Does anyone have any hints as to what I am doing wrong?

(Edit) I include the following

[void][Reflection.Assembly]::Load("Microsoft.SqlServer.SqlEnum, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")
share|improve this question
    
PowerShell v1 or v2? –  Kev Aug 5 '11 at 14:48
    
Powershell version 2 –  k_Dank Aug 5 '11 at 15:10
    
Also it is x86. –  k_Dank Aug 5 '11 at 15:11
    
[Reflection.Assembly]::Load will not return an error if you don't have the assembly on the machine. Do you have SQL 2008 installed on your machine and can you post the rest your code for loading the assemblies? –  Chad Miller Aug 5 '11 at 16:42

2 Answers 2

Did you load the SQLEnum assembly?

Assuming you're using SQL Server 2008 or 2008 R2:

add-type -AssemblyName "Microsoft.SqlServer.SqlEnum, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
share|improve this answer
    
I was on version 9.0.242.0. I switched to 10.0.0.0, but I still get the same results –  k_Dank Aug 5 '11 at 15:44

Not sure how you're loading your assemblies, however this works for me:

[Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfo.dll")
[Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Smo.dll")

# Then your code.

I tested this on a 64 bit PowerShell v2 instance.

What I suspect is that you're missing this assembly: Microsoft.SqlServer.SqlEnum.dll.

This is where the LoginType enum is declared.

On my 64 bit machine this resides in:

C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\

The 32 bit assemblies reside here in a 64 bit machine:

C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies

On this machine I have the following SQL Server 2008 R2 feature pack items installed:

  • SQL Server 2008 R2 Native Client (x64)
  • SQL Server 2008 R2 Management Objects (x86)
  • SQL Server 2008 R2 Management Objects (x64)
  • SQL Server System CLR Types (x86)
  • SQL Server System CLR Types (x64)

You can find these here:

Microsoft SQL Server 2008 R2 Feature Pack

I also built the same code in C# in VS 2010 and if the Microsoft.SqlServer.SqlEnum assembly isn't referenced then you get a compile error.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.