none
Sometimes: Unable to load assembly RRS feed

  • General discussion

  • Not sure that this is a FIM question exactly, but here goes.

    I'm running FIM 2010 SP1 on a Windows Server 2012 box.

    When running my Powershell import script (below) from a prompt, it runs perfectly always;

    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;database=xxxx;server=server,port;Connect Timeout=30"
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = $sql
    $SqlCmd.Connection = $SqlConnection
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $DataSet = New-Object System.Data.DataSet
    $null = $SqlAdapter.Fill($DataSet)
    $SqlConnection.Close()
    $global:users = $DataSet.Tables[0]

    However when run from my PS MA, I sometimes get this error

    29-04-2013 08:54:32: System.Management.Automation.RuntimeException: Unable to find type [assembly.reflection]: make sure that the assembly containing this type is loaded.

    It is the System.Data.SqlClient.SqlConnection, that cannot be loaded.

    What is really killing me, is that it runs fine "sometimes", usually if I don't touch the box for a while it works (I have scheduled tasks running the import as well). I can't seem to figure out what the ... is going on - it seems as if some DLL or similar is unloaded and then the import script works fine for a while. But the strange thing is that it runs perfectly from a PS prompt everytime.

    Suggestions?


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt


    Monday, April 29, 2013 10:02 AM

All replies

  • * UPDATE *

    Now I'm even more confused... - I put this in the start of my script for debugging purposes - and suddenly it runs :-|

    [System.AppDomain]::CurrentDomain.GetAssemblies() | Out-File C:\Temp\Assem.txt


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    Monday, April 29, 2013 12:44 PM
  • Where is the load statement? "[assembly.reflection]" - should be "[Reflection.Assembly]"

    My Book - Active Directory, 4th Edition
    My Blog - www.briandesmond.com

    Monday, April 29, 2013 2:33 PM
    Moderator
  • Thanks, Brian...

    It works "sometimes" even without the load statement. For entirety - full script enclosed; nothing fancy...

    param (
    	$Username,
    	$Password,
        $OperationType
    )
    [System.AppDomain]::CurrentDomain.GetAssemblies() | out-file c:\temp\assem.txt
    $sql = @'
    WITH summary AS (
        SELECT 
    			p.cpr as ssn,
    			p.orgid as orgunitid,
    			p.title,
    			p.startdato,
    			p.stopdato,
    			convert(datetime, p.startdato) as startdate,
    			case p.stopdato
    				when null then 
    					convert(datetime, '2059-01-01')
    				else
    					convert(datetime, p.stopdato)
    				end as enddate,
               ROW_NUMBER() OVER(PARTITION BY p.cpr
                                     ORDER BY p.stopdato DESC) AS rk
          FROM ansaettelser p
          where syncstatus = 'logisk'
          )
    SELECT 
    		s.ssn, s.orgunitid, s.title, s.startdate, s.enddate, s.startdato, s.stopdato,
    		case when  (getdate() <= s.enddate)
    		then
    		 1
    		 else
    		 0
    		 end as isactive
      FROM summary s
      WHERE rk = 1
    '@
    $constr = "Persist Security Info=False;Integrated Security=SSPI;database=xxxx;server=xxx;Connect Timeout=30"
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection $constr
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = $sql
    $SqlCmd.Connection = $SqlConnection
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $DataSet = New-Object System.Data.DataSet
    $null = $SqlAdapter.Fill($DataSet)
    $SqlConnection.Close()
    $global:users = $DataSet.Tables[0]
    foreach ($person in $users) 
    {
    	$obj = @{}
    	$obj.id = $person["ssn"]
    	$obj.ssn = $person["ssn"]
    	$obj.objectClass = "user"
    	$obj.orgunit = $person["orgunitid"]
    	$obj.isactive = [bool] $person["isactive"]
    	$obj.islogical = $True
    	if ($person["title"] -notmatch '^$') { $obj.title = $person["title"] }
    	
    	if ($person["startdato"] -notmatch '^$')
    	{
    		$obj.startdate = ( [datetime]::parse($person["startdato"]) ).ToString("yyyy-MM-dd 00:00:00")
    	}
    	if ($person["stopdato"] -notmatch '^$')
    	{
    		$obj.enddate = ( [datetime]::parse($person["stopdato"]) ).ToString("yyyy-MM-dd 00:00:00")
    	}
    	$obj
    }


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    Monday, April 29, 2013 2:42 PM
  • How about a stacktrace?  This isn't enough information to verify that System.Data.SqlClient.SqlConnection is at fault...

    Steve Kradel, Zetetic LLC

    Monday, April 29, 2013 8:09 PM
  • There is no stacktrace from PowerShell; however, I get this in the eventlog

    The extensible extension returned an unsupported error.
     The stack trace is:
     
     "Microsoft.MetadirectoryServices.ExtensibleExtensionException: Cannot find type [System.Data.SqlClient.SqlConnection]: make sure the assembly containing this type is loaded.
       at Granfeldt.PowerShellManagementAgent.GetImportEntries(GetImportEntriesRunStep importRunStep)
    Forefront Identity Manager 4.1.3419.0"

    Today, it stopped working again after running fine (unchanged) for several import runs.

    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    Tuesday, April 30, 2013 12:46 PM
  • Hmm, this suggests that some inconsistency in the Powershell Runspace hosted in the ECMA.

    How about sprinkling a bit of "[System.Reflection.Assembly]::LoadWithPartialName" into the Powershell script itself?


    Steve Kradel, Zetetic LLC

    Tuesday, April 30, 2013 5:54 PM
  • Steve,

    Already been there - doesnt make a difference and other sources tells me that LoadWithPartialName is deprecated btw - would use if it if it worked, though...

    Btw, I've never seen this on a Windows Server 2008 R2 box running the same PS MA - it is something new on this Windows Server 2012 box. Even tried installing the SQL 2008 R2 Native Client - same problem.

    I'm still confused.


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    Tuesday, April 30, 2013 6:54 PM
  • I don't know if this helps, but I recently had a similar problem.

    I was advised to add "Add-Type -AssemblyName System.Data" to the start of the script.

    It worked.

    Monday, October 7, 2013 6:14 PM
  • Thanks.

    I'll give a go when I get the chance.


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    Monday, October 7, 2013 6:18 PM
  • Hi Søren,

    Did you ever find a solution to this issue? Happening to me also when using your PS MA on server 2012.

    [Cannot find type [System.Data.SQLClient.SQLConnection]: make sure the assembly containing this type is loaded.] 

    If I add Add-Type -AssemblyName System.Data to the top of my script it works. My import script works without this fix it's only the export.

    thanks again for your great work

    Clayton


    Thursday, September 18, 2014 5:06 AM
  • Still unresolved but had to workaround to finish project on time

    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    Thursday, September 18, 2014 6:08 AM
  • I have exactly the same problem. I load in DLLs using either Add-Type -Path "<Full path of dll>", or "[Reflection.Assembly]::LoadFrom(<Full path of dll>)".

    Sometimes i can access things within the DLLs, sometimes i can't. It's totally random, but when it works it works for a long period of time, when it fails, it fails for long periods of time. It's completely out of my control and it's driving me crazy. If this continues I'm going to have to abandon powershell for the bug ridden POS it really is.

    When it doesn't work it gives the error:

    System.Management.Automation.RuntimeException: Unable to find type [...]

    No language should ever have non-deterministic behaviour.

    Wednesday, August 2, 2017 3:41 PM
  • This issue can occur when PowerShell script is being run by a C# app using .net RunSpace

    It happens when the system has .Net 4 and Powershell 2, and the C# app is running under CLR 4

    You will find the assemblies are loaded fine, but the specific [System.Data.SqlClient.SqlConnection] language feature fails for some reason.

    The solution is to directly access the type. Sample given relates to some WMI code I have, but it is essentally the same.

    $sysManagement = [System.Reflection.Assembly]::LoadWithPartialName("System.Management");

    $netType = $sysManagement.GetType("System.Management.ManagementDateTimeConverter",$true);

    $sinceTime = $netType::ToDmtfDateTime($threeHrsago)



    Wednesday, November 1, 2017 3:25 AM