Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

powershell - How do I use Add-Type to load Microsoft.Web.Deployment?

I am writing some PowerShell scripts that use the MSDeploy API. I can load the assembly using

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Deployment")

The location is in the GAC:

PS > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Deployment") | fl Location

Location : C:WindowsassemblyGAC_MSILMicrosoft.Web.Deployment7.1.0.0__31bf3856ad364e35Microsoft.Web.Deployment.dll

However, I am not able to load the assembly using Add-Type. I get an error saying the assembly cannot be found and that one or more assemblies are missing.

PS > Add-Type -AssemblyName Microsoft.Web.Deployment
Add-Type : Cannot add type. The assembly 'Microsoft.Web.Deployment' could not be found.
At line:1 char:9
+ Add-Type <<<<  -AssemblyName Microsoft.Web.Deployment
    + CategoryInfo          : ObjectNotFound: (Microsoft.Web.Deployment:String) [Add-Type], Exception
    + FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. One or more required assemblies are missing.
At line:1 char:9
+ Add-Type <<<<  -AssemblyName Microsoft.Web.Deployment
    + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
    + FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

How can I use Add-Type to load Microsoft.Web.Deployment?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

PowerShell only allows a certain pre-defined set of assemblies to be loaded by their partial/simple name. You're going to need to load it via its fully qualified name, for example:

Add-Type -AssemblyName ('Microsoft.Web.Deployment, Version=7.1.0.0, ' +
                        'Culture=neutral, PublicKeyToken=31bf3856ad364e35')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...