MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#if !UNIX5using System;6using System.Collections;7using System.Reflection;8using System.Globalization;9using System.Management.Automation;10using System.Diagnostics.CodeAnalysis;11using System.DirectoryServices;12using Dbg = System.Management.Automation.Diagnostics;13 14namespace Microsoft.PowerShell15{16 /// <summary>17 /// Contains auxiliary ToString CodeMethod implementations for some types.18 /// </summary>19 public static partial class ToStringCodeMethods20 {21 /// <summary>22 /// ToString implementation for PropertyValueCollection.23 /// </summary>24 /// <param name="instance">Instance of PSObject wrapping a PropertyValueCollection.</param>25 public static string PropertyValueCollection(PSObject instance)26 {27 if (instance == null)28 return string.Empty;29 30 var values = (PropertyValueCollection)instance.BaseObject;31 if (values == null)32 return string.Empty;33 34 if (values.Count == 1)35 {36 if (values[0] == null)37 {38 return string.Empty;39 }40 41 return (PSObject.AsPSObject(values[0]).ToString());42 }43 44 return PSObject.ToStringEnumerable(null, (IEnumerable)values, null, null, null);45 }46 }47 48 /// <summary>49 /// Contains CodeMethod implementations for some adapted types like:50 ///51 /// 1. DirectoryEntry Related Code Methods52 /// (a) Convert from DE LargeInteger to Int64.53 /// (b) Convert from DE Dn-With-Binary to string.54 /// </summary>55 public static class AdapterCodeMethods56 {57 #region DirectoryEntry related CodeMethods58 59 /// <summary>60 /// Converts instance of LargeInteger to .net Int64.61 /// </summary>62 /// <param name="deInstance">Instance of PSObject wrapping DirectoryEntry object.</param>63 /// <param name="largeIntegerInstance">Instance of PSObject wrapping LargeInteger instance.</param>64 /// <returns>Converted Int64.</returns>65 [SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "integer")]66 public static Int64 ConvertLargeIntegerToInt64(PSObject deInstance, PSObject largeIntegerInstance)67 {68 if (largeIntegerInstance == null)69 {70 throw PSTraceSource.NewArgumentException(nameof(largeIntegerInstance));71 }72 73 object largeIntObject = (object)largeIntegerInstance.BaseObject;74 Type largeIntType = largeIntObject.GetType();75 76 // the following code might throw exceptions,77 // engine will catch these exceptions78 int highPart = (int)largeIntType.InvokeMember("HighPart",79 BindingFlags.GetProperty | BindingFlags.Public,80 null,81 largeIntObject,82 null,83 CultureInfo.InvariantCulture);84 int lowPart = (int)largeIntType.InvokeMember("LowPart",85 BindingFlags.GetProperty | BindingFlags.Public,86 null,87 largeIntObject,88 null,89 CultureInfo.InvariantCulture);90 91 // LowPart is not really a signed integer. Do not try to92 // use LowPart as a signed integer or you may get intermittent93 // surprises.94 95 // (long)highPart << 32 | (uint)lowPart96 byte[] data = new byte[8];97 BitConverter.GetBytes(lowPart).CopyTo(data, 0);98 BitConverter.GetBytes(highPart).CopyTo(data, 4);99 100 return BitConverter.ToInt64(data, 0);101 }102 103 /// <summary>104 /// Converts instance of DN-With-Binary to .net String.105 /// </summary>106 /// <param name="deInstance">Instance of PSObject wrapping DirectoryEntry object.</param>107 /// <param name="dnWithBinaryInstance">Instance of PSObject wrapping DN-With-Binary object.</param>108 /// <returns>Converted string.</returns>109 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "dn", Justification = "DN represents valid prefix w.r.t Active Directory.")]110 public static string ConvertDNWithBinaryToString(PSObject deInstance, PSObject dnWithBinaryInstance)111 {112 if (dnWithBinaryInstance == null)113 {114 throw PSTraceSource.NewArgumentException(nameof(dnWithBinaryInstance));115 }116 117 object dnWithBinaryObject = (object)dnWithBinaryInstance.BaseObject;118 Type dnWithBinaryType = dnWithBinaryObject.GetType();119 120 // the following code might throw exceptions,121 // engine will catch these exceptions122 string dnString = (string)dnWithBinaryType.InvokeMember("DNString",123 BindingFlags.GetProperty | BindingFlags.Public,124 null,125 dnWithBinaryObject,126 null,127 CultureInfo.InvariantCulture);128 129 return dnString;130 }131 132 #endregion133 }134}135#endif136 