Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Utils.cs170 linesDownload Raw Back to security
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.IO;6using System.Management.Automation;7using System.Management.Automation.Host;8using System.Management.Automation.Internal;9using System.Security;10 11namespace Microsoft.PowerShell12{13    internal static class SecurityUtils14    {15        /// <summary>16        /// Gets the size of a file.17        /// </summary>18        /// <param name="filePath">Path to file.</param>19        /// <returns>File size.</returns>20        internal static long GetFileSize(string filePath)21        {22            long size = 0;23 24            using (FileStream fs = new(filePath, FileMode.Open))25            {26                size = fs.Length;27            }28 29            return size;30        }31 32        /// <summary>33        /// Present a prompt for a SecureString data.34        /// </summary>35        /// <param name="hostUI">Ref to host ui interface.</param>36        /// <param name="prompt">Prompt text.</param>37        /// <returns> user input as secure string.</returns>38        internal static SecureString PromptForSecureString(PSHostUserInterface hostUI,39                                                           string prompt)40        {41            SecureString ss = null;42 43            hostUI.Write(prompt);44            ss = hostUI.ReadLineAsSecureString();45            hostUI.WriteLine(string.Empty);46 47            return ss;48        }49 50        /// <summary>51        /// </summary>52        /// <param name="resourceStr">Resource string.</param>53        /// <param name="errorId">Error identifier.</param>54        /// <param name="args">Replacement params for resource string formatting.</param>55        /// <returns></returns>56        internal static57        ErrorRecord CreateFileNotFoundErrorRecord(string resourceStr,58                                                  string errorId,59                                                  params object[] args)60        {61            string message =62                StringUtil.Format(63                    resourceStr,64                    args65                );66 67            FileNotFoundException e = new(message);68 69            ErrorRecord er = new(70                e,71                errorId,72                ErrorCategory.ObjectNotFound,73                targetObject: null);74 75            return er;76        }77 78        /// <summary>79        /// </summary>80        /// <param name="path">Path that was not found.</param>81        /// <param name="errorId">Error identifier.</param>82        /// <returns>ErrorRecord instance.</returns>83        internal static84        ErrorRecord CreatePathNotFoundErrorRecord(string path,85                                                  string errorId)86        {87            ItemNotFoundException e = new(path, "PathNotFound", SessionStateStrings.PathNotFound);88 89            ErrorRecord er = new(90                e,91                errorId,92                ErrorCategory.ObjectNotFound,93                targetObject: null);94 95            return er;96        }97 98        /// <summary>99        /// Create an error record for 'operation not supported' condition.100        /// </summary>101        /// <param name="resourceStr">Resource string.</param>102        /// <param name="errorId">Error identifier.</param>103        /// <param name="args">Replacement params for resource string formatting.</param>104        /// <returns></returns>105        internal static106        ErrorRecord CreateNotSupportedErrorRecord(string resourceStr,107                                                  string errorId,108                                                  params object[] args)109        {110            string message = StringUtil.Format(resourceStr, args);111 112            NotSupportedException e = new(message);113 114            ErrorRecord er = new(115                e,116                errorId,117                ErrorCategory.NotImplemented,118                targetObject: null);119 120            return er;121        }122 123        /// <summary>124        /// Create an error record for 'operation not supported' condition.125        /// </summary>126        /// <param name="e">Exception to include in ErrorRecord.</param>127        /// <param name="errorId">Error identifier.</param>128        /// <returns></returns>129        internal static130        ErrorRecord CreateInvalidArgumentErrorRecord(Exception e,131                                                     string errorId)132        {133            ErrorRecord er = new(134                e,135                errorId,136                ErrorCategory.InvalidArgument,137                targetObject: null);138 139            return er;140        }141 142        /// <summary>143        /// Convert the specified provider path to a provider path144        /// and make sure that all of the following is true:145        /// -- it represents a FileSystem path146        /// -- it points to a file147        /// -- the file exists.148        /// </summary>149        /// <param name="cmdlet">Cmdlet instance.</param>150        /// <param name="path">Provider path.</param>151        /// <returns>152        /// filesystem path if all conditions are true,153        /// null otherwise154        /// </returns>155        internal static string GetFilePathOfExistingFile(PSCmdlet cmdlet,156                                                         string path)157        {158            string resolvedProviderPath = cmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath(path);159            if (File.Exists(resolvedProviderPath))160            {161                return resolvedProviderPath;162            }163            else164            {165                return null;166            }167        }168    }169}170