| |
| |
|
|
| using System; |
| using System.IO; |
| using System.Management.Automation; |
| using System.Management.Automation.Host; |
| using System.Management.Automation.Internal; |
| using System.Security; |
|
|
| namespace Microsoft.PowerShell |
| { |
| internal static class SecurityUtils |
| { |
| |
| |
| |
| |
| |
| internal static long GetFileSize(string filePath) |
| { |
| long size = 0; |
|
|
| using (FileStream fs = new(filePath, FileMode.Open)) |
| { |
| size = fs.Length; |
| } |
|
|
| return size; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| internal static SecureString PromptForSecureString(PSHostUserInterface hostUI, |
| string prompt) |
| { |
| SecureString ss = null; |
|
|
| hostUI.Write(prompt); |
| ss = hostUI.ReadLineAsSecureString(); |
| hostUI.WriteLine(string.Empty); |
|
|
| return ss; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| internal static |
| ErrorRecord CreateFileNotFoundErrorRecord(string resourceStr, |
| string errorId, |
| params object[] args) |
| { |
| string message = |
| StringUtil.Format( |
| resourceStr, |
| args |
| ); |
|
|
| FileNotFoundException e = new(message); |
|
|
| ErrorRecord er = new( |
| e, |
| errorId, |
| ErrorCategory.ObjectNotFound, |
| targetObject: null); |
|
|
| return er; |
| } |
|
|
| |
| |
| |
| |
| |
| internal static |
| ErrorRecord CreatePathNotFoundErrorRecord(string path, |
| string errorId) |
| { |
| ItemNotFoundException e = new(path, "PathNotFound", SessionStateStrings.PathNotFound); |
|
|
| ErrorRecord er = new( |
| e, |
| errorId, |
| ErrorCategory.ObjectNotFound, |
| targetObject: null); |
|
|
| return er; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| internal static |
| ErrorRecord CreateNotSupportedErrorRecord(string resourceStr, |
| string errorId, |
| params object[] args) |
| { |
| string message = StringUtil.Format(resourceStr, args); |
|
|
| NotSupportedException e = new(message); |
|
|
| ErrorRecord er = new( |
| e, |
| errorId, |
| ErrorCategory.NotImplemented, |
| targetObject: null); |
|
|
| return er; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| internal static |
| ErrorRecord CreateInvalidArgumentErrorRecord(Exception e, |
| string errorId) |
| { |
| ErrorRecord er = new( |
| e, |
| errorId, |
| ErrorCategory.InvalidArgument, |
| targetObject: null); |
|
|
| return er; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static string GetFilePathOfExistingFile(PSCmdlet cmdlet, |
| string path) |
| { |
| string resolvedProviderPath = cmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath(path); |
| if (File.Exists(resolvedProviderPath)) |
| { |
| return resolvedProviderPath; |
| } |
| else |
| { |
| return null; |
| } |
| } |
| } |
| } |
|
|