如何在 PowerShell 中加载程序集? [英] How to load assemblies in PowerShell?
本文介绍了如何在 PowerShell 中加载程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下 PowerShell 代码
The following PowerShell code
#Get a server object which corresponds to the default instance
$srv = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server
... rest of the script ...
给出以下错误信息:
New-Object : Cannot find type [Microsoft.SqlServer.Management.SMO.Server]: make sure
the assembly containing this type is loaded.
At C:\Users\sortelyn\ ... \tools\sql_express_backup\backup.ps1:6 char:8
+ $srv = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
互联网上的每个答案都写到我必须加载程序集 - 很确定我可以从错误消息中读取它:-) - 问题是:
Every answer on the Internet writes that I have to load the assembly - well sure I can read that from the error message :-) - the question is:
你如何加载程序集并使脚本工作?
How do you load the assembly and make the script work?
推荐答案
LoadWithPartialName
已弃用.PowerShell V3 的推荐解决方案是使用 Add-Type
cmdlet,例如:
LoadWithPartialName
has been deprecated. The recommended solution for PowerShell V3 is to use the Add-Type
cmdlet e.g.:
Add-Type -Path 'C:\Program Files\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.Smo.dll'
有多个不同的版本,您可能需要选择一个特定的版本.:-)
There are multiple different versions and you may want to pick a particular version. :-)
这篇关于如何在 PowerShell 中加载程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文