Below is the C# code where I am creating this powershell cmdlt which has two argument MaxAge and ContinuationToken. I would like
- MaxAge to be mandatory when Continuation token is not provided.
- ContinuationToken if provided to be mandatory and MaxAge to be
optional.
- 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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…