-1

I have a load.PS1 and it loads 'Microsoft.SqlServer.Smo' as below

[System.Reflection.Assembly]::load('Microsoft.SqlServer.Smo') | Out-Null

it works well when I use the ISE to load and run it , but get error when I run it in the powershell shell or use as "powershell -command ".\RemoveInvalidSSRSAgentJob.ps1 xxxx" as below

Exception calling "Load" with "1" argument(s): "Could not load file or assembly
 'Microsoft.SqlServer.Smo' or one of its dependencies. The system cannot find t
he file specified."
At line:1 char:38
+ & {[System.Reflection.Assembly]::load <<<< ('Microsoft.SqlServer.Smo') | Out-
Null}
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

how can I set it so that it can work in the powershell shell or command?

Thanks

user1504146
  • 79
  • 2
  • 8

1 Answers1

1

You can workaround using

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Smo')

but this method it's declared 'obsolete' from microsoft and in future can be eliminated.

The method load fails because it loads an assembly given the long form of its name but your ar passing to it a partial name: Read here. And I find really strange that in ISE works, for me fails!

CB.
  • 58,865
  • 9
  • 159
  • 159