2010-01-22 15 views
1

是否有可能只是通過定義一個新的模塊清單,並把它在指定的目錄中註冊一個新的PowerShell模塊?PowerShell的清單引用外部組件

E.g.應該創建一個名爲SampleModule的新模塊。我將空清單文件SampleModule.psd1放在目錄<%PSModulePath%>\SampleModule中。 (使用哪個(用戶或全局)模塊路徑無關緊要)。

這足以讓PowerShell來列出我與Get-Module -ListAvailable命令新的模塊。

在下一步我試着填寫清單和ModuleToProcess屬性設置爲一個組件,它位於另一個目錄。調用Import-Module失敗,PowerShell無法找到程序集。

回答

1

Get-Module cmdlet與-ListAvailable參數工作良好。問題是架構,即32位PowerShell主機無法列出64位模塊...

1

確實如此。事實上,我有一個非常好的技巧,可以爲SQL管理單元做到這一點。代碼的第一部分是一個函數,它將從模塊位置之一爲您創建模塊清單。它仍然會提示您(如New-ModuleManifest cmdlet所做的那樣)以獲取一些必需的參數,如描述。它發現任何類型和格式的文件,以及任何.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)到模塊目錄中的任何文件,這將使獲取幫助工作的cmdlet。

這招已經工作了幾年snapins,但可能需要一些自定義的每個管理單元要重新公開爲模塊。幸運的是,這是一次性成本。

$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 
} 

希望這有助於