增强版本的HelloWorld,通过传递一族人民到HellowWorld.
一下只写明增强版本的cmdlet程序,注册的snapin没有变化。
HelloWorld.cs
在PS下通过 get-helloworld,一个接一个输入所有名称,回车表示输入结束。
或者通过get-helloworld ("William wang","Peter Zhang","李华") 来直接得到结果.
using System;
using System.Management.Automation;
[Cmdlet(VerbsCommon.Get, "HelloWorld", SupportsShouldProcess = true)]
public class HelloWorld : PSCmdlet
{
// An array is used to support the following scenario:
[Parameter(
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
Mandatory = true,
HelpMessage = "恭贺的名字清单")]
[ValidateNotNullOrEmpty]
public string[] FullName
{
get { return fullName; }
set {fullName = value; }
}
private string[] fullName;
// Called when cmdlet is invoked.
protected override void BeginProcessing()
{
// You could open a database connection here for example.
base.BeginProcessing();
}
// Called when we have pipeline input.
protected override void ProcessRecord()
{
// this.Name could be null in BeginProcessing (no parameters passed)
// but could be filled when ProcessRecord gets called (parameters from pipeline)
try
{
if(this.fullName==null)
{
WriteObject("fullName is null");
}else
{
foreach (string item in this.fullName)
{
WriteObject("Hello World!"+item);
}
}
}catch(Exception)
{
WriteObject("excetpin");
}
}
// Called after every record has been processed.
protected override void EndProcessing()
{
base.EndProcessing();
}
// Called when the user hits CTRL+C
protected override void StopProcessing()
{
base.StopProcessing();
}
}