Question [Fonction] Enregistrement d'assembly dans le GAC

Plus d'informations
il y a 11 ans 11 mois #11839 par Laurent Dardenne
Cette fonction évite l'usage de l'utilitaire GacUtil, utile pour un setup :
[code:1]
function Publish-Gac {
#Insére un assembly dans le gac sans passer par l'utilitaire Gacutil du SDK
# gacutil - je \"C:\projet\log4net.dll\"
#from weblogs.asp.net/adweigert/archive/2008/1...-for-powershell.aspx
param ($assembly)
BEGIN {
$ErrorActionPreference = \"Stop\"

if ( $null -eq ([AppDomain]::CurrentDomain.GetAssemblies() |? { $_.FullName -eq \"System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\" }) ) {
[System.Reflection.Assembly]::Load(\"System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\"«») | Out-Null
}

$publish = New-Object System.EnterpriseServices.Internal.Publish
}
PROCESS {
$assembly = $null

if ( $_ -is [string] ) {
$assembly = $_
} elseif ( $_ -is [System.IO.FileInfo] ) {
$assembly = $_.FullName
} else {
throw (\"The object type '{0}' is not supported.\" -f $_.GetType().FullName)
}

if ( -not (Test-Path $assembly -type Leaf) ) {
throw \"The assembly '$assembly' does not exist.\"
}

if ( [System.Reflection.Assembly]::LoadFile( $assembly ).GetName().GetPublicKey().Length -eq 0 ) {
throw \"The assembly '$assembly' must be strongly signed.\"
}

Write-Output \"Installing: $assembly\"

$publish.GacInstall( $assembly )
}
}
[/code:1]

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 11 ans 8 mois #12472 par Matthew BETTON
Salut Laurent,

Merci pour ce code.

Juste une précision : il faut nécessairement renvoyer le chemin ou le fichier correspondant à l'assemby via le pipe.

La fonction de marche pas de cette façon :

[code:1]Publish-Gac -assembly \"...\...\"[/code:1]

Dans ce cas, le message d'erreur systématique est :

[code:1]Vous ne pouvez pas appeler de méthode sur une expression ayant la valeur Null.
throw (\"The object type '{0}' is not supported.\" -f $_.GetType( <<<< ).FullName
[/code:1]

@+

Matthew

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 11 ans 8 mois #12475 par Laurent Dardenne
Bonjour Matthew,
merci pour le retour.
Comme quoi les tests unitaires, ça sert :)

Reste à trouver le temps pour corriger...

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 11 ans 6 mois #12918 par Laurent Dardenne
Une nouvelle version :
[code:1]
function Publish-Gac {
[CmdletBinding()]
#Insére un assembly dans le gac sans passer par l'utilitaire Gacutil du SDK
#Adaptation de weblogs.asp.net/adweigert/archive/2008/1...-for-powershell.aspx
param (
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory=$true,ValueFromPipeline = $true)]
[Alias('FullName')]
[String] $Path,
[switch] $ForceVerification
)
BEGIN {
$Code=@'
[DllImport(\"mscoree.dll\", CharSet=CharSet.Unicode)]
public static extern bool StrongNameSignatureVerificationEx(string wszFilePath, bool fForceVerification, ref bool pfWasVerified);
'@
Add-Type -memberDefinition $Code -name \"Verification\" -namespace Win32Functions

Add-Type -AssemblyName \"System.EnterpriseServices\"
$Publisher = New-Object System.EnterpriseServices.Internal.Publish
}

PROCESS {
Write-Debug \"Path=$Path\"
Resolve-Path $Path|
Foreach {
$Current=$_
Write-Debug \"Current assembly =$Current\"
[bool] $isForceVerification=$ForceVerification
[ref] $WasVerified=$False
if ( -not [Win32Functions.Verification]::«»StrongNameSignatureVerificationEx($Current,$isForceVerification,$WasVerified))
{ Write-Error \"The assembly '$Current' must be strongly signed.\" }
else
{
try{
$Publisher.GacInstall( $Current)
Write-Verbose \"Installing: $Current\"
} catch [System.Security.SecurityException] {
#\"A caller in the call chain does not have permission to access unmanaged code.\"
$PSCmdlet.WriteError(
(New-Object System.Management.Automation.ErrorRecord(
$_,
\"NoAccessPermission\",
\"SecurityError\",
$Path # Si $ErrorView=\"CategoryView\" l'information est affichée
)
)
)#WriteError
}#catch
}
}#for
}#process
} #Publish-Gac
[/code:1]

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.072 secondes
Propulsé par Kunena