PowerShell的表现引用外部组件
-
22-09-2019 - |
题
是否有可能注册一个新的PowerShell模块只要定义清单的新的模块,并把它在指定的目录?
E.g。一个新的模块名为SampleModule
应创建。我把空清单文件SampleModule.psd1
目录<%PSModulePath%>\SampleModule
。 (这并不重要(用户或全局)模块路径时)。
这是足够的PowerShell列出我的新模块与Get-Module -ListAvailable
命令。
在下一步我尝试以填充清单和ModuleToProcess
属性设置为一个组件,它位于另一个目录。主叫Import-Module
失败,PowerShell中找不到组件。
解决方案 2
与Get-Module
参数的-ListAvailable
小命令工作良好。问题是该架构,即32位PowerShell主机无法列出64位模块...
其他提示
有确实。其实,我有一个非常漂亮的把戏,这是否为SQL管理单元。代码的第一块是从的模块位置之一创建一个模块清单给你的函数。它仍然会提示你(作为New-ModuleManifest将cmdlet的一样)对于一些需要的参数,如描述。它发现任何类型和格式的文件,以及任何.dll文件的命名的 .PS 的.dll文件(最snapins遵守这个约定),然后创建一个模块清单,它包装的原始模块。代码的第二组块执行此specificially为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
}
希望这有助于