Welcome › Forums › General PowerShell Q&A › How to check that an argument to be not null when other argument is not present
- This topic has 0 replies, 1 voice, and was last updated 1 week, 6 days ago by
ankitdagreat71.
-
AuthorPosts
-
-
January 12, 2021 at 2:13 pm #285517
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 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 });
}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:\Users\Desktop\ContinuationToken.txt',$MaxAge = '2')$existingToken = Get-Content -Path $TokenFilePath$recordings = Get-ChangedRecordings -ContinuationToken $existingToken -MaxResults 10Write-Host $recordings.Recordings.Count$recordings.ContinuationToken|Set-Content -Path $TokenFilePath$recs = $recordings.Recordings-
This topic was modified 1 week, 6 days ago by
ankitdagreat71.
-
-
AuthorPosts
- You must be logged in to reply to this topic.