File size: 15,878 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// #define LOGENABLE // uncomment this line to enable the log,
// create c:\temp\cim.log before invoking cimcmdlets
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
namespace Microsoft.Management.Infrastructure.CimCmdlets
{
/// <summary>
/// <para>
/// Global Non-localization strings
/// </para>
/// </summary>
internal static class ConstValue
{
/// <summary>
/// <para>
/// Default computername
/// </para>
/// </summary>
internal static readonly string[] DefaultSessionName = { @"*" };
/// <summary>
/// <para>
/// Empty computername, which will create DCOM session
/// </para>
/// </summary>
internal static readonly string NullComputerName = null;
/// <summary>
/// <para>
/// Empty computername array, which will create DCOM session
/// </para>
/// </summary>
internal static readonly string[] NullComputerNames = { NullComputerName };
/// <summary>
/// <para>
/// localhost computername, which will create WSMAN session
/// </para>
/// </summary>
internal static readonly string LocalhostComputerName = @"localhost";
/// <summary>
/// <para>
/// Default namespace
/// </para>
/// </summary>
internal static readonly string DefaultNameSpace = @"root\cimv2";
/// <summary>
/// <para>
/// Default namespace
/// </para>
/// </summary>
internal static readonly string DefaultQueryDialect = @"WQL";
/// <summary>
/// Name of the note property that controls if "PSComputerName" column is shown.
/// </summary>
internal static readonly string ShowComputerNameNoteProperty = "PSShowComputerName";
/// <summary>
/// <para>
/// Whether given computername is either null or empty
/// </para>
/// </summary>
/// <param name="computerName"></param>
/// <returns></returns>
internal static bool IsDefaultComputerName(string computerName)
{
return string.IsNullOrEmpty(computerName);
}
/// <summary>
/// <para>
/// Get computer names, if it is null then return DCOM one
/// </para>
/// </summary>
/// <param name="computerNames"></param>
/// <returns></returns>
internal static IEnumerable<string> GetComputerNames(IEnumerable<string> computerNames)
{
return computerNames ?? NullComputerNames;
}
/// <summary>
/// Get computer name, if it is null then return default one.
/// </summary>
/// <param name="computerName"></param>
/// <returns></returns>
internal static string GetComputerName(string computerName)
{
return string.IsNullOrEmpty(computerName) ? NullComputerName : computerName;
}
/// <summary>
/// <para>
/// Get namespace, if it is null then return default one
/// </para>
/// </summary>
/// <param name="nameSpace"></param>
/// <returns></returns>
internal static string GetNamespace(string nameSpace)
{
return nameSpace ?? DefaultNameSpace;
}
/// <summary>
/// <para>
/// Get queryDialect, if it is null then return default query Dialect
/// </para>
/// </summary>
/// <param name="queryDialect"></param>
/// <returns></returns>
internal static string GetQueryDialectWithDefault(string queryDialect)
{
return queryDialect ?? DefaultQueryDialect;
}
}
/// <summary>
/// <para>
/// Debug helper class used to dump debug message to log file
/// </para>
/// </summary>
internal static class DebugHelper
{
#region private members
/// <summary>
/// Flag used to control generating log message into file.
/// </summary>
internal static bool GenerateLog { get; set; } = true;
/// <summary>
/// Whether the log been initialized.
/// </summary>
private static bool logInitialized = false;
internal static bool GenerateVerboseMessage { get; set; } = true;
/// <summary>
/// Flag used to control generating message into powershell.
/// </summary>
internal static readonly string logFile = @"c:\temp\Cim.log";
/// <summary>
/// Indent space string.
/// </summary>
internal static readonly string space = @" ";
/// <summary>
/// Indent space strings array.
/// </summary>
internal static readonly string[] spaces = {
string.Empty,
space,
space + space,
space + space + space,
space + space + space + space,
space + space + space + space + space,
};
/// <summary>
/// Lock the log file.
/// </summary>
internal static readonly object logLock = new();
#endregion
#region internal strings
internal static readonly string runspaceStateChanged = "Runspace {0} state changed to {1}";
internal static readonly string classDumpInfo = @"Class type is {0}";
internal static readonly string propertyDumpInfo = @"Property name {0} of type {1}, its value is {2}";
internal static readonly string defaultPropertyType = @"It is a default property, default value is {0}";
internal static readonly string propertyValueSet = @"This property value is set by user {0}";
internal static readonly string addParameterSetName = @"Add parameter set {0} name to cache";
internal static readonly string removeParameterSetName = @"Remove parameter set {0} name from cache";
internal static readonly string currentParameterSetNameCount = @"Cache have {0} parameter set names";
internal static readonly string currentParameterSetNameInCache = @"Cache have parameter set {0} valid {1}";
internal static readonly string currentnonMandatoryParameterSetInCache = @"Cache have optional parameter set {0} valid {1}";
internal static readonly string optionalParameterSetNameCount = @"Cache have {0} optional parameter set names";
internal static readonly string finalParameterSetName = @"------Final parameter set name of the cmdlet is {0}";
internal static readonly string addToOptionalParameterSet = @"Add to optional ParameterSetNames {0}";
internal static readonly string startToResolveParameterSet = @"------Resolve ParameterSet Name";
internal static readonly string reservedString = @"------";
#endregion
#region runtime methods
internal static string GetSourceCodeInformation(bool withFileName, int depth)
{
StackTrace trace = new();
StackFrame frame = trace.GetFrame(depth);
return string.Create(CultureInfo.CurrentUICulture, $"{frame.GetMethod().DeclaringType.Name}::{frame.GetMethod().Name} ");
}
#endregion
/// <summary>
/// Write message to log file named @logFile.
/// </summary>
/// <param name="message"></param>
internal static void WriteLog(string message)
{
WriteLog(message, 0);
}
/// <summary>
/// Write blank line to log file named @logFile.
/// </summary>
/// <param name="message"></param>
internal static void WriteEmptyLine()
{
WriteLog(string.Empty, 0);
}
/// <summary>
/// Write message to log file named @logFile with args.
/// </summary>
/// <param name="message"></param>
internal static void WriteLog(string message, int indent, params object[] args)
{
string outMessage = string.Empty;
FormatLogMessage(ref outMessage, message, args);
WriteLog(outMessage, indent);
}
/// <summary>
/// Write message to log file w/o arguments.
/// </summary>
/// <param name="message"></param>
/// <param name="indent"></param>
internal static void WriteLog(string message, int indent)
{
WriteLogInternal(message, indent, -1);
}
/// <summary>
/// Write message to log file named @logFile with args.
/// </summary>
/// <param name="message"></param>
internal static void WriteLogEx(string message, int indent, params object[] args)
{
string outMessage = string.Empty;
WriteLogInternal(string.Empty, 0, -1);
FormatLogMessage(ref outMessage, message, args);
WriteLogInternal(outMessage, indent, 3);
}
/// <summary>
/// Write message to log file w/o arguments.
/// </summary>
/// <param name="message"></param>
/// <param name="indent"></param>
internal static void WriteLogEx(string message, int indent)
{
WriteLogInternal(string.Empty, 0, -1);
WriteLogInternal(message, indent, 3);
}
/// <summary>
/// Write message to log file w/o arguments.
/// </summary>
/// <param name="message"></param>
/// <param name="indent"></param>
internal static void WriteLogEx()
{
WriteLogInternal(string.Empty, 0, -1);
WriteLogInternal(string.Empty, 0, 3);
}
/// <summary>
/// Format the message.
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
/// <returns></returns>
[Conditional("LOGENABLE")]
private static void FormatLogMessage(ref string outMessage, string message, params object[] args)
{
outMessage = string.Format(CultureInfo.CurrentCulture, message, args);
}
/// <summary>
/// Write message to log file named @logFile
/// with indent space ahead of the message.
/// </summary>
/// <param name="message"></param>
/// <param name="nIndent"></param>
[Conditional("LOGENABLE")]
private static void WriteLogInternal(string message, int indent, int depth)
{
if (!logInitialized)
{
lock (logLock)
{
if (!logInitialized)
{
DebugHelper.GenerateLog = File.Exists(logFile);
logInitialized = true;
}
}
}
if (GenerateLog)
{
if (indent < 0)
{
indent = 0;
}
if (indent > 5)
{
indent = 5;
}
string sourceInformation = string.Empty;
if (depth != -1)
{
sourceInformation = string.Format(
CultureInfo.InvariantCulture,
"Thread {0}#{1}:{2}:{3} {4}",
Environment.CurrentManagedThreadId,
DateTime.Now.Hour,
DateTime.Now.Minute,
DateTime.Now.Second,
GetSourceCodeInformation(true, depth));
}
lock (logLock)
{
using (FileStream fs = new(logFile, FileMode.OpenOrCreate))
using (StreamWriter writer = new(fs))
{
writer.WriteLineAsync(spaces[indent] + sourceInformation + @" " + message);
}
}
}
}
}
/// <summary>
/// <para>
/// Helper class used to validate given parameter
/// </para>
/// </summary>
internal static class ValidationHelper
{
/// <summary>
/// Validate the argument is not null.
/// </summary>
/// <param name="obj"></param>
/// <param name="argumentName"></param>
public static void ValidateNoNullArgument(object obj, string argumentName)
{
ArgumentNullException.ThrowIfNull(obj, argumentName);
}
/// <summary>
/// Validate the argument is not null and not whitespace.
/// </summary>
/// <param name="obj"></param>
/// <param name="argumentName"></param>
public static void ValidateNoNullorWhiteSpaceArgument(string obj, string argumentName)
{
if (string.IsNullOrWhiteSpace(obj))
{
throw new ArgumentException(argumentName);
}
}
/// <summary>
/// Validate that given classname/propertyname is a valid name compliance with DMTF standard.
/// Only for verifying ClassName and PropertyName argument.
/// </summary>
/// <param name="parameterName"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="ArgumentException">Throw if the given value is not a valid name (class name or property name).</exception>
public static string ValidateArgumentIsValidName(string parameterName, string value)
{
DebugHelper.WriteLogEx();
if (value != null)
{
string trimed = value.Trim();
// The first character should be contained in set: [A-Za-z_]
// Inner characters should be contained in set: [A-Za-z0-9_]
Regex regex = new(@"^[a-zA-Z_][a-zA-Z0-9_]*\z");
if (regex.IsMatch(trimed))
{
DebugHelper.WriteLogEx("A valid name: {0}={1}", 0, parameterName, value);
return trimed;
}
}
DebugHelper.WriteLogEx("An invalid name: {0}={1}", 0, parameterName, value);
throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.InvalidParameterValue, value, parameterName));
}
/// <summary>
/// Validate given arry argument contains all valid name (for -SelectProperties).
/// * is valid for this case.
/// </summary>
/// <param name="parameterName"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="ArgumentException">Throw if the given value contains any invalid name (class name or property name).</exception>
public static string[] ValidateArgumentIsValidName(string parameterName, string[] value)
{
if (value != null)
{
foreach (string propertyName in value)
{
// * is wild char supported in select properties
if ((propertyName != null) && string.Equals(propertyName.Trim(), "*", StringComparison.OrdinalIgnoreCase))
{
continue;
}
ValidationHelper.ValidateArgumentIsValidName(parameterName, propertyName);
}
}
return value;
}
}
}
|