-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNiceties.ps1
112 lines (95 loc) · 4.05 KB
/
Niceties.ps1
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
#Test a TCP port's connnection status
[System.Net.Sockets.TCPClient]::New('127.0.0.1',1234).Connected
# -or for PowerShell v2.0-
([System.Activator]::CreateInstance([System.Net.Sockets.TcpClient],@('127.0.0.1',1234))).Connected
#Always know your script location regardless of version
If(!$PSScriptRoot){$PSScriptRoot = (Split-Path -Parent $MyInvocation.MyCommand.Defenition)}
#The shortest and fastest way I know of to add a procname to the end of a netstat in powershell
$X=@{};PS|%{$X[$_.Id]=$_.Name};Netstat -ano|Select -Skip 4|%{"$_="+$X.[Int]$_.Substring(71)}
#Circumvent the Set-ExecutionPolicy
Powershell.exe -Command "[ScriptBlock]::Create((Get-Content 'PATHTOSCRIPT.ps1')).Invoke()"
#True Press any key to continue (essentially PAUSE from CMD). This won't work in ISE.
Write-Host 'Press any key to continue...';[Void]$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
#Make any powershell code that uses cmdlets backwards compatible
$MainBlock = {
# Code goes here
}
If($(Try{[Void][PSObject]::New()}Catch{$True})){
$MainBlock = ($MainBlock.toString().Split([System.Environment]::NewLine) | %{
$FlipFlop = $True
}{
If($FlipFLop){$_}
$FlipFlop = !$FlipFlop
} | %{
If($_ -match '::New\('){
(($_.Split('[')[0]+'(New-Object '+$_.Split('[')[-1]+')') -replace ']::New',' -ArgumentList ').Replace(' -ArgumentList ()','')
}Else{
$_
}
}) -join [System.Environment]::NewLine
}
$MainBlock = [ScriptBlock]::Create($MainBlock)
$MainBlock.Invoke()
# Find the pointer for any variable in memory, not particularly useful for normal PowerShell, but interesting nonetheless
# This is from here https://stackoverflow.com/questions/4994277/memory-address-of-an-object-in-c-sharp
Add-Type -TypeDefinition '
using System;
using System.Runtime.InteropServices;
namespace Pointers{
public static class AddressHelper
{
private static object mutualObject;
private static ObjectReinterpreter reinterpreter;
static AddressHelper()
{
AddressHelper.mutualObject = new object();
AddressHelper.reinterpreter = new ObjectReinterpreter();
AddressHelper.reinterpreter.AsObject = new ObjectWrapper();
}
public static IntPtr GetAddress(object obj)
{
lock (AddressHelper.mutualObject)
{
AddressHelper.reinterpreter.AsObject.Object = obj;
IntPtr address = AddressHelper.reinterpreter.AsIntPtr.Value;
AddressHelper.reinterpreter.AsObject.Object = null;
return address;
}
}
public static T GetInstance<T>(IntPtr address)
{
lock (AddressHelper.mutualObject)
{
AddressHelper.reinterpreter.AsIntPtr.Value = address;
return (T)AddressHelper.reinterpreter.AsObject.Object;
}
}
// I bet you thought C# was type-safe.
[StructLayout(LayoutKind.Explicit)]
private struct ObjectReinterpreter
{
[FieldOffset(0)] public ObjectWrapper AsObject;
[FieldOffset(0)] public IntPtrWrapper AsIntPtr;
}
private class ObjectWrapper
{
public object Object;
}
private class IntPtrWrapper
{
public IntPtr Value;
}
}
}
'
# This one is super ugly, but you can convert a csv to ExpandoObject[]
$Arr = [System.Dynamic.ExpandoObject[]](
(Import-Csv .\FILE.csv) | %{$Count = 0}{
Set-Variable -Name ("tmp"+$Count) -Value ([System.Dynamic.ExpandoObject]::new())
$_.PSObject.Properties | %{
(Get-Variable ("tmp"+$Count) -ValueOnly).$($_.Name) = $_.Value
}
Get-Variable ("tmp"+$Count) -ValueOnly
$Count++
}
)