Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

c# - How to check null on passed PowerShell argument

Below is the C# code where I am creating this powershell cmdlt which has two argument MaxAge and ContinuationToken. I would like

  1. MaxAge to be mandatory when Continuation token is not provided.
  2. ContinuationToken if provided to be mandatory and MaxAge to be optional.
  3. ContinuationToken cannot be null whenever it is provided without MaxAge and it should be prompted to user to provide that value.

I am able to achieve my 1st two scenario but unable to achieve the last one. Below is my pwshell cmdlet and my C# code. Please advice:

[Cmdlet(VerbsCommon.Get, "ChangedRecordings", DefaultParameterSetName = GetChangedRecordingsCmd.ParamSetCloud)]
public class GetChangedRecordingsCmd : PwshCmd

{
protected const string ParamSetCloud = "Cloud";
protected const string ParamSetFile = "File";

[Parameter(Mandatory = true,
Position = 0, ParameterSetName = ParamSetCloud,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true, HelpMessage = "Provide the maximum age of recordings from the change feed")]
[Parameter(Mandatory = false, ParameterSetName = ParamSetFile)]
public TimeSpan MaxAge { get; set; }

[Parameter(Position = 1,Mandatory = true,
ValueFromPipeline = true, ParameterSetName = ParamSetFile,
ValueFromPipelineByPropertyName = true, HelpMessage = "Provide a continuation token for the change feed")]

public string ContinuationToken { get; set; }

protected override void BeginProcessing()
{
if (ParameterSetName.Equals(ParamSetFile) && string.IsNullOrWhiteSpace(ContinuationToken))
{
WriteWarning("Continuation Token can't be null. Please pass a valid Continuation Token");
}
}

protected override void ProcessRecord()
{
//My opeartions
WriteObject(new { ContinuationToken = result, Recordings = recs });
}

Corresponding Powershell Cmndlet(Token File path contains the Continuation token. As per above scenario that can be blank and I need to handle this in C#)

param (
  $TokenFilePath = 'C:UsersDesktopContinuationToken.txt',
  $MaxAge = '2'
)
$existingToken = Get-Content -Path $TokenFilePath
$recordings = Get-ChangedRecordings -ContinuationToken $existingToken -MaxResults 10
Write-Host $recordings.Recordings.Count
$recordings.ContinuationToken|Set-Content -Path $TokenFilePath
$recs = $recordings.Recordings

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For this, you'll probably want to use the ValidateNotNull attribute:

[ValidateNotNull()]
[Parameter(Position = 1,Mandatory = true,
ValueFromPipeline = true, ParameterSetName = ParamSetFile,
ValueFromPipelineByPropertyName = true, HelpMessage = "Provide a continuation token for the change feed")]
public string ContinuationToken { get; set; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...