问题标签 [powershell-module]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
40043 浏览

powershell - 为所有用户持久安装 PowerShell 模块

我正在通过Octopus Deploy将 PowerShell 模块安装到许多不同的服务器上。出于测试目的,我参考了 Microsoft安装 PowerShell Modules的文档。

这工作正常,但正如文档所述,我的更改仅对当前会话可见。也就是说,如果我要执行以下操作:

在 tentacle 服务器上自动运行此安装程序时,未来的 PowerShell 会话似乎看不到新安装的模块。

如何以与配置文件无关的方式安装 PowerShell 模块,以便启动的每个 PowerShell 会话都可以看到它?

0 投票
4 回答
12602 浏览

powershell - 将 -Verbose 状态传递给模块 cmdlet

我有一个 PowerShell 模块,它封装了许多常用的业务功能。通常不会从控制台调用它;相反,它的功能由导入模块的自动部署和管理脚本调用。

该模块包含一个日志功能,可写入两个集中的日志位置。我还想加入Write-Verbose功能以写入控制台。

我的限制是 - 从 Corporate PowerShell 模块中 - 我需要确定是否使用-Verbose参数调用了 Script.ps1。理想情况下,我希望确定代码完全在模块本身内。

这是一个例子:

将以上内容另存为test.ps1。从控制台调用时:

如您所见,$VerbosePreference 变量在模块中不可用。有没有办法从模块中获取调用脚本是否已使用-Verbose标志调用?

0 投票
1 回答
8863 浏览

function - PowerShell 模块设计 - Export-ModuleMember

我正在构建一个模块,用于导出我想通过我的配置文件提供的 cmdlet。此 cmdlet 的实现分布在多个实现文件中,这些文件包含我不想公开的实现功能。所以我使用Export-ModuleMember来隐藏它们。

文件get_something.psm1

然后我将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 模块的正常方面吗?

0 投票
1 回答
286 浏览

powershell - PSModulePath PowerShell 4.0 中的相对路径引用

以下技术已成功用于 PowerShell v2 和 3。

$env:PSModulePath += ';.\Modules'

这允许模块文件夹与脚本一起分发,而无需向最终用户解释如何安装 PowerShell 模块。从 PowerShell v4 开始,它不再有效。运行Get-Module -ListAvailable不会显示存储在那里的模块。我无法在发行说明中找到有关此更改的任何内容。PowerShell v4 中是否有新的相对引用语法?它是一个错误吗?还是有意的?

0 投票
1 回答
1020 浏览

powershell - Pass a Full Path Through Different Functions in PS Modules

What is the best way to declare full paths in Powershell? I am writing a module that needs to pass full file path variables (ex: $Backup = C:\backups\file1.bak) through multiple psm1 files and is called remotely.

Right now, I need to pass a fully qualified path to Powershell and have it do a lot of different things with it. It seems to work fine when it's all in 1 functional script, otherwise, the path doesn't actually go through to other functions written in other PSM1 files, but all other variables do. It seems to just output a null value.

First I have a FileValidator script which among other things, will find the latest filename in the path, and then concat the latest filename, with the path. Perhaps I need to use joinpath somewhere to fix this is. The code to get the fully qualified path that isn't exporting to other PSM1 files is:

As you can see, I was testing the output of $FQNBackup in various places but it just made things worse. I'm not sure how to define this in the modules or how to cast/convert it. After declaring the $FQNBackup variable as listed above, I then modify it by trying to do this as found else where on stackoverflow earlier:

I've tried converting it to a string as well but I might be doing it wrong. It seems like I should be able to:

1- Get the path

2- Convert to string

3- Pass the value as a string to other modules.

However is this 'bad' from an architecture design view? Should I just keep it the same object type instead as it jumps from different psm's? That is the real question.

0 投票
2 回答
3745 浏览

powershell - 计划任务中的 Powershell 模块

有一些关于模块的东西我不太明白....

如果我作为普通用户

我得到这样的结果:

这就是我所期望的......

但是,当从计划任务(与不同的用户)做同样的事情时,我得到了这个:

为什么这两者有区别?

我很困惑,因为我实际上感兴趣的模块是我自己创建并放入文件夹的模块:

除了通过计划任务运行外,这似乎工作正常。

我错过了什么?我忘记了什么?

此外 - 我可以确认两者$env:PSModulePath都是相同的:

当然除了用户路径。

0 投票
1 回答
358 浏览

powershell - 从 Powershell 函数中捕获详细信息

我正在尝试从另一个 Powershell 函数调用一个 Powershell 函数。我正在调用的函数有很多详细的输出,我需要将它们捕获到一个文件中。由于我多次调用所述函数,我需要将详细输出附加到文件末尾。

我尝试了以下

但是,代码没有按预期工作并不断重复

该功能是源代码控制的(因此,我无法触摸该功能。)。这些函数也是PSM1文件的一部分。Teardown.log 文件在执行上述代码之前有一些条目。

0 投票
1 回答
648 浏览

c# - 获取二进制 powershell 模块中的工作目录

我正在用 C# 编写一个二进制 powershell 模块,但我似乎无法从命令中找到当前工作目录。

Directory.GetCurrentDirectory();我的第一个想法是从库中做一个简单的事情,System.IO但它似乎只给了我启动 powershell 的目录。

如何在命令执行期间获取二进制 powershell 模块中的当前目录?

谢谢

0 投票
1 回答
145 浏览

powershell - Powershell 模块在不受支持的主机上中止加载

词汇表:

  • 主机:PowershellHost 会话
  • 交互的:[Environment]::UserInteractive -eq $True

设想:

创建一个 powershell 模块,它只会在失败的情况下正确中止并且不会出错。在这种情况下,某些命令/模块只能在完全交互式主机(如 ISE 和控制台)中正常工作,但不能在像 NuGet 包管理器控制台这样的假交互式主机中正常工作

失败解决方案:

失败解决方案:

失败解决方案:

失败解决方案:

失败解决方案:

烦人的解决方案:

不是解决方案:

有问题的解决方案:

将模块拆分为嵌套子模块,一个用于“通用”,一个用于每个受支持的主机。为每个使用子文件夹,并为每个复制 psd1。看起来它最终将成为可维护性的噩梦,尤其是在嵌套依赖项方面。

有没有更好的方法来做到这一点,或者超级模块拆分是唯一的方法?

0 投票
1 回答
84 浏览

c# - 二进制 powershell 模块命令格式

我正在编写一个二进制 powershell 模块,它将替换另一个只输出文本的命令行工具。

我有一些遵循 powershell 动词指南的命令(例如Add-MyModuleUser -UserName "My Username"),但我还想有一个重载命令,它接受这些对象的响应并将它们格式化,就像我要替换的工具一样,它接受以下格式的输入:

我的直觉告诉我有一个Invoke-MyModule使用别名 to调用的命令myModule,所以我可以调用Invoke-MyModule addUserName "My Username"which 将调用Add-MyModuleUser和格式化输出,就像原始工具一样。

问题:如何让 powershell 命令接受这样的输入?

注意:我正在阅读看起来很有希望的 ParameterSets,我想我可以将参数集声明为字符串,因此字符串中的第一个单词为输入的其余参数设置 ParameterSet。

谢谢!