-
Notifications
You must be signed in to change notification settings - Fork 39
/
unix.ps1
192 lines (157 loc) · 5.29 KB
/
unix.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
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
# Just a couple of things (sed, to interpret sed scripts) from http://unxutils.sourceforge.net/
Add-PathVariable "${env:ProgramFiles}\UnxUtils"
# Note PSReadLine uses vi keybindings by default. If you want emacs enable:
# Set-PSReadlineOption -EditMode Emacs
# I like vi keybindings, so I just add my favourite one from emacs
# See https://github.com/lzybkr/PSReadLine#usage
Set-PSReadlineKeyHandler -Key 'Escape,_' -Function YankLastArg
# Change how powershell does tab completion
# http://stackoverflow.com/questions/39221953/can-i-make-powershell-tab-complete-show-me-all-options-rather-than-picking-a-sp
Set-PSReadlineKeyHandler -Chord Tab -Function MenuComplete
# For dig, host, etc.
Add-PathVariable "${env:ProgramFiles}\ISC BIND 9\bin"
# Should really be name=value like Unix version of export but not a big deal
function export($name, $value) {
set-item -force -path "env:$name" -value $value;
}
function pkill($name) {
get-process $name -ErrorAction SilentlyContinue | stop-process
}
function pgrep($name) {
get-process $name
}
# Like Unix touch, creates new files and updates time on old ones
# PSCX has a touch, but it doesn't make empty files
Remove-Alias touch
function touch($file) {
if ( Test-Path $file ) {
Set-FileTime $file
} else {
New-Item $file -type file
}
}
# From https://stackoverflow.com/questions/894430/creating-hard-and-soft-links-using-powershell
function ln($target, $link) {
New-Item -ItemType SymbolicLink -Path $link -Value $target
}
set-alias new-link ln
# http://stackoverflow.com/questions/39148304/fuser-equivalent-in-powershell/39148540#39148540
function fuser($relativeFile){
$file = Resolve-Path $relativeFile
write-output "Looking for processes using $file"
foreach ( $Process in (Get-Process)) {
foreach ( $Module in $Process.Modules) {
if ( $Module.FileName -like "$file*" ) {
$Process | select-object id, path
}
}
}
}
# https://gallery.technet.microsoft.com/WHOIS-PowerShell-Function-ed69fde6
Unblock-File $PSScriptRoot\whois.ps1
. $PSScriptRoot\whois.ps1
function uptime {
Get-CimInstance Win32_OperatingSystem | select-object csname, @{LABEL='LastBootUpTime';
EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
}
function df {
get-volume
}
# Todo: look at 'edit-file' from PSCX
# Repalced with real 'sed' to interpret sed scripts
# function sed($file, $find, $replace){
# (Get-Content $file).replace("$find", $replace) | Set-Content $file
# }
# Like a recursive sed
function edit-recursive($filePattern, $find, $replace) {
$files = get-childitem . "$filePattern" -rec # -Exclude
write-output $files
foreach ($file in $files) {
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace "$find", "$replace" } |
Set-Content $file.PSPath
}
}
function grep($regex, $dir) {
if ( $dir ) {
get-childitem $dir | select-string $regex
return
}
$input | select-string $regex
}
function grepv($regex) {
$input | where-object { !$_.Contains($regex) }
}
function show-links($dir){
get-childitem $dir | where-object {$_.LinkType} | select-object FullName,LinkType,Target
}
function which($name) {
Get-Command $name | Select-Object -ExpandProperty Definition
}
function cut(){
foreach ($part in $input) {
$line = $part.ToString();
$MaxLength = [System.Math]::Min(200, $line.Length)
$line.subString(0, $MaxLength)
}
}
function Private:file($file) {
$extension = (Get-Item $file).Extension
$fileType = (get-itemproperty "Registry::HKEY_Classes_root\$extension")."(default)"
$description = (get-itemproperty "Registry::HKEY_Classes_root\$fileType")."(default)"
write-output $description
}
# From https://github.com/Pscx/Pscx
function sudo(){
Invoke-Elevated @args
}
# https://gist.github.com/aroben/5542538
function pstree {
$ProcessesById = @{}
foreach ($Process in (Get-WMIObject -Class Win32_Process)) {
$ProcessesById[$Process.ProcessId] = $Process
}
$ProcessesWithoutParents = @()
$ProcessesByParent = @{}
foreach ($Pair in $ProcessesById.GetEnumerator()) {
$Process = $Pair.Value
if (($Process.ParentProcessId -eq 0) -or !$ProcessesById.ContainsKey($Process.ParentProcessId)) {
$ProcessesWithoutParents += $Process
continue
}
if (!$ProcessesByParent.ContainsKey($Process.ParentProcessId)) {
$ProcessesByParent[$Process.ParentProcessId] = @()
}
$Siblings = $ProcessesByParent[$Process.ParentProcessId]
$Siblings += $Process
$ProcessesByParent[$Process.ParentProcessId] = $Siblings
}
function Show-ProcessTree([UInt32]$ProcessId, $IndentLevel) {
$Process = $ProcessesById[$ProcessId]
$Indent = " " * $IndentLevel
if ($Process.CommandLine) {
$Description = $Process.CommandLine
} else {
$Description = $Process.Caption
}
Write-Output ("{0,6}{1} {2}" -f $Process.ProcessId, $Indent, $Description)
foreach ($Child in ($ProcessesByParent[$ProcessId] | Sort-Object CreationDate)) {
Show-ProcessTree $Child.ProcessId ($IndentLevel + 4)
}
}
Write-Output ("{0,6} {1}" -f "PID", "Command Line")
Write-Output ("{0,6} {1}" -f "---", "------------")
foreach ($Process in ($ProcessesWithoutParents | Sort-Object CreationDate)) {
Show-ProcessTree $Process.ProcessId 0
}
}
function find-file($name) {
get-childitem -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | foreach-object {
write-output($PSItem.FullName)
}
}
set-alias find find-file
set-alias find-name find-file
function reboot {
shutdown /r /t 0
}