public override void ExecuteCmdlet() { ListVMFileOptions options = new ListVMFileOptions(this.BatchContext, this.PoolName, this.VMName, this.VM, this.AdditionalBehaviors) { VMFileName = this.Name, Filter = this.Filter, MaxCount = this.MaxCount, Recursive = this.Recursive.IsPresent }; // The enumerator will internally query the service in chunks. Using WriteObject with the enumerate flag will enumerate // the entire collection first and then write the items out one by one in a single group. Using foreach, we can take // advantage of the enumerator's behavior and write output to the pipeline in bursts. foreach (PSVMFile vmFile in BatchClient.ListVMFiles(options)) { WriteObject(vmFile); } }
/// <summary> /// Lists the vm files matching the specified filter options /// </summary> /// <param name="options">The options to use when querying for vm files</param> /// <returns>The vm files matching the specified filter options</returns> public IEnumerable<PSVMFile> ListVMFiles(ListVMFileOptions options) { if (options == null) { throw new ArgumentNullException("options"); } // Get the single vm file matching the specified name if (!string.IsNullOrEmpty(options.VMFileName)) { WriteVerbose(string.Format(Resources.GBVMF_GetByName, options.VMFileName, options.VMName)); using (IPoolManager poolManager = options.Context.BatchOMClient.OpenPoolManager()) { ITaskFile vmFile = poolManager.GetVMFile(options.PoolName, options.VMName, options.VMFileName, options.AdditionalBehaviors); PSVMFile psVMFile = new PSVMFile(vmFile); return new PSVMFile[] { psVMFile }; } } // List vm files using the specified filter else { string vmName = options.VM == null ? options.VMName : options.VM.Name; ODATADetailLevel odata = null; string verboseLogString = null; if (!string.IsNullOrEmpty(options.Filter)) { verboseLogString = string.Format(Resources.GBVMF_GetByOData, vmName); odata = new ODATADetailLevel(filterClause: options.Filter); } else { verboseLogString = string.Format(Resources.GBVMF_NoFilter, vmName); } WriteVerbose(verboseLogString); IEnumerableAsyncExtended<ITaskFile> vmFiles = null; if (options.VM != null) { vmFiles = options.VM.omObject.ListVMFiles(options.Recursive, odata, options.AdditionalBehaviors); } else { using (IPoolManager poolManager = options.Context.BatchOMClient.OpenPoolManager()) { vmFiles = poolManager.ListVMFiles(options.PoolName, options.VMName, options.Recursive, odata, options.AdditionalBehaviors); } } Func<ITaskFile, PSVMFile> mappingFunction = f => { return new PSVMFile(f); }; return PSAsyncEnumerable<PSVMFile, ITaskFile>.CreateWithMaxCount( vmFiles, mappingFunction, options.MaxCount, () => WriteVerbose(string.Format(Resources.MaxCount, options.MaxCount))); } }