PowerShell清单引用外部程序集

时间:2010-01-22 21:01:45

标签: .net powershell powershell-v2.0

是否可以通过定义新模块清单并将其放入指定目录来注册新的PowerShell模块?

E.g。应创建一个名为SampleModule的新模块。我将空清单文件SampleModule.psd1放在目录<%PSModulePath%>\SampleModule中。 (使用哪个(用户或全局)模块路径无关紧要。)

这足以让PowerShell使用Get-Module -ListAvailable命令列出我的新模块。

在下一步中,我尝试填充清单并将ModuleToProcess属性设置为程序集,该程序集位于另一个目录中。调用Import-Module失败,PowerShell无法找到程序集。

2 个答案:

答案 0 :(得分:1)

确实如此。事实上,我有一个非常好的技巧,可以为SQL snapin执行此操作。代码的第一个块是一个函数,它将从一个模块位置为您创建模块清单。它仍会提示您(如New-ModuleManifest cmdlet所做的那样)以获取某些必需参数,例如description。它找到任何类型和格式的文件,以及任何.dll的名为 .ps .dll(大多数snapins遵循此约定),然后它创建一个包装原始模块的模块清单。第二块代码专门用于SQL PSSnapin。

function New-ModuleManifestFromSnapIn {

    [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
    param(

    [Parameter(Mandatory=$true, Position=0)]
    [System.String]
    ${Path},

    # The location in the filesystem where the V1 snapin resides
    [Parameter(Mandatory=$true)]
    [System.String]
    ${OriginalPath},

    [System.Guid]
    ${Guid},

    [Parameter(Mandatory=$true)]
    [AllowEmptyString()]
    [System.String]
    ${Author},

    [Parameter(Mandatory=$true)]
    [AllowEmptyString()]
    [System.String]
    ${CompanyName},

    [Parameter(Mandatory=$true)]
    [AllowEmptyString()]
    [System.String]
    ${Copyright},

    [ValidateNotNull()]
    [System.Version]
    ${ModuleVersion},

    [Parameter(Mandatory=$true)]
    [AllowEmptyString()]
    [System.String]
    ${Description},

    [System.Reflection.ProcessorArchitecture]
    ${ProcessorArchitecture},

    [System.Version]
    ${PowerShellVersion},

    [System.Version]
    ${ClrVersion},

    [System.Version]
    ${DotNetFrameworkVersion},

    [System.String]
    ${PowerShellHostName},

    [System.Version]
    ${PowerShellHostVersion},

    [System.Object[]]
    ${RequiredModules},

    [AllowEmptyCollection()]
    [System.String[]]
    ${ScriptsToProcess},

    [AllowEmptyCollection()]
    [System.Object[]]
    ${ModuleList},

    [AllowNull()]
    [System.Object]
    ${PrivateData},

    [Switch]
    ${PassThru}
    )

    process {
        $types = Get-ChildItem $originalPath -Filter *.types.ps1xml
        $formats = Get-ChildItem $originalPath -Filter *.format.ps1xml
        $dlls = Get-ChildItem $originalPath -Filter *.ps*.dll
        $null = $psBoundParameters.Remove("OriginalPath")
        $psBoundParameters += @{
            VariablesToExport = "*"
            FunctionsToExport = "*"
            AliasesToExport = "*"
            CmdletsToExport = "*"
            FileList = @()
            RequiredAssemblies = @()
            ModuleToProcess = ""
            NestedModules = @($dlls | Select-Object -ExpandProperty FullName)
            TypesToProcess = @($types | Select-Object -ExpandProperty FullName)
            FormatsToProcess = @($formats | Select-Object -ExpandProperty FullName)
        }
        New-ModuleManifest @psBoundParameters
    }
}

此块将显示通过指向工具@ SQL创建清单。

$basePath = $env:SqlSamplesSourceDataPath | Split-Path
if (${env:ProgramFiles(x86)}) {
    $basePath = $basePath.Replace($env:ProgramFiles, ${env:ProgramFiles(x86)})
} 

$path = Join-Path $basePath "Binn"


$basicMetaData = @{
    Author = "Microsoft Corporation"
    Description = "A Manifest to enable using the SQL PowerShell snapin in V2"
    CompanyName = "Microsoft Corporation"
    Copyright = (Get-Date).Year 
}
New-ModuleManifestFromSnapin -Path $psScriptRoot\Sql.psd1 -OriginalPath $path @BasicMetaData

最终的块会将名为当前文化的任何文件(即SQL \ en-us)复制到模块目录中,这将使得Get-Help适用于cmdlet。

这个技巧适用于一些管理单元,但可能需要为每个要作为模块重新公开的管理单元进行一些自定义。幸运的是,这是一次性成本。

$Culture = Get-Culture
$CultureList = "$path\$culture", 
    (Join-Path $path $culture.ThreeLetterIsoLanguageName), 
    (Join-Path $path $culture.TwoLetterIsoLanguageName)

$CultureDirectory = Get-Item $CultureList -ErrorAction SilentlyContinue |
    Select-Object -First 1 

if ($CultureDirectory) {
    $localDir = Join-Path $psScriptRoot (Split-Path $CultureDirectory -Leaf)
    $item = Get-Item $localDir -ErrorAction SilentlyContinue 
    if ($item) {
        Remove-Item $item -Recurse -Force 
    }
    Copy-Item $CultureDirectory $LocalDir -Recurse
}

希望这有帮助

答案 1 :(得分:1)

带有Get-Module参数的-ListAvailable cmdlet效果很好。问题是架构,即32位PowerShell主机无法列出64位模块...