是否可以通过定义新模块清单并将其放入指定目录来注册新的PowerShell模块?
E.g。应创建一个名为SampleModule
的新模块。我将空清单文件SampleModule.psd1
放在目录<%PSModulePath%>\SampleModule
中。 (使用哪个(用户或全局)模块路径无关紧要。)
这足以让PowerShell使用Get-Module -ListAvailable
命令列出我的新模块。
在下一步中,我尝试填充清单并将ModuleToProcess
属性设置为程序集,该程序集位于另一个目录中。调用Import-Module
失败,PowerShell无法找到程序集。
答案 0 :(得分:1)
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位模块...