Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract publickeytoken using powershell?

I am trying to check whether the assembly is strong name signed or not.

Following is my powershell script

function Get-AssemblyStrongName($assemblyPath)
{
    [System.Reflection.AssemblyName]::GetAssemblyName($assemblyPath).FullName 
}

$File = Get-ChildItem -Path $args[0] -Include @("*.dll","*.exe") -Recurse

Foreach ($f in $File)
{
  $assembly=$f.FullName

  Get-AssemblyStrongName $assembly

}

The output looks below.

Sampledll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=70ea1bb087B4tbed

If publickeytoken is null i think it is not signed. I was trying to write if condition like

$Assembly=Get-AssemblyStrongName $assembly
If ($assembly -contains "null" ) { "assembly is not signed" }

Though many exe or dll are having publickey as null but still it displays no output as "assembly is not signed".

How to check with if condition to find whether the publickeytoken is null or not? (or the assembly is signed or not)

like image 555
Samselvaprabu Avatar asked Nov 21 '25 11:11

Samselvaprabu


1 Answers

Another option is to use the GetPublicKeyToken() method which returns either a populated or empty byte array e.g.:

function Test-AssemblyStrongNamed($assemblyPath) {
    [reflection.assemblyname]::GetAssemblyName($assemblyPath).GetPublicKeyToken().Count -gt 0
}
like image 81
Keith Hill Avatar answered Nov 23 '25 02:11

Keith Hill