Powershell 调用表达式调用时出错
我的要求是在GAC中注册DLL,我正在使用gacutil进行注册。我编写的coe如下Powershell 调用表达式调用时出错,powershell,Powershell,我的要求是在GAC中注册DLL,我正在使用gacutil进行注册。我编写的coe如下 function RegisterAssembliesToGAC([string]$frameworkPath,[string]$GACDllLocation) { try { $Dirs = Get-ChildItem $GACDllLocation -Recurse $Dlls = $Dirs | Where { $_.extension -eq ".dll"
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;
}
}
将程序集添加到缓存时出错:文件名、目录名或卷标语法不正确。尝试了很多次,无法修复。
请帮助:)首先:
$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"
非常感谢。它成功了。我在函数中指定了$frameworkpath,以显示我正在形成的路径。无论如何,非常感谢。@user1595214很高兴能提供帮助!