0

我的要求是在GAC中注册dll,我正在使用gacutil。我写的coe如下。

function RegisterAssembliesToGAC([string]$frameworkPath,[string]$GACDllLocation)
{
    try
    {
       $Dirs = Get-ChildItem $GACDllLocation -Recurse 
        $Dlls = $Dirs | Where { $_.extension -eq ".dll" }

        ForEach($dll in $Dlls)
        { 

            C:\Windows\Microsoft.NET\Framework64\v4.0.30319\gacutil.exe -i $dll.FullName
        }   

    }
    catch [Exception]
    {
    write-host $_.Exception.Message `n;
    }

}

这对我来说很好。现在我如图所示 $frameworkpath 是一个参数,我想将它的值作为参数传递。所以我修改了我的代码如下

function RegisterAssembliesToGAC([string]$frameworkPath,[string]$GACDllLocation)
{
    try
    {
       $Dirs = Get-ChildItem $GACDllLocation -Recurse 
        $Dlls = $Dirs | Where { $_.extension -eq ".dll" }

        ForEach($dll in $Dlls)
        {       

        $frameworkPath="C:\Windows\Microsoft.NET\Framework64\v4.0.30319"  
    $gacpath=[string]$frameworkPath + "\gacutil.exe"
        Invoke-Expression "$gacpath -i $dll.FullName"


        }   

    }
    catch [Exception]
    {
    write-host $_.Exception.Message `n;
    }

}

这给出了一个错误:将程序集添加到缓存失败:文件名、目录名或卷标语法不正确。试了很多次,无法修复。请帮忙 :)

4

1 回答 1

1

第一的:

$frameworkPath如果在脚本中总是给它一个值,参数就没有意义:

$frameworkPath="C:\Windows\Microsoft.NET\Framework64\v4.0.30319"

第二:您需要进行此更改:

Invoke-Expression "$gacpath -i $($dll.FullName)"

因为在字符串中,变量参数扩展需要包含在$(). 如果在您之前添加的脚本中,iex"$gacpath -i $dll.FullName"可以看到如何评估。

第三:我建议使用 join-path cmdlet 来构建你的路径:

$gacpath= join-path $frameworkPath "gacutil.exe"
于 2013-05-31T06:53:09.463 回答