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)
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With