protected override void ProcessRecord() { if (NewFileName == null) { throw new ArgumentNullException(nameof(NewFileName)); } string extensionName; if (NewExtension != null) { NewExtensionId = NewExtension.Id; extensionName = NewExtension.Extension; } else { extensionName = ExtensionHelper.GetExtensionName(NewExtensionId, out _, out _); if (extensionName == null) { throw new ArgumentException("Extension cannot be found.", nameof(NewExtensionId)); } } if (File != null) { Id = File.Id; } var connection = DatabaseConnection.Current; if (!SkipFile.IsPresent) { var fileName = FileHelper.GetFileName(Id, out var folderPath, out var path, out _, out var isFolderSealed, out var folderId); if (fileName == null) { ThrowTerminatingError(new ErrorRecord(new ArgumentException("File cannot be found.", nameof(Id)), "ImageStore Rename File", ErrorCategory.InvalidArgument, null)); } if (isFolderSealed && !OverrideSealedFolder.IsPresent) { ThrowTerminatingError(new ErrorRecord(new InvalidOperationException("Folder containing this file is set to sealed and -" + nameof(OverrideSealedFolder) + " is not present."), "ImageStore Rename File", ErrorCategory.SecurityError, folderId)); } if (System.IO.File.Exists(fileName)) { string newName; if (path != "") { newName = folderPath + path + DirectorySeparatorString.Value + NewFileName + "." + extensionName; } else { newName = folderPath + NewFileName + "." + extensionName; } System.IO.File.Move(fileName, newName); WriteInformation("File is renamed from " + fileName + " to " + newName + ".", new string[] { "RenameFile" }); } else { WriteWarning("File to be renaming cannot be found. Path: " + fileName); } } using (var command = new SqlCommand("Update [File] Set [FileName]=@FileName, [ExtensionId]=@ExtensionId where [Id]=@Id")) { command.Connection = connection; command.Parameters.Add(new SqlParameter("@Id", System.Data.SqlDbType.UniqueIdentifier) { Value = Id }); command.Parameters.Add(new SqlParameter("@FileName", System.Data.SqlDbType.NVarChar, 256) { Value = NewFileName }); command.Parameters.Add(new SqlParameter("@ExtensionId", System.Data.SqlDbType.UniqueIdentifier) { Value = NewExtensionId }); if (command.ExecuteNonQuery() > 0) { if (SkipReturn.IsPresent) { WriteObject(null); } else { var newFile = GetFileCmdlet.GetFile(File.Id); WriteObject(newFile); } } else { ThrowTerminatingError(new ErrorRecord( new InvalidOperationException("Cannot rename this file."), "ImageStore Rename File", ErrorCategory.WriteError, null)); } } }