我正在编写自定义PowerShell cmdlet,我想知道哪种方法可以验证参数.
我认为这可以在属性集访问器中或在Cmdlet执行期间完成:
[Cmdlet(VerbsCommon.Add,"X")]
public class AddX : Cmdlet {
private string _name;
[Parameter(
Mandatory=false,
HelpMessage="The name of the X")]
public string name {
get {return _name;}
set {
// Should the parameter be validated in the set accessor?
if (_name.Contains(" ")) {
// call ThrowTerminatingError
}
_name = value;
}
}
protected override void ProcessRecord() {
// or in the ProcessRecord method?
if (_name.Contains(" ")) {
// call ThrowTerminatingError
}
}
}
Run Code Online (Sandbox Code Playgroud)
哪种是"标准"方法?属性设置器,ProcessRecord还是完全不同的东西?
我对powershell很新.我已经构建了一个自定义cmdlet.我已经在powershell中注册了它,但我想在开始使用cmdlet之前测试它.所以我在我的解决方案中添加了一个简单的testapp.我正在尝试调用我的自定义cmdlet,如下所示:
var deploy = new DeployCommand();
deploy.BranchDir = @"";
deploy.DevDir = @"d:\sandbox\testdeploy";
deploy.Invoke();
Run Code Online (Sandbox Code Playgroud)
我在我的cmdlet中设置了一个断点,当我执行.invoke时,它从不做任何事情.我在cmdlet中覆盖的唯一方法是"ProcessRecord",但是当我调用invoke时,它从不做任何事情.我确信这很简单.有谁知道我做错了什么?
如果$文件名存在,那么该cmdlet相当于[System.IO.Path]::GetFullPath($fileName);是(Get-Item $fileName).FullName.但是,如果路径不存在,则抛出异常.他们的另一个cmdlet我不见了?
Join-Path 是不可接受的,因为它在传递绝对路径时不起作用:
C:\Users\zippy\Documents\deleteme> join-path $pwd 'c:\config.sys'
C:\Users\zippy\Documents\deleteme\c:\config.sys
C:\Users\zippy\Documents\deleteme>
Run Code Online (Sandbox Code Playgroud) 我创建了一个类库,它应该连接到我托管的WCF端点项目.客户端项目定义了应该与服务交互的命令行开关.
但是,我不断收到以下错误:
Could not find default endpoint element that references contract Service1.MyService in the
ServiceModel client configuration section. This might be because no configuration file was found
for your application, or because no endpoint element matching this contract could be found in the
client element.
Run Code Online (Sandbox Code Playgroud)
你知道问题可能是什么吗?
编辑 我所拥有的是定义cmdlet的单个类库.我使用.psd1文件到Import-Module,它使用生成的dll文件.
EDIT2 再一次,我没有一个引用我的库的项目.powershell调用定义的命令行开关,这些cmdlet应连接到WCF端点
谢谢
我是Powershell的新手,我正在使用Get-Hash cmdlet:
这有效:
PS M:\> Get-Hash -Path "C:\Users\medmondson\Desktop\New Folder\database.adp" -Algorithm SHA512
Run Code Online (Sandbox Code Playgroud)
这失败了:
PS M:\> Get-Hash -Path "C:\Users\medmondson\Desktop\New [Folder]\database.adp" -Algorithm SHA512
Get-Hash : Cannot bind argument to parameter 'Path' because it is an empty array.
Run Code Online (Sandbox Code Playgroud)
我理解这可能是由于路径中的方括号在[]Windows操作系统下有效.那么如何让PowerShell逃脱这些呢?
我正在尝试在此处设置 NuGet Feed,效果很好。我通过我的提要安装了一个模块
Install-Module -Name MyCmdlets -Repository $RepoName -Scope CurrentUser -Force
Import-Module -Name MyCmdlets
Run Code Online (Sandbox Code Playgroud)
但是,当我运行 Get-Module 时,我没有任何功能,它是一个清单?
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 1.0 MyCmdlets
Run Code Online (Sandbox Code Playgroud)
如果我手动转到安装位置并手动导入
Import-Module <my-path>\1.0\MyCmdlets.psm1
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 0.0 MyCmdlets {Create-Project, Get-AuditLogs, Get-..
Run Code Online (Sandbox Code Playgroud)
我的清单文件确实有这些行,所以我不明白为什么Import-Module不能正常工作。
FunctionsToExport = '*'
CmdletsToExport = '*'
编写PowerShell脚本时,有两个选项可用:
在后台,cmdlet可能更多地使用.NET库.
性能明智的是,在Powershell脚本中哪一个更好用?
我倾向于更喜欢直接使用.NET库,因为它更接近C#.
我真的处于使用命令行开关和 powershell 东西的初学者级别。我正在使用 PowerShell API 从 C# 调用命令行开关。我看到了奇怪的行为。虽然在 stackoverfow 的不同线程上,人们使用 Import-Command 或 PSImportModule 方法显式导入模块,但我可以查看 $env:PSModulePath 中是否有可用的程序集,它会自动加载。这种行为是默认情况下还是由于我忽略的标准配置。我在 ms 测试环境中运行单元测试。
我正在使用以下代码。
System.Management.Automation.PowerShell _powerShellInstance
_powerShellInstance.AddCommand("Connect-AzureRmAccount", false);
var output = _powerShellInstance.Invoke();
// Invoking my commandlets
_powerShellInstance.AddCommand("Get-LinkParameter", false);
Run Code Online (Sandbox Code Playgroud)
上面的命令会自动从C:\Windows\system32\WindowsPowerShellModules\v1.0\Modules\. 我没有创建任何运行空间,也没有配置集。就在上面自动加载东西。我需要确认 powershell 和运行空间的行为究竟如何。因为我需要清楚我需要如何在生产机器上安装我的命令行开关。生产机器上的单元测试如何访问我的命令行开关以完美加载。
我正在使用 PowerShell 5.1,我试图确定为什么Write-Information消息不会显示在由创建的脚本日志中,Start-Transcript除非我设置$InformationPreference为SilentlyContinue. 我想在控制台中显示消息并将它们写入日志文件。
我在这里看了:https : //docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-5.1#informationpreference
然后我决定创建这个脚本来测试写入的内容和时间。请参阅下方的首选项部分Testing explicit behavior with transcripts -------------
Clear-Host
$ErrorActionPreference = "Stop"
try {
Write-Host "Starting transcript"
Start-Transcript -Force -Path "$PSScriptRoot\default.txt"
<#
In PowerShell 5.1 the default behavior is as follows:
$DebugPreference = SilentlyContinue
$InformationPreference = SilentlyContinue
$ProgressPreference = Continue
$VerbosePreference = SilentlyContinue
See the following for more information:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-5.1
#>
# I am not testing Write-Output as I am not worried about …Run Code Online (Sandbox Code Playgroud) 全部,原谅我,我是新手Power Shell,目前我正在读书Windows PowerShell CookBook试着开始吧.到目前为止,一切意义,我除了一两件事,我是用完全糊涂Positional Parameter到Cmdlet.
例如: Select-String
语法如下:
Select-String [-Pattern] <String[]> [-Path] <String[]> [-AllMatches] [-CaseSensitive] [-Context <Int32[]>]
[-Encoding <String>] [-Exclude <String[]>] [-Include <String[]>] [-List] [-NotMatch] [-Quiet] [-SimpleMatch]
[<CommonParameters>]
Select-String [-Pattern] <String[]> [-AllMatches] [-CaseSensitive] [-Context <Int32[]>] [-Encoding <String>]
[-Exclude <String[]>] [-Include <String[]>] [-List] [-NotMatch] [-Quiet] [-SimpleMatch] -InputObject <PSObject>
[<CommonParameters>]
Select-String [-Pattern] <String[]> [-AllMatches] [-CaseSensitive] [-Context <Int32[]>] [-Encoding <String>]
[-Exclude <String[]>] [-Include <String[]>] [-List] [-NotMatch] [-Quiet] [-SimpleMatch] -LiteralPath <String[]>
[<CommonParameters>]
Run Code Online (Sandbox Code Playgroud)
我可以通过Cmdlet忽略参数名称直接传递参数值.如下:
"Hello …Run Code Online (Sandbox Code Playgroud) cmdlets ×10
powershell ×10
c# ×3
.net ×2
logging ×1
module ×1
runspace ×1
validation ×1
wcf ×1