在Powershell cmdlet中公开一组相关函数时,是否可以共享属性名称和摘要帮助以在程序集中跨cmdlet规范化这些函数?
我知道这可以通过派生类来完成,但是当有多个具有不同属性的cmdlet要共享时,此解决方案最多是尴尬的.
这是一个非常简单的例子.我想分享属性'Name'和所有相关的注释,以便它们在我们生成的N个cmdlet中是相同的,但我想不出在c#中执行此操作的好方法.理想情况下,任何共享都允许指定参数属性,例如Mandatory或Position.
namespace FrozCmdlets
{
using System.Management.Automation;
/// <summary>
/// Adds a new froz to the system.
/// </summary>
[Cmdlet( VerbsCommon.Add, "Froz" )]
public class AddFroz : Cmdlet
{
/// <summary>
/// The name of the froz.
/// For more information on the froz, see froz help manual.
/// </summary>
[Parameter]
public string Name { get; set; }
protected override void ProcessRecord()
{
base.ProcessRecord();
// Add the froz here
}
}
/// <summary>
/// Removes a froz from …Run Code Online (Sandbox Code Playgroud) 我在模块中定义了枚举类型.如何在模块加载后将其导出为可从外部访问?
enum fruits {
apple
pie
}
function new-fruit {
Param(
[fruits]$myfruit
)
write-host $myfruit
}
Run Code Online (Sandbox Code Playgroud)
我的高级函数使用枚举而不是ValidateSet枚举可用的枚举,但如果不可用则失败.
更新: 将它分成ps1并点源(ScriptsToProcess),但我希望有一种更清洁的方式.
任何人都可以帮我将C#.NET程序转换为PowerShell cmdlet吗?我对这个领域很新.请帮我走出这个检查站!
问候,
阿伦
我用的时候
Import-Module -Name <path_to_local_dll> -Verbose
Run Code Online (Sandbox Code Playgroud)
DLL文件中包含的cmdlet不会导出.
因此,当我键入Get-Module我导入的模块时,但没有任何ExportedCommands.为什么?
ModuleType Name ExportedCommands
---------- ---- ----------------
Binary MyModule
Run Code Online (Sandbox Code Playgroud)
在具有相同软件(PowerShell,.NET Framework,...)的第二台PC上,相同的导入DLL文件可以正常工作.在那里我得到了ExportedCommands.
这种行为取决于什么?
不幸的是,Import-Modulecmdlet没有表明它无法导入cmdlet.有没有办法得到它失败的原因?
我喜欢编写一个cmdlet"Convert-ToHashTable",它执行以下任务:
$HashTable = Import-Csv Table.csv | Convert-ToHashTable
Run Code Online (Sandbox Code Playgroud)
Import-csv在管道上放置一个数组,如何将其更改为Convert-ToHashTable cmdlet中的哈希表?在cmdlet的Process部分中,我可以访问元素,但我不知道如何更改管道本身的类型
进程{Write-Verbose"Process $($ myinvocation.mycommand)"$ CurrentInput = $ _ ...}
有没有办法将完整的哈希表作为新管道返回或创建一个带有类型哈希表的新管道?
我正在尝试使用Jenkins和ServiceFabric PowerShell扩展自动部署到Azure Service Fabric.由于缺乏对部署过程的控制和灵活性,Jenkins ServiceFabric插件在我的案例中不是一个好选择.我遇到了以下问题 - Jenkins无法识别SF PowerShell cmdlet
Connect-ServiceFabricCluster:术语"Connect-ServiceFabricCluster"未被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试
ServiceFabric设置是正确的,因为当我从PowerShell本地运行脚本时,tt就像一个魅力.所以,我试图在本地运行Jenkins,而不是像在互联网上的不同帖子中所建议的服务模式,但这还没有解决问题.
我试过的其他事情:
我仍然收到相同的结果.
所以,我尝试使用ServiceFabric Python Cli作为替代方案,但面临另一个问题 - 它在带有证书的"sfctl cluster select"上返回"Bad SSL handshake",它在本地与PS ServiceFabric cmdlet一起使用
有任何想法吗?
我正在 powershell 上编写一个 cmdlet(脚本),我想使用 eunm 作为参数之一。但我不知道将枚举定义放在哪里,以便它对 cmdlet 参数声明可见。
例如,我有一个像这样的脚本的参数定义
[cmdletbinding()]
param(
[Parameter(Mandatory=$True)]
[string]$Level
)
Run Code Online (Sandbox Code Playgroud)
和这样的枚举
enum LevelEnum { NC = 1; NML = 2; CS = 3 }
Run Code Online (Sandbox Code Playgroud)
我无法替换参数定义中的[string]with [LevelEnum],因为脚本将无法找到枚举定义。而且我之前不能放定义cmdletbinding,这是不允许的。如果那是一个函数,我知道该怎么做,我知道它可以使用 解决ValidateSet,但我需要有与枚举选项相关的整数值。
[ValidateSet('NC','NML','CS')]
Run Code Online (Sandbox Code Playgroud)
但问题是,我可以对 cmdlet 做同样的事情吗?
谢谢大家。我最终得到了不同答案的组合。
[cmdletbinding()]
param(
[Parameter(Mandatory=$True)]
[ValidateSet('NC','NML','CS')]
[string]$Level
)
# Convert level from string to enum
enum PatchLevel { NC = 1; NML = 2; CS = 3 }
[PatchLevel]$l = $Level
# Use the numeric value
Write-Host $l.value__
Run Code Online (Sandbox Code Playgroud) 我正在用 C# 为 PowerShell 编写一个 Cmdlet。我正在继承 aCmdlet而不是 a PSCmdlet。
有没有办法从 PowerShell 获取当前目录?我可以PSCmdlet使用 using这样做GetVariableValue("pwd")。但是在 Cmd 类中我没有那个可用。
Environment.CurrentDiretory 将我指向 powershell 启动的路径,而不是 PowerShell 本身当前所在的位置。
编辑
例子:
我通过 - say - 启动powershell powershell_ise.exe。它在C:\Windows\System32\WindowsPowerShell\v1.0. 然后我使用更改路径cd c:\my\folder并运行我的命令Do-Something。在“Do-Something”(C# 端)的实现中,我希望能够检索当前路径 => c:\my\folder。
如果可能,我想避免使用PSCmdlet.
我试图编写一个简单的PS脚本来检查大型.txt日志文件中的短字符串:“ SRVE0242I:”
$lines = Select-String -Path $logDir -Pattern "SRVE0242I:" | Select-Object line | Out-String
Run Code Online (Sandbox Code Playgroud)
但是在输出时,它仅显示以下内容:
行
[28/06/17 13:48:27:839] 00000020 ServletWrappe I SRVE0242I:[User] [User] [com_xxxxxxx _...
而不是全线。提取的字符数是否有限制?我找不到有关Select-String cmdlet的任何限制的任何信息。有没有更好的方法可以做到这一点,所以我不会a)在行列表中拉标题“ Line”(不想真的为这种简单的输出创建表格格式),b)获得整行当我提取信息时?
我有一个查询,例如当我尝试执行包含带空格的路径的表达式时,出现如下错误。
代码:
$path="E:\Test\My space\Log"
Invoke-Expression $path
E:\Test\My: The term 'E:\test\My' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ E:\Test\My space\Log
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (E:\Test\My :String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)
你能帮我解决这个问题吗?