11using namespace System.Security.Cryptography.X509Certificates
22using namespace System.Management.Automation
33
4- $Script :LibPath = Join-Path - Path $PSScriptRoot - ChildPath lib
4+ $importModule = Get-Command - Name Import-Module - Module Microsoft.PowerShell.Core
5+ if (' PSWSMan.OnModuleImportAndRemove' -as [type ]) {
6+ & $importModule - Force - Assembly ([PSWSMan.OnModuleImportAndRemove ].Assembly)
7+ }
8+ else {
9+ & $importModule ([IO.Path ]::Combine($PSScriptRoot , ' bin' , ' PSWSMan.dll' )) - ErrorAction Stop
10+ }
11+
12+ $Script :LibPath = Join-Path - Path $PSScriptRoot - ChildPath bin
513
614class X509CertificateChainAttribute : ArgumentTransformationAttribute {
715 [object ] Transform([EngineIntrinsics ]$EngineIntrinsics , [object ]$InputData ) {
@@ -23,136 +31,6 @@ class X509CertificateChainAttribute : ArgumentTransformationAttribute {
2331 }
2432}
2533
26- Add-Type - TypeDefinition @'
27- using System;
28- using System.Collections.Generic;
29- using System.Reflection;
30- using System.Runtime.InteropServices;
31-
32- namespace PSWSMan
33- {
34- public class Native
35- {
36- [StructLayout(LayoutKind.Sequential)]
37- public class PWSH_Version
38- {
39- public Int32 Major;
40- public Int32 Minor;
41- public Int32 Build;
42- public Int32 Revision;
43-
44- public static explicit operator Version(PWSH_Version v)
45- {
46- return new Version(v.Major, v.Minor, v.Build, v.Revision);
47- }
48- }
49-
50- [DllImport("libc")]
51- public static extern void setenv(string name, string value);
52-
53- [DllImport("libc")]
54- public static extern void unsetenv(string name);
55-
56- [DllImport("libc")]
57- public static extern IntPtr gnu_get_libc_version();
58-
59- [DllImport("libmi")]
60- public static extern void MI_Version_Info(PWSH_Version version);
61-
62- [DllImport("libpsrpclient")]
63- public static extern void PSRP_Version_Info(PWSH_Version version);
64-
65- private delegate uint OpenSSL_version_num_ptr();
66-
67- public static uint OpenSSL_version_num(string[] libSSLPaths)
68- {
69- IntPtr lib = LoadLibrary(libSSLPaths);
70- if (lib == IntPtr.Zero)
71- return 0;
72-
73- try
74- {
75- // OpenSSL_version_num was introduced in 1.1.x, use SSLeay for older versions.
76- string[] functionNames = {"OpenSSL_version_num", "SSLeay"};
77-
78- foreach (string name in functionNames)
79- {
80- IntPtr functionAddr = IntPtr.Zero;
81- try
82- {
83- functionAddr = NativeLibrary.GetExport(lib, name);
84- }
85- catch (EntryPointNotFoundException) {}
86-
87- if (functionAddr == IntPtr.Zero)
88- continue;
89-
90- var function = (OpenSSL_version_num_ptr)Marshal.GetDelegateForFunctionPointer(
91- functionAddr, typeof(OpenSSL_version_num_ptr));
92- return function();
93- }
94-
95- return 0;
96- }
97- finally {
98- NativeLibrary.Free(lib);
99- }
100- }
101-
102- private delegate IntPtr OpenSSL_version_ptr(int t);
103-
104- public static string OpenSSL_version(string[] libSSLPaths, int t)
105- {
106- IntPtr lib = LoadLibrary(libSSLPaths);
107- if (lib == IntPtr.Zero)
108- return null;
109-
110- try
111- {
112- IntPtr functionAddr = IntPtr.Zero;
113-
114- try
115- {
116- functionAddr = NativeLibrary.GetExport(lib, "OpenSSL_version");
117- }
118- catch (EntryPointNotFoundException) {}
119-
120- if (functionAddr == IntPtr.Zero)
121- return null;
122-
123- var function = (OpenSSL_version_ptr)Marshal.GetDelegateForFunctionPointer(
124- functionAddr, typeof(OpenSSL_version_ptr));
125-
126- return Marshal.PtrToStringAuto(function(t));
127- }
128- finally {
129- NativeLibrary.Free(lib);
130- }
131- }
132-
133- private static IntPtr LoadLibrary(string[] loadPaths)
134- {
135- foreach(string path in loadPaths)
136- {
137- IntPtr handle = IntPtr.Zero;
138- try
139- {
140- if (NativeLibrary.TryLoad(path, out handle))
141- return handle;
142- }
143- catch
144- {
145- // TryLoad can actually through an exception so we just ignore it and continue on.
146- continue;
147- }
148- }
149-
150- return IntPtr.Zero;
151- }
152- }
153- }
154- '@
155-
15634Function exec {
15735 <#
15836 . SYNOPSIS
0 commit comments