我正在创建一个模块,该模块依赖于我需要加载到全局环境中的其他几个模块.我尝试创建一个脚本并使用ScriptsToProcess导入模块,但看起来在ScriptstoProcess运行之前检查RequiredModules.
在模块清单中是否有一个很好的,干净的方法来同时需要一个模块并在它尚未加载时自动加载它?如果无法加载模块,则RequiredModule将抛出错误.
目前我们正在使用下面的命令安装一些模块,但它在C:\Program Files\WindowsPowerShell\Modules.
Install-Module -Name XXX -RequiredVersion XXX -Repository XXX -Scope AllUsers
Run Code Online (Sandbox Code Playgroud)
我们的要求是在E:\Modules文件夹中安装这个模块。为此,我更新了PSModulePath环境变量,如下所示。( https://msdn.microsoft.com/en-us/library/dd878350(v=vs.85).aspx )
$p = [Environment]::GetEnvironmentVariable("PSModulePath")
$p += ";E:\Modules"
[Environment]::SetEnvironmentVariable("PSModulePath",$p)
Run Code Online (Sandbox Code Playgroud)
但它仍然安装在C:\Program Files\WindowsPowerShell\Modules.
如何更新PSModulePath到E:\Modules安装模块之前?
我正在构建一个模块,用于导出我想通过我的个人资料提供的cmdlet.此cmdlet的实现分布在多个实现文件中,这些文件包含我不想公开的实现函数.所以我使用Export-ModuleMember来隐藏它们.
import-module .\get_something_impl.psm1
function Get-Something {
[cmdletbinding()]
Get-SomethingImplementation
}
Export-ModuleMember -Function Get-Something
Run Code Online (Sandbox Code Playgroud)
然后我将get_something.psm1添加到我的个人资料中.通过仅导出Get-Something,我的所有实现功能都保持"私有".
我遇到的问题是,当使用Export-ModuleMember命令时,每次我需要一个函数时,我必须在我的实现文件中导入一个模块.例如,假设我有一个模块person.psm1,其中包含我需要在所有实现文件中调用的函数Get-Person.现在我必须在每个需要调用Get-Person的文件中导入person.psm1.这是使用Export-ModuleMember -Function Get-Something的结果.没有它,我只需要导入person.psm1一次,它就可用了.
实质上,Export-ModuleMember不仅阻止我的实现到外部,它还阻止了我自己的实现.
这是预期的,并被认为是设计Powershell模块的正常方面吗?
我有一组实用函数和其他代码,我将它们点源到我编写的每个 powershell 文件中。在被调用者作用域变量影响后,我开始考虑将其更改为 powershell 模块。
我在其中做的一些特殊事情遇到了问题,实际上我确实希望在范围之间进行一些交互。我想知道是否有任何方式“进入”模块调用者的范围以在移动到 powershell 模块时保持此功能?
如果没有,我将这些更专业的东西保存在一个点源文件中并将更传统的实用程序功能移动到一个模块中是我最好的方法吗?以下是不容易移至模块的内容:
设置严格模式和错误操作首选项以保持理智,例如:
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$PSDefaultParameterValues['*:ErrorAction']='Stop'
Run Code Online (Sandbox Code Playgroud)
当代码从 .psm1 powershell 模块运行时,这(如预期)对调用者的环境没有影响。有没有办法从 psm1 范围跨越到调用者范围来进行这些更改?
打印出有关顶级脚本调用的信息,例如:
$immediateCallerPath = Get-Variable -scope 1 -name PSCommandPath -ValueOnly
Write-Host "Starting script at $immediateCallerPath"
$boundParameters = Get-Variable -scope 1 -name PSBoundParameters -ValueOnly
Write-Host "Bound parameters are:"
foreach($psbp in $boundParameters.GetEnumerator())
{
"({0},{1})" -f $psbp.Key,$psbp.Value | Write-Host
}
Run Code Online (Sandbox Code Playgroud)
同样,这些命令一旦放置在 .psm1 文件中就无法再看到最顶层的调用范围
问题描述
当我在 Visual Studio Code 1.58.2 中的模块 SampleModule.psm1 上选择“运行而不调试”时
function ConvertTo-PascalCase([String []] $words) {
[String] $pascalCaseString = [String]::Empty
foreach ($word in $words) {
$pascalCaseString = $pascalCaseString + ($word.Substring(0,1).ToUpper() + $word.Substring(1))
}
$pascalCaseString = ($pascalCaseString.TrimEnd(',')).Trim()
return $pascalCaseString
}
Export-ModuleMember -Function Convert-ToPascalCase
Run Code Online (Sandbox Code Playgroud)
我不断收到错误消息:
InvalidOperation:无法在管道中间运行文档
屏幕截图更详细地显示了它。每当我构建任何模块时,我都会收到同样的错误消息
我尝试过但不起作用的事情:
我如何知道这是运行 PSM1 文件的问题
如果我将 .psm1 模块文件转换回普通的旧 Powershell 脚本 (*.ps1) 脚本并删除所有Export-Module 成员命令,则脚本运行得很好。
我的工作
要在 VSCode 中执行该模块,我必须使用 dotsource 才能成功导入该模块: …
我有一个很长的PowerShell代码,我在一个长期项目的过程中写的; 这些脚本执行各种各样的功能,并且大多数功能在某种程度上依赖于项目范围内的其他功能.现在,这项工作由几个文件组成,每个文件包含许多功能.最初,为了使用这些脚本,所有脚本文件都是偶然的点源到环境中.
但是,我最近了解到Powershell 2.0引入了模块,我想以这种方式将这些脚本一起部署.由于模块的内容全部加载在一起,我想拆分我的文件,以便每个脚本都有自己的文件,以帮助控制源.但是,我现在对脚本之间的联系还不清楚.
我做了一些测试,似乎可以将每个函数的Export-ModuleMember命令移动到各个.ps1文件; 这似乎更像是在C#中声明自己的范围,如公共和私有作用域的函数.但是,在执行此操作后,我的.psm1文件只包含此内容:
Get-ChildItem -recurse $psScriptRoot | where { $_.Extension -eq ".ps1" } | foreach { . $_.FullName }
Run Code Online (Sandbox Code Playgroud)
那似乎对吗?所有脚本都是点源的,并且所有脚本在该假设下相互引用.他们应该使用相对于$ psScriptRoot的位置来互相引用吗?
有没有办法与这两种方式不同?有人可以提供建议吗?我对这些还不太了解.
考虑以下模块脚本:
MyWebApp.psm1:#Requires -Version 4
#Requires -Modules WebAdministration
function Test-MyWebApp() {
return ((Get-WebApplication 'myapp') -ne $null)
}
Run Code Online (Sandbox Code Playgroud)
(Export-ModuleMember为简单起见省略.脚本仍然可以作为没有它的模块.)
如果这是一个ps1脚本,#Requires顶部的注释会强制PowerShell抛出错误
但是,如果我尝试使用这个导入Import-Module,这些会产生什么影响吗?该#Requires文件只是说"剧本",但它没有说明脚本模块是否算作"脚本"或不在这里.如果不是,我该怎么办呢?
我创建了一个名为 ODBCManager 的二进制 Powershell 模块。从提要安装后,我能够成功使用它的功能,但Get-Module无法按名称找到它(Get-Module -Name ODBCManager返回 null)。Get-Module -ListAvailable查询2分钟后就会显示出来,所以肯定已经安装了。安装时也-Verbose说成功。
C:\Users\xxxxx> Get-Module -ListAvailable
Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0.1 Microsoft.PowerShell.Operation.V... {Get-OperationValidation, Invoke-OperationValidation}
Binary 0.1.0.1 ODBCManager {Add-OracleODBC, Get-ODBCDrivers, New-OracleODBC}
Binary 1.0.0.1 PackageManagement {Find-Package, Get-Package, Get-PackageProvider, Get-PackageSource...}
Script 3.4.0 Pester {Describe, Context, It, Should...}
Script 1.0.0.1 PowerShellGet {Install-Module, Find-Module, Save-Module, Update-Module...}
Script 1.2 PSReadline {Get-PSReadlineKeyHandler, Set-PSReadlineKeyHandler, Remove-PSReadlineKeyHandler, Get-PSReadlineOption...}
Manifest 20.0 SqlServer {Add-SqlColumnEncryptionKeyValue, Complete-SqlColumnMasterKeyRotation, Get-SqlColumnEncryptionKey, Get-SqlColumnMasterKey...}
Directory: C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
ModuleType …Run Code Online (Sandbox Code Playgroud) > nuget
nuget : The term 'nuget' is not recognized as the name of a cmdlet, function, script file, or operable program. ...
> find-package -name nuget | Install-package -verbose
VERBOSE: Skipping installed package NuGet 1.3.3.
> nuget
nuget : The term 'nuget' is not recognized as the name of a cmdlet, function, script file, or operable program. ...
Run Code Online (Sandbox Code Playgroud)
如果已安装,它在哪里安装?我搜索了整个驱动器,没有其他副本nuget.exe存在,除了我手动安装的那个(但显然不在$ PATH中,所以肯定不是指那个 - 我将它重命名为仔细检查!).
我对Java经历有所期待,似乎我在问错误的问题.
我的用例如下.背景首先.
dotnetcli一起使用.MSBuild和使用.targets文件 …据我所知,PowerShell 存储库是 NuGet 存储库...
GitHub 刚刚发布了他们的包注册表,我的公司目前将其用于 npm,但也有一个用于 NuGet 的端点。
我可以使用我的 GitHub 凭据访问 NuGet 端点 ( https://nuget.pkg.github.com/mycompany/index.json ),它返回一个有效的 json:
{
"version": "3.0.0-beta.1",
"resources": [
{
"@id": "https://nuget.pkg.github.com/mycompany/download",
"@type": "PackageBaseAddress/3.0.0",
"comment": "Get package content (.nupkg)."
},
{
"@id": "https://nuget.pkg.github.com/mycompany/query",
"@type": "SearchQueryService",
"comment": "Filter and search for packages by keyword."
},
{
"@id": "https://nuget.pkg.github.com/mycompany/query",
"@type": "SearchQueryService/3.0.0-beta",
"comment": "Filter and search for packages by keyword."
},
{
"@id": "https://nuget.pkg.github.com/mycompany/query",
"@type": "SearchQueryService/3.0.0-rc",
"comment": "Filter and search for packages by keyword."
}, …Run Code Online (Sandbox Code Playgroud)