Skip to content

Commit 3a79ce1

Browse files
v1.2.0
Updated Write-Detail Updated README
1 parent 5d7f6fb commit 3a79ce1

6 files changed

Lines changed: 228 additions & 89 deletions

File tree

PSScriptTools.psd1

0 Bytes
Binary file not shown.

README.md

Lines changed: 82 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ This PowerShell module contains a number of functions you might use to enhance y
66
The current release is [PSScriptTools-v1.0.1](https://github.com/jdhitsolutions/PSScriptTools/archive/v1.0.1.zip)
77

88
You can also install this from the PowerShell Gallery:
9-
```
9+
10+
```powershell
1011
Install-Module PSScriptTools
1112
```
13+
1214
or in PowerShell Core:
13-
```
15+
16+
```powershell
1417
Install-Module PSScriptTools -scope currentuser
1518
```
1619

17-
1820
Please post any questions, problems or feedback in Issues. Any input is greatly appreciated.
1921

2022
## Add-Border
23+
2124
This command will create a character or text based border around a line of text. You might use this to create a formatted text report or to improve the display of information to the screen.
2225

23-
```
26+
```powershell
2427
PS C:\> add-border $env:computername
2528
2629
*************
@@ -29,9 +32,10 @@ PS C:\> add-border $env:computername
2932
```
3033

3134
## Get-PSWho
35+
3236
This command will provide a summary of relevant information for the current user in a PowerShell Session. You might use this to troubleshoot an end-user problem running a script or command.
3337

34-
```
38+
```powershell
3539
PS C:\> Get-PSWho
3640
3741
User : COWPC\Jeff
@@ -46,10 +50,12 @@ WSMan : 3.0
4650
ExecutionPolicy : RemoteSigned
4751
Culture : en-US
4852
```
53+
4954
## New-CustomFileName
55+
5056
This command will generate a custom file name based on a template string that you provide.
5157

52-
```
58+
```powershell
5359
PS C:\> New-CustomFileName %computername_%day%monthname%yr-%time.log
5460
COWPC_28Nov17-142138.log
5561
@@ -59,65 +65,77 @@ Tuesday-3128.dat
5965

6066
You can create a template string using any of these variables. Most of these should be self-explanatory
6167

62-
- %username
68+
- %username
6369
- %computername
64-
- %year - 4 digit year
65-
- %yr - 2 digit year
66-
- %monthname - The abbreviated month name
67-
- %month - The month number
68-
- %dayofweek - The full name of the week day
69-
- %day
70-
- %hour
71-
- %minute
72-
- %time
73-
- %string - A random string
74-
- %guid
70+
- %year - 4 digit year
71+
- %yr - 2 digit year
72+
- %monthname - The abbreviated month name
73+
- %month - The month number
74+
- %dayofweek - The full name of the week day
75+
- %day
76+
- %hour
77+
- %minute
78+
- %time
79+
- %string - A random string
80+
- %guid
7581

7682
You can also insert a random number using %### with a # character for each digit. If you want a 2 digit random number use %##. If you want 6 digits, use %######.
7783

7884
## New-RandomFileName
85+
7986
Create a new random file name. The default is a completely random name including the extension.
80-
```
87+
88+
```powershell
8189
PS C:\> new-randomfilename
8290
fykxecvh.ipw
8391
```
84-
But you can specify an extentions.
85-
```
92+
93+
But you can specify an extension.
94+
95+
```powershell
8696
PS C:\> new-randomfilename -extension dat
8797
emevgq3r.dat
8898
```
99+
89100
Optionally you can create a random file name using the TEMP folder or your HOME folder. On Windows platforms this will default to your Documents folder.
90-
```
101+
102+
```powershell
91103
PS C:\> new-randomfilename -extension log -UseHomeFolder
92104
C:\Users\Jeff\Documents\kbyw4fda.log
93105
```
106+
94107
On Linux machines it will be the home folder.
95-
```
108+
109+
```powershell
96110
PS /mnt/c/scripts> new-randomfilename -home -Extension tmp
97111
/home/jhicks/oces0epq.tmp
98112
```
99113
## Write-Detail
100-
This command is designed to be used within your functions and scripts to make it easier to write a detailed message that you can use as verbose output. The assumption is that you are using an advanced function with a Begin, Process and End scriptblocks. You can create a detailed message to indicate what part of the code is being executed. The output will include a full time stamp, although you can shorten it to be only a time string which includes a millisecond value.
101114

115+
This command is designed to be used within your functions and scripts to make it easier to write a detailed message that you can use as verbose output. The assumption is that you are using an advanced function with a Begin, Process and End scriptblocks. You can create a detailed message to indicate what part of the code is being executed. The output can be configured to include a datetime stamp or just the time.
116+
117+
```powershell
118+
PS C:\> write-detail "Getting file information" -Prefix Process -Date
119+
9/15/2018 11:42:43 [PROCESS] Getting file information
102120
```
103-
PS C:\>write-detail "Getting file information" -Prefix Process -NoDate
104-
02:39:18:4874 [PROCESS] Getting file information
105-
```
121+
106122
In a script you might use it like this:
107-
```
123+
124+
```powershell
108125
Begin {
109-
Write-Detail "Starting $($myinvocation.mycommand)" -Prefix begin | Write-Verbose
126+
Write-Detail "Starting $($myinvocation.mycommand)" -Prefix begin -time | Write-Verbose
110127
$tabs = "`t" * $tab
111-
Write-Detail "Using a tab of $tab" -Prefix BEGIN | Write-Verbose
128+
Write-Detail "Using a tab of $tab" -Prefix BEGIN -time | Write-Verbose
112129
} #begin
113130
```
114131

115132
## Out-VerboseTee
133+
116134
This command is intended to let you see your verbose output and write the verbose messages to a log file. It will only work if the verbose pipeline is enabled, usually when your command is run with -Verbose. This function is designed to be used within your scripts and functions. You either have to hard code a file name or find some other way to define it in your function or control script. You could pass a value as a parameter or set it as a PSDefaultParameterValue.
117135

118136
This command has an alias of Tee-Verbose.
119137

120-
```
138+
```powershell
121139
Begin {
122140
$log = New-RandomFilename -useTemp -extension log
123141
Write-Detail "Starting $($myinvocation.mycommand)" -Prefix begin | Tee-Verbose $log
@@ -126,9 +144,11 @@ Begin {
126144
$data = @()
127145
} #begin
128146
```
147+
129148
When the command is run with -Verbose you will see the verbose output and it will be saved to the specified log file.
130149

131150
## Out-ConditionalColor
151+
132152
This command is designed to take pipeline input and display it in a colorized format,based on a set of conditions. Unlike Write-Host which doesn't write to the pipeline, this command will write to the pipeline.
133153

134154
You can use a simple hashtable to define a color if the given property matches the hashtable key.
@@ -141,11 +161,14 @@ Or you can specify an ordered hashtable for more complex processing.
141161
This command doesn't always work depending on the type of object you pipe to it. The problem appears to be related to the formatting system. Development and testing is ongoing.
142162

143163
## Copy-Command
164+
144165
This command will copy a PowerShell command, including parameters and help to a new user-specified command. You can use this to create a "wrapper" function or to easily create a proxy function. The default behavior is to create a copy of the command complete with the original comment-based help block.
145166

146167
## Format-Functions
168+
147169
A set of simple commands to make it easier to format values.
148-
```
170+
171+
```powershell
149172
PS C:\> format-percent -Value 123.5646MB -total 1GB -Decimal 4
150173
12.0669
151174
PS C:\> format-string "powershell" -Reverse -Case Proper
@@ -155,6 +178,7 @@ PS C:\> format-value 1235465676 -Unit kb
155178
```
156179

157180
## Get-PSLocation
181+
158182
A simple function to get common locations. This can be useful with cross-platform scripting.
159183

160184
![](./images/pslocation-win.png)
@@ -163,15 +187,19 @@ A simple function to get common locations. This can be useful with cross-platfor
163187

164188

165189
## Get-PowerShellEngine
190+
166191
Use this command to quickly get the path to the PowerShell executable. In Windows you should get a result like this:
167-
```
192+
193+
```powershell
168194
PS C:\> Get-PowerShellEngine
169195
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
170196
```
197+
171198
But PowerShell Core is a bit different:
172-
```
199+
200+
```powershell
173201
PS /home/jhicks> Get-PowerShellEngine
174-
/opt/microsoft/powershell/6.0.0-rc/pwsh
202+
/opt/microsoft/powershell/6/pwsh
175203
```
176204

177205
You can also get detailed information.
@@ -182,36 +210,48 @@ You can also get detailed information.
182210
![PowerShell Core on Linux](./images/get-powershellengine3.png)
183211

184212
Results will vary depending on whether you are running Windows PowerShell or PowerShell Core.
213+
185214
## Out-More
215+
186216
This command provides a PowerShell alternative to the cmd.exe MORE command, which doesn't work in the PowerShell ISE. When you have screens of information, you can page it with this function.
187-
```
217+
218+
```powershell
188219
get-service | out-more
189220
```
221+
190222
![](./images/out-more.png)
191223

192224
This also works in PowerShell Core.
193225

194226
## Invoke-InputBox
227+
195228
This function is a graphical replacement for Read-Host. It creates a simple WPF form that you can use to get user input. The value of the text box will be written to the pipeline.
196-
```
229+
230+
```powershell
197231
$name = Invoke-InputBox -Prompt "Enter a user name" -Title "New User Setup"
198232
```
233+
199234
![](./images/ibx-1.png)
200235

201236
You can also capture a secure string.
202-
```
237+
238+
```powershell
203239
Invoke-Inputbox -Prompt "Enter a password for $Name" -AsSecureString -BackgroundColor red
204240
```
241+
205242
![](./images/ibx-2.png)
206243

207244
This example also demonstrates that you can change form's background color.
208245
This function will **not** work in PowerShell Core.
209246

210247
## ToDo
248+
211249
Because this module is intended to make scripting easier for you, it adds options to insert ToDo statements into PowerShell files. If you are using the PowerShell ISE or VS Code and import this module, it will add the capability to insert a line like this:
212-
```
250+
251+
```yaml
213252
# [12/13/2017 16:52:40] TODO: Add parameters
214253
```
254+
215255
In the PowerShell ISE, you will get a new menu under Add-Ons
216256

217257
![](./images/todo-1.png)
@@ -221,9 +261,11 @@ You can use the menu or keyboard shortcut which will launch an input box.
221261
![](./images/todo-2.png)
222262

223263
The comment will be inserted at the current cursor location.
264+
224265
In VS Code, access the command palette (Ctrl+Shift+P) and then "PowerShell: Show Additional Commands from PowerShell Modules". Select "Insert ToDo" from the list and you'll get the same input box. Note that this will only work for PowerShell files.
225266

226267
### Compatibility
268+
227269
Where possible these commands have been tested with PowerShell Core, but not every platform. If you encounter problems,have suggestions or other feedback, please post an issue.
228270

229-
*last updated 14 December 2017*
271+
*last updated 15 September 2018*

Write-Detail.ps1

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
Function Write-Detail {
2-
[cmdletbinding()]
2+
[cmdletbinding(DefaultParameterSetName="Default")]
33
Param(
44
[Parameter(Position = 0, Mandatory)]
5+
[Parameter(ParameterSetName="Default")]
6+
[Parameter(ParameterSetName="Date")]
7+
[Parameter(ParameterSetName="Time")]
8+
[ValidateNotNullorEmpty()]
59
[string]$Message,
6-
[ValidateSet("BEGIN","PROCESS","END")]
10+
11+
[Parameter(ParameterSetName="Default")]
12+
[Parameter(ParameterSetName="Date")]
13+
[Parameter(ParameterSetName="Time")]
714
[string]$Prefix = "PROCESS",
8-
[switch]$NoDate
15+
16+
[Parameter(ParameterSetName="Date")]
17+
[switch]$Date,
18+
[Parameter(ParameterSetName="Time")]
19+
[Switch]$Time
920
)
1021

11-
$pfx = $($Prefix.ToUpper()).PadRight("process".length)
12-
if ($Nodate) {
22+
#$pfx = $($Prefix.ToUpper()).PadRight("process".length)
23+
if ($time) {
1324
$dt = (Get-Date -Format "hh:mm:ss:ffff")
1425
}
26+
elseif ($Date) {
27+
$dt = "{0} {1}" -f (Get-Date).ToShortDateString(),(Get-Date -Format "hh:mm:ss")
28+
}
29+
30+
if ($PSCmdlet.ParameterSetName -eq 'Default') {
31+
$Text = "[$($prefix.ToUpper())] $Message"
32+
33+
}
1534
else {
16-
$dt = "{0} {1}" -f (Get-Date).ToShortDateString(),(Get-Date -Format "hh:mm:ss:ffff")
35+
$Text = "$dt [$($prefix.toUpper())] $Message"
1736
}
18-
$Text = "$dt [$pfx] $Message"
19-
Write-Output $Text
37+
$Text
2038

2139
} #close Write-Detail
2240

changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#Changelog for PSScriptTools
22

3+
v1.2.0
4+
Updated Write-Detail
5+
Updated README
6+
37
v1.1.0
48
Cleaned up ToDo code (Issue #12)
59
Updated README

0 commit comments

Comments
 (0)