Skip to content

Commit 5a0efae

Browse files
committed
New ShouldProcess, and Parsing Native Apps examples
1 parent 5eff9e9 commit 5a0efae

17 files changed

+658
-0
lines changed

Diff for: Console/PSReadLine-Expand-Aliases.ps1

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using namespace System.Management.Automation
2+
using namespace System.Management.Automation.Language
3+
4+
# This example will replace any aliases on the command line with the resolved commands.
5+
$setPSReadLineKeyHandlerSplat = @{
6+
Chord = 'Alt+%'
7+
BriefDescription = 'ExpandAliases'
8+
Description = 'Replace all aliases with the full command'
9+
}
10+
11+
Set-PSReadLineKeyHandler @setPSReadLineKeyHandlerSplat -ScriptBlock {
12+
param($key, $arg)
13+
14+
$ast = $null
15+
$tokens = $null
16+
$errors = $null
17+
$cursor = $null
18+
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$errors, [ref]$cursor)
19+
20+
$startAdjustment = 0
21+
foreach ($token in $tokens) {
22+
if ($token.TokenFlags -band [TokenFlags]::CommandName) {
23+
$alias = $ExecutionContext.InvokeCommand.GetCommand($token.Extent.Text, 'Alias')
24+
if ($alias -ne $null) {
25+
$resolvedCommand = $alias.ResolvedCommandName
26+
if ($resolvedCommand -ne $null) {
27+
$extent = $token.Extent
28+
$length = $extent.EndOffset - $extent.StartOffset
29+
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(
30+
$extent.StartOffset + $startAdjustment,
31+
$length,
32+
$resolvedCommand)
33+
34+
# Our copy of the tokens won't have been updated, so we need to
35+
# adjust by the difference in length
36+
$startAdjustment += ($resolvedCommand.Length - $length)
37+
}
38+
}
39+
}
40+
}
41+
}

Diff for: Debugging/Capture-Debug-Stream-To-PSHost.ps1

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
function TraceFilter {
3+
<#
4+
from the shell even if you redirect debug or even *>&1
5+
it will never see the debug stream
6+
running it like this lets you read it
7+
#>
8+
$TraceScript = {
9+
Trace-Command -Name ParameterBinding -PSHost -Verbose -Expression {
10+
$null | Join-String -sep ', '
11+
}
12+
}
13+
$regex = @{ 'trace' = '^.*tion: 0 :' }
14+
15+
$runspace = [PowerShell]::Create([InitialSessionState]::CreateDefault2())
16+
$stdOut = $runspace.AddScript( $TraceScript ).Invoke()
17+
$runspace.Streams.Debug | ForEach-Object {
18+
$_ -replace $regex.trace, ''
19+
}
20+
}
21+
22+
function RegularTrace {
23+
# same command, using a regular Trace-Command that spams
24+
# <https://gist.github.com/ninmonkey/eb7610c1ddf9ac8e565387a90a0e11a0>
25+
Trace-Command -Name ParameterBinding -PSHost -Verbose -Expression {
26+
$null | Join-String -sep ', '
27+
}
28+
}
29+
30+
# try:
31+
# RegularTrace
32+
# TraceFilter

Diff for: Format/Export-FormatData.ps1

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
$APP_PATH_ROOT = Get-Item -ea stop $PSScriptRoot
2+
3+
function _exportFormatData {
4+
<#
5+
.synopsis
6+
export formatting and generate file to load from if not existing
7+
#>
8+
param(
9+
# typename
10+
[Parameter(mandatory, valuefromPipeline)]
11+
[string]$TypeName
12+
)
13+
14+
$SubDir = 'format.ps1xml'
15+
Write-Verbose "Type: '$TypeName'"
16+
if (! (Test-Path "$APP_PATH_ROOT\$SubDir")) {
17+
New-Item -Path "$APP_PATH_ROOT\$SubDir" -ItemType Directory
18+
# throw 'folder'
19+
}
20+
$Path_Original = "$APP_PATH_ROOT\$SubDir\format-$TypeName.ps1xml"
21+
$Path_Custom = "$APP_PATH_ROOT\$SubDir\format-$TypeName-nin.ps1xml"
22+
$Fd = Get-FormatData -TypeName $TypeName -PowerShellVersion 7.1 #-PowerShellVersion 5.1
23+
24+
if ($fd) {
25+
if (! (Test-Path $Path_Original) ) {
26+
Write-Verbose "New type, creating: '$Path_Original'"
27+
# you don't want to export the modified version if already existing
28+
Export-FormatData -InputObject $Fd -Path $Path_Original -IncludeScriptBlock
29+
}
30+
} else {
31+
# Label TypeNameNotFound $TypeName -fg orange
32+
Write-Error "TypeNameNotFound: $TypeName"
33+
}
34+
35+
if (! (Test-Path $Path_Custom) ) {
36+
Write-Verbose "New type, creating: '$Path_Custom'"
37+
Copy-Item -Path $Path_Original -Destination $Path_Custom
38+
}
39+
$customFormat = Get-Item -ea stop $Path_Custom
40+
Update-FormatData -PrependPath $customFormat
41+
Write-Verbose 'success'
42+
}
43+
44+
$typeList = @(
45+
'System.RuntimeType'
46+
'System.IO.FileInfo'
47+
'System.IO.DirectoryInfo'
48+
'System.IO.FileSystemInfo'
49+
) | Sort-Object -Unique
50+
51+
foreach ($name in $typeList) {
52+
_exportFormatData $name -Verbose
53+
}

Diff for: Format/File-Listing/File-Listing-Custom-Icons.ps1xml

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<!--
2+
from
3+
https://gist.github.com/SeeminglyScience/35c67a450367a493e56b0ab46623bf47#file-profile-format-ps1xml
4+
and
5+
https://gist.github.com/ninmonkey/18203a0bc2b9fae38c95d9abcd9e3c12
6+
-->
7+
8+
<Configuration>
9+
<SelectionSets>
10+
<SelectionSet>
11+
<Name>CustomGCI</Name>
12+
<Types>
13+
<TypeName>System.IO.FileSystemInfo</TypeName>
14+
<TypeName>System.IO.FileInfo</TypeName>
15+
<TypeName>System.IO.DirectoryInfo</TypeName>
16+
</Types>
17+
</SelectionSet>
18+
</SelectionSets>
19+
<ViewDefinitions>
20+
<View>
21+
<Name>CustomGci</Name>
22+
<ViewSelectedBy>
23+
<SelectionSetName>CustomGCI</SelectionSetName>
24+
</ViewSelectedBy>
25+
<Controls>
26+
<Control>
27+
<Name>FSIGroup</Name>
28+
<CustomControl>
29+
<CustomEntries>
30+
<CustomEntry>
31+
<CustomItem>
32+
<Frame>
33+
<LeftIndent>4</LeftIndent>
34+
<CustomItem>
35+
<Text> Directory: </Text>
36+
<ExpressionBinding>
37+
<ScriptBlock>
38+
$_.PSParentPath.Replace('Microsoft.PowerShell.Core\FileSystem::', '')
39+
</ScriptBlock>
40+
</ExpressionBinding>
41+
<NewLine />
42+
</CustomItem>
43+
</Frame>
44+
</CustomItem>
45+
</CustomEntry>
46+
</CustomEntries>
47+
</CustomControl>
48+
</Control>
49+
</Controls>
50+
<GroupBy>
51+
<PropertyName>PSParentPath</PropertyName>
52+
<CustomControlName>FSIGroup</CustomControlName>
53+
</GroupBy>
54+
<TableControl>
55+
<TableHeaders>
56+
<TableColumnHeader>
57+
<Alignment>Left</Alignment>
58+
<Label>Mode</Label>
59+
<Width>4</Width>
60+
</TableColumnHeader>
61+
<TableColumnHeader>
62+
<Alignment>Right</Alignment>
63+
<Label>LastWriteTime</Label>
64+
<Width>26</Width>
65+
</TableColumnHeader>
66+
<TableColumnHeader>
67+
<Alignment>Right</Alignment>
68+
<Label>Length</Label>
69+
<Width>10</Width>
70+
</TableColumnHeader>
71+
<TableColumnHeader>
72+
<Alignment>Left</Alignment>
73+
<Label>Name</Label>
74+
</TableColumnHeader>
75+
</TableHeaders>
76+
<TableRowEntries>
77+
<TableRowEntry>
78+
<TableColumnItems>
79+
<TableColumnItem>
80+
<ScriptBlock>
81+
$text = (&amp; {
82+
$mode = $_.Mode
83+
if ($mode[0] -eq 'l') {
84+
# nf-fa-link
85+
[char]0xf0c1
86+
} elseif ($mode[0] -eq 'd') {
87+
# nf-mdi-folder
88+
[char]0xf74a
89+
} else {
90+
# nf-fa-file
91+
[char]0xf15b
92+
}
93+
94+
if ($mode[1] -eq 'a') {
95+
# nf-mdi-archive
96+
[char]0xf53b
97+
}
98+
99+
if ($mode[2] -eq 'r') {
100+
# nf-fa-lock
101+
[char]0xf023
102+
}
103+
104+
if ($mode[3] -eq 'h') {
105+
# nf-mdi-file-hidden
106+
[char]0xfb12
107+
}
108+
109+
if ($mode[4] -eq 's') {
110+
# nf-custom-windows
111+
[char]0xe62a
112+
}
113+
}) -join ' '
114+
115+
return "$([char]27)[38;2;255;153;51m$text$([char]27)[0m"
116+
</ScriptBlock>
117+
</TableColumnItem>
118+
<TableColumnItem>
119+
<ScriptBlock>
120+
[string]::Format(
121+
"{2}{0,10} {1,8}{3}",
122+
$_.LastWriteTime.ToString("d"),
123+
$_.LastWriteTime.ToString("t"),
124+
"$([char]27)[38;2;102;153;255m",
125+
"$([char]27)[0m")
126+
</ScriptBlock>
127+
</TableColumnItem>
128+
<TableColumnItem>
129+
<ScriptBlock>
130+
if ($_ -is [IO.DirectoryInfo]) {
131+
return ''
132+
}
133+
134+
if ($_.Length -gt 1GB) {
135+
return '{1}{0:N2}GB{2}' -f (
136+
($_.Length / 1GB),
137+
"$([char]27)[38;2;255;153;0m",
138+
"$([char]27)[0m")
139+
}
140+
141+
if ($_.Length -gt 1MB) {
142+
return '{1}{0:N2}MB{2}' -f (
143+
($_.Length / 1MB),
144+
"$([char]27)[38;2;255;255;0m",
145+
"$([char]27)[0m")
146+
}
147+
148+
if ($_.Length -gt 1KB) {
149+
return '{1}{0:N2}KB{2}' -f (
150+
($_.Length / 1KB),
151+
"$([char]27)[38;2;153;255;51m",
152+
"$([char]27)[0m")
153+
}
154+
155+
return '{1}{0:N2}B{2}' -f (
156+
$_.Length,
157+
"$([char]27)[38;2;0;255;51m",
158+
"$([char]27)[0m")
159+
</ScriptBlock>
160+
</TableColumnItem>
161+
<TableColumnItem>
162+
<ScriptBlock>
163+
'{1}{0}{2}' -f (
164+
$_.Name,
165+
"$([char]27)[38;2;102;204;255m",
166+
"$([char]27)[0m")
167+
</ScriptBlock>
168+
</TableColumnItem>
169+
</TableColumnItems>
170+
</TableRowEntry>
171+
</TableRowEntries>
172+
</TableControl>
173+
</View>
174+
</ViewDefinitions>
175+
</Configuration>

Diff for: Format/File-Listing/Readme.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Your format file will have a section like this
2+
3+
```html
4+
<TableColumnItem>
5+
<PropertyName>Mode</PropertyName>
6+
</TableColumnItem>
7+
```
8+
9+
You can replace it with a `<ScriptBlock>`
10+
11+
```html
12+
<TableColumnItem>
13+
<ScriptBlock>
14+
[string]$textAcc = ''
15+
switch($_.Mode[0]) {
16+
'd' {
17+
$textAcc += '📁'
18+
continue;
19+
}
20+
'l' {
21+
$textAcc += 'lnk'
22+
continue;
23+
}
24+
default {
25+
$textACC += '📄'
26+
continue;
27+
}
28+
}
29+
return $textAcc -join ''
30+
</ScriptBlock>
31+
</TableColumnItem>
32+
```
33+
34+
# Docs
35+
36+
- [about_Format.ps1xml](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_format.ps1xml?view=powershell-7.1)
37+
- [about_Types.ps1xml](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_types.ps1xml?view=powershell-7.1)
38+
- [Export-FormatData](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-formatdata?view=powershell-7)
39+
40+
[this file at gist](https://gist.github.com/ninmonkey/18203a0bc2b9fae38c95d9abcd9e3c12)
41+
42+
# Utilities
43+
44+
- <https://github.com/StartAutomating/EZOut>

Diff for: Format/ShouldProcess-FrontHeader/Readme.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
[↑ Go Back](./..)
3+
4+
[ShouldProcess-FrontHeader.ps1](ShouldProcess-FrontHeader.ps1)
5+
6+
![ShouldProcess Screenshot](ShouldProcess-FrontHeader-SeeminglySci.png)
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<#
2+
from: <https://github.com/SeeminglyScience>
3+
4+
#>
5+
6+
function Test-ShouldProcess {
7+
[cmdletbinding(SupportsShouldProcess, PositionalBinding = $false)]
8+
param()
9+
10+
end {
11+
$null = $PSCmdlet.ShouldProcess(
12+
'description',
13+
'warning',
14+
'caption'
15+
)
16+
}
17+
}
18+
19+
Test-ShouldProcess -WhatIf
20+
21+
Test-ShouldProcess -Confirm
46.4 KB
Loading

0 commit comments

Comments
 (0)