public override void ExecuteCmdlet() { if (this.ShouldProcess(string.Format("Close File Handles for File or FileDirectory on Path: {0}", this.Path != null? this.Path : (this.FileHandle != null? this.FileHandle.Path: null)), "This operation will force the provided file handle(s) closed, which may cause data loss or corruption for active applications/users.", null)) { CloudFileDirectory baseDirectory = null; switch (this.ParameterSetName) { case DirectoryCloseAllParameterSetName: baseDirectory = this.Directory; break; case ShareNameCloseSingleParameterSetName: case ShareNameCloseAllParameterSetName: baseDirectory = this.BuildFileShareObjectFromName(this.ShareName).GetRootDirectoryReference(); break; case ShareCloseSingleParameterSetName: case ShareCloseAllParameterSetName: baseDirectory = this.Share.GetRootDirectoryReference(); break; case FileCloseAllParameterSetName: // Don't need to set baseDirectory when input is a CloudFile break; default: throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName)); } if (ParameterSetName == ShareNameCloseSingleParameterSetName || ParameterSetName == ShareCloseSingleParameterSetName) { this.Path = FileHandle.Path; } // When not input path/File, the list handle target must be a Dir bool foundAFolder = true; CloudFileDirectory targetDir = baseDirectory; CloudFile targetFile = null; if (this.File != null) { targetFile = this.File; foundAFolder = false; } else { if (!string.IsNullOrEmpty(this.Path)) { string[] subfolders = NamingUtil.ValidatePath(this.Path); targetDir = baseDirectory.GetDirectoryReferenceByPath(subfolders); // Don't need check the path target to File or FileDir since: // 1. check File/FileDir exist will fail on File/FileDir with DeletePending status // 2. The File handle request send with CloudFileDirectory and CloudFile are same with same path, so need to differ it. } } // Recursive only take effect on File Dir if (!foundAFolder && Recursive.IsPresent) { WriteVerbose("The target object of the 'Path' is an Azure File, the parameter '-Recursive' won't take effect."); } //Close handle CloseFileHandleResultSegment closeResult = null; FileContinuationToken continuationToken = null; int numHandlesClosed = 0; do { if (foundAFolder) { if (FileHandle != null) { // close single handle on fileDir if (this.FileHandle.HandleId == null) { throw new System.ArgumentException(string.Format("The HandleId of the FileHandle on path {0} should not be null.", this.FileHandle.Path), "FileHandle"); } closeResult = targetDir.CloseHandleSegmented(this.FileHandle.HandleId.ToString(), continuationToken, Recursive, null, this.RequestOptions, this.OperationContext); } else { // close all handle on fileDir closeResult = targetDir.CloseAllHandlesSegmented(continuationToken, Recursive, null, this.RequestOptions, this.OperationContext); } } else { if (FileHandle != null) { // close single handle on file if (this.FileHandle.HandleId == null) { throw new System.ArgumentException(string.Format("The HandleId of the FileHandle on path {0} should not be null.", this.FileHandle.Path), "FileHandle"); } closeResult = targetFile.CloseHandleSegmented(this.FileHandle.HandleId.ToString(), continuationToken, null, this.RequestOptions, this.OperationContext); } else { // close all handle on file closeResult = targetFile.CloseAllHandlesSegmented(continuationToken, null, this.RequestOptions, this.OperationContext); } } numHandlesClosed += closeResult.NumHandlesClosed; continuationToken = closeResult.ContinuationToken; } while (continuationToken != null && continuationToken.NextMarker != null); if (PassThru) { WriteObject(numHandlesClosed); } } }