-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPoshFunctions.psm1
251 lines (216 loc) · 10.4 KB
/
PoshFunctions.psm1
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
# PoshFunctions.psm1
# Author: Bill Riedy
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Web
$Script:ModulePath = Split-Path -Parent -Path $MyInvocation.MyCommand.Path
$Script:FunctionsPath = Join-Path -Path $Script:ModulePath -ChildPath 'Functions'
$Functions = Get-ChildItem -Path $Script:FunctionsPath -Filter *.ps1
$Functions | ForEach-Object { . $_.FullName }
$Script:WordList = Get-Content -Path $PSScriptRoot\Resources\WordList.txt -ReadCount 0
$Script:WordListFull = Get-Content -Path $PSScriptRoot\Resources\words_alpha.txt -ReadCount 0
$Script:FortuneFile = Join-Path -Path $ModulePath -ChildPath 'Resources\Wisdom.txt'
$Script:Stopwatch = [System.Diagnostics.Stopwatch]::New()
$Script:IconFile = Join-Path -Path $ModulePath -ChildPath 'Resources\PoshFunctions.ico'
# inspired by: http://powershell-scripting.com/index.php?option=com_joomlaboard&Itemid=76&func=view&view=threaded&id=24376&catid=5
# also sourced at: https://gallery.technet.microsoft.com/Edit-old-fashioned-INI-f8fbc067?redir=0
# most recently found at: https://web.archive.org/web/*/https://gallery.technet.microsoft.com/Edit-old-fashioned-INI-f8fbc067/file/148760/1/IniFileManager.ps1
$IniCode = @'
/* ======================================================================
C# Source File -- Created with SAPIEN Technologies PrimalScript 2011
NAME:
AUTHOR: James Vierra, DSS
DATE : 8/30/2012
COMMENT:
Examples:
add-type -Path profileapi.cs
$sb = New-Object System.Text.StringBuilder(256)
[profileapi]::GetPrivateProfileString('section1', 'test1', 'dummy', $sb, $sb.Capacity, "$pwd\test.ini")
Write-Host ('Returned value is {0}.' -f $sb.ToString()) -ForegroundColor green
[profileapi]::WritePrivateProfileString('section2', 'test5', 'Some new value', "$pwd\test.ini")
[profileapi]::GetPrivateProfileString('section2', 'test5', 'dummy', $sb, $sb.Capacity, "$pwd\test.ini")
Write-Host ('Returned value is {0}.' -f $sb.ToString()) -ForegroundColor green
====================================================================== */
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
public class ProfileAPI{
[DllImport("kernel32.dll")]
public static extern bool WriteProfileSection(
string lpAppName,
string lpString);
[DllImport("kernel32.dll")]
public static extern bool WriteProfileString(
string lpAppName,
string lpKeyName,
string lpString);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WritePrivateProfileString(
string lpAppName,
string lpKeyName,
string lpString,
string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern uint GetPrivateProfileSectionNames(
IntPtr lpReturnedString,
uint nSize,
string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
uint nSize,
string lpFileName);
public static string[] GetSectionNames(string iniFile) {
uint MAX_BUFFER = 32767;
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile);
if (bytesReturned == 0) {
Marshal.FreeCoTaskMem(pReturnedString);
return null;
}
string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
char[] c = new char[1];
c[0] = '\x0';
return local.Split(c, System.StringSplitOptions.RemoveEmptyEntries);
//Marshal.FreeCoTaskMem(pReturnedString);
//use of Substring below removes terminating null for split
//char[] c = local.ToCharArray();
//return MultistringToStringArray(ref c);
//return c;
//return local; //.Substring(0, local.Length - 1).Split('\0');
}
public static string[] GetSection(string iniFilePath, string sectionName) {
uint MAX_BUFFER = 32767;
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
uint bytesReturned = GetPrivateProfileSection(sectionName, pReturnedString, MAX_BUFFER, iniFilePath);
if (bytesReturned == 0) {
Marshal.FreeCoTaskMem(pReturnedString);
return null;
}
string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
char[] c = new char[1] { '\x0' };
return local.Split(c, System.StringSplitOptions.RemoveEmptyEntries);
}
}
'@
Add-Type -TypeDefinition $IniCode
# source: https://stackoverflow.com/questions/255419/how-can-i-mute-unmute-my-sound-from-powershell
# changed public class from 'Audio' to 'PFAudio'
Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;
[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume {
// f(), g(), ... are unused COM method slots. Define these if you care
int f(); int g(); int h(); int i();
int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
int j();
int GetMasterVolumeLevelScalar(out float pfLevel);
int k(); int l(); int m(); int n();
int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
int GetMute(out bool pbMute);
}
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice {
int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator {
int f(); // Unused
int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
public class PFAudio {
static IAudioEndpointVolume Vol() {
var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
IMMDevice dev = null;
Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
IAudioEndpointVolume epv = null;
var epvid = typeof(IAudioEndpointVolume).GUID;
Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
return epv;
}
public static float Volume {
get {float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v;}
set {Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty));}
}
public static bool Mute {
get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
}
}
'@
<#
A note on the properties of PFDateFormat:
DMTF is a [string] representing [datetime] in the form 'yyyymmddHHMMSS.ffffff+UUU'
Unix is Unix epoch format [double] which is the number of seconds since '1/1/1970 12:00:00 AM UTC'
FileTime is an [int64] which represents a [datetime] expressed in Ticks. A Tick represents 1/100,000 of a second. Ticks can range from 0 - 2650467743999999999. Translating these into dates you get
0 = Monday, January 01, 1601 12:00:00.00000 AM
2650467743999999999 = Friday, December 31, 9999 11:59:59.99999 PM
ICSDateTime is a [datetime] formatted is of the form 'yyyymmddTHHMMSSZ'
Excel is a [double] which represents dates as the number of days since (Get-Date 1/1/1900)
#>
# console output formatted according to PFDateFormat entry in .\PoshFunctions.ps1xml
class PFDateFormat {
[datetime] $Date = $(Get-Date)
[string] $DMTF
[double] $Unix
[int64] $FileTime
[string] $ICSDateTime
[string] $ISO8601
[double] $Excel
# add parameterless default constructor
PFDateFormat() {
$this.Date = $(Get-Date)
$this.DMTF = $(ConvertFrom-DateTime -Date $this.Date -DMTF -Verbose:$false)
$this.Unix = $(ConvertFrom-DateTime -Date $this.Date -Unix -Verbose:$false)
$this.FileTime = $(ConvertFrom-DateTime -Date $this.Date -FileTime -Verbose:$false)
$this.ICSDateTime = $(ConvertFrom-DateTime -Date $this.Date -ICSDateTime -Verbose:$false)
$this.ISO8601 = $(ConvertFrom-DateTime -Date $this.Date -ISO8601 -Verbose:$false)
$this.Excel = $(ConvertFrom-DateTime -Date $this.Date -Excel -Verbose:$false)
}
# add custom constructor that takes parameters
PFDateFormat([datetime] $Date) {
$this.Date = $Date
$this.DMTF = $(ConvertFrom-DateTime -Date $Date -DMTF -Verbose:$false)
$this.Unix = $(ConvertFrom-DateTime -Date $Date -Unix -Verbose:$false)
$this.FileTime = $(ConvertFrom-DateTime -Date $Date -FileTime -Verbose:$false)
$this.ICSDateTime = $(ConvertFrom-DateTime -Date $Date -ICSDateTime -Verbose:$false)
$this.ISO8601 = $(ConvertFrom-DateTime -Date $Date -ISO8601 -Verbose:$false)
$this.Excel = $(ConvertFrom-DateTime -Date $Date -Excel -Verbose:$false)
}
}
[Flags()]
enum PFUserAccountControl {
SCRIPT = 0x1
ACCOUNTDISABLE = 0x2
HOMEDIR_REQUIRED = 0x8
LOCKOUT = 0x10
PASSWD_NOTREQD = 0x20
PASSWD_CANT_CHANGE = 0x40
ENCRYPTED_TEXT_PASSWORD_ALLOWED = 0x80
TEMP_DUPLICATE_ACCOUNT = 0x100
NORMAL_ACCOUNT = 0x200
INTERDOMAIN_TRUST_ACCOUNT = 0x800
WORKSTATION_TRUST_ACCOUNT = 0x1000
SERVER_TRUST_ACCOUNT = 0x2000
DONT_EXPIRE_PASSWD = 0x10000
MNS_LOGON_ACCOUNT = 0x20000
SMARTCARD_REQUIRED = 0x40000
TRUSTED_FOR_DELEGATION = 0x80000
NOT_DELEGATED = 0x100000
USE_DES_KEY_ONLY = 0x200000
DONT_REQUIRE_PREAUTH = 0x400000
PASSWORD_EXPIRED = 0x800000
TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 0x1000000
ADS_UF_PARTIAL_SECRETS_ACCOUNT = 0x04000000
}
# EOF: PoshFunctions.psm1