使用Powershell在Gac中注册文件

13 powershell gac gacutil

有没有一种简单的方法在PowerShell中(我想使用gacutil.exe)从文本文档中读取路径\程序集并在GAC中注册它?例如,一个.txt文件看起来像:

c:\test\myfile.dll
c:\myfile2.dll
d:\gac\gacthisfile.dll
Run Code Online (Sandbox Code Playgroud)

PowerShell脚本会将其读入流中,然后在找到的每个程序集上运行gacutil?我想这会是这样的:

#read files into array?

foreach ($file in Get-ChildItem -Filter "*.dll" )
{ 
  Write-Host $file.Name
  C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\gacutil.exe /nologo /i $file.Name
}
Run Code Online (Sandbox Code Playgroud)

小智 24

让.Net担心gacutil怎么样?

# load System.EnterpriseServices assembly
[Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices") > $null

# create an instance of publish class
[System.EnterpriseServices.Internal.Publish] $publish = new-object System.EnterpriseServices.Internal.Publish

# load and add to gac :)
get-content fileOfDlls.txt | ?{$_ -like "*.dll"} | Foreach-Object {$publish.GacInstall($_)}
Run Code Online (Sandbox Code Playgroud)


zda*_*dan 5

如果您对文本文件进行整理以使每个dll位于单独的行上,则可以使用Get-Content命令并将每个dll管道输出到执行命令的过滤器:

filter gac-item { C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\gacutil.exe /nologo /i $_}

get-content fileOfDlls.txt | ?{$_ -like "*.dll"} | gac-item
Run Code Online (Sandbox Code Playgroud)