Skip to content

Commit 6e6cc2d

Browse files
see changelog for v1.5.0
1 parent 6f59f8f commit 6e6cc2d

11 files changed

Lines changed: 1974 additions & 49 deletions

Get-MyVariable2.ps1

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
Function Get-MyVariable {
3+
4+
5+
[cmdletbinding()]
6+
[OutputType([System.Management.Automation.PSVariable])]
7+
[Alias("gmv")]
8+
9+
Param(
10+
[Parameter(Position = 0)]
11+
[ValidateSet("Global", "Local", "Script", "Private", 0, 1, 2, 3)]
12+
[ValidateNotNullOrEmpty()]
13+
[string]$Scope = "Global",
14+
[switch]$NoTypeInformation
15+
)
16+
17+
if ($psise) {
18+
Write-Warning "This function is not designed for the PowerShell ISE."
19+
}
20+
else {
21+
Write-Verbose "Getting system defined variables"
22+
#get all global variables from PowerShell with no profiles
23+
$psvariables = powershell -noprofile -COMMAND "& {GET-VARIABLE | select -expandproperty name}"
24+
25+
Write-Verbose "Found $($psvariables.count) initial state variables"
26+
27+
<#
28+
find all the variables where the name isn't in the variable we just created
29+
and also isn't a system variable generated after the shell has been running
30+
and also any from this function
31+
#>
32+
33+
Write-Verbose "Getting current variables in $Scope scope"
34+
$variables = Get-Variable -Scope $Scope
35+
36+
Write-Verbose "Found $($variables.count)"
37+
Write-Verbose "Filtering variables"
38+
39+
#define variables to also exclude
40+
$skip = "LastExitCode|_|PSScriptRoot|skip|PSCmdlet|psvariables|variables|Scope|this"
41+
42+
#filter out some automatic variables
43+
$filtered = $variables | Where-object {$psvariables -notcontains $_.name -AND $_.name -notmatch $skip}
44+
45+
if ($NoTypeInformation) {
46+
#write results with not object types
47+
$filtered
48+
}
49+
else {
50+
#add type information for each variable
51+
Write-Verbose "Adding value type"
52+
$filtered | Select-Object Name, Value, @{Name = "Type"; Expression = {$_.Value.GetType().Name}}
53+
}
54+
55+
Write-Verbose "Finished getting my variables"
56+
57+
}
58+
} #end function
59+
60+
61+
62+

New-PSDriveHere.ps1

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
Function New-PSDriveHere {
2+
3+
[cmdletBinding(SupportsShouldProcess = $True, DefaultParameterSetName = "Folder")]
4+
[OutputType([System.Management.Automation.PSDriveInfo])]
5+
[Alias("npsd")]
6+
7+
Param(
8+
[Parameter(Position = 0)]
9+
[ValidateScript( {Test-Path $_})]
10+
[string]$Path = ".",
11+
12+
[Parameter(Position = 1, ParameterSetName = "Name")]
13+
[ValidateNotNullorEmpty()]
14+
[string]$Name,
15+
16+
[Parameter(ParameterSetName = "Folder")]
17+
[switch]$First,
18+
19+
[Alias("cd")]
20+
[switch]$SetLocation
21+
)
22+
23+
Write-Verbose "Starting: $($MyInvocation.Mycommand)"
24+
25+
Write-Verbose "Getting the location for $path"
26+
#get the specified location
27+
$location = Get-Item -Path $path
28+
29+
#did the user specify a name?
30+
if ($pscmdlet.ParameterSetName -eq "Name") {
31+
Write-Verbose "Defining a new PSDrive with the name $Name."
32+
} #if $name
33+
else {
34+
if ($first) {
35+
Write-Verbose "Using the first word in the target location."
36+
$pattern = "^\w+"
37+
}
38+
else {
39+
Write-Verbose "Using the last word in the target location."
40+
$pattern = "\w+$"
41+
}
42+
#Make sure name contains valid characters. This function
43+
#should work for all but the oddest named folders.
44+
45+
if ($location.Name -match $pattern) {
46+
$name = $matches[0]
47+
}
48+
else {
49+
#The location has something odd about it so bail out
50+
Write-Warning "$path doesn't meet the criteria"
51+
Break
52+
}
53+
54+
} #else using part of folder name
55+
56+
#verify a PSDrive doesn't already exist
57+
Write-Verbose "Testing $($name):"
58+
59+
If (-not (Test-Path -path "$($name):")) {
60+
Write-Verbose "Creating PSDrive for $name"
61+
$paramHash = @{
62+
Name = $name
63+
PSProvider = $location.PSProvider
64+
Root = $Path
65+
Description = "Created $(get-date)"
66+
Scope = 'Global'
67+
}
68+
69+
New-PSDrive @paramHash
70+
71+
if ($SetLocation) {
72+
Write-Verbose "Setting location to $($name):"
73+
Set-Location -Path "$($name):"
74+
}
75+
}
76+
else {
77+
Write-Warning "A PSDrive for $name already exists"
78+
}
79+
80+
Write-Verbose "Ending: $($MyInvocation.Mycommand)"
81+
82+
} #function
83+
84+
85+

PSScriptTools.psd1

244 Bytes
Binary file not shown.

README.md

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# ![](./images/toolbox-thumbnail.png) PSScriptTools
1+
# ![toolbox](./images/toolbox-thumbnail.png) PSScriptTools
22

33
This PowerShell module contains a number of functions you might use to enhance your own functions and scripts. The [Samples](./samples) folder contains demonstration script files.
44

55
## Current Release
66

7-
The current release is [PSScriptTools-v1.4.0](https://github.com/jdhitsolutions/PSScriptTools/archive/v1.4.0.zip)
7+
The current release is [PSScriptTools-v1.5.0](https://github.com/jdhitsolutions/PSScriptTools/archive/v1.5.0.zip)
88

99
You can also install this from the PowerShell Gallery:
1010

@@ -280,7 +280,7 @@ The output will also show the median and trimmed values as well as some metadata
280280
```powershell
281281
PS C:\> $cred = Get-credential globomantics\administrator
282282
PS C:\> Test-Expression {param($cred) get-wmiobject win32_logicaldisk -computer chi-dc01 -credential $cred } -argumentList $cred
283-
283+
284284
Tests : 1
285285
TestInterval : 0.5
286286
AverageMS : 1990.6779
@@ -425,7 +425,6 @@ Convert a hashtable object to a string equivalent that you can copy into your sc
425425

426426
This command will take an object and create a hashtable based on its properties. You can have the hashtable exclude some properties as well as properties that have no value.
427427

428-
429428
```powershell
430429
PS C:\> get-process -id $pid | select name,id,handles,workingset | ConvertTo-HashTable
431430
@@ -460,8 +459,58 @@ Computer HAL
460459
Count 3
461460
```
462461

462+
## Select Functions
463+
464+
The module contains 2 functions which simplify the use of `Select-Object`. The commands are intended to make it easier to select the first or last X number of objects. The commands include features so that you can sort the incoming objects on a given property first.
465+
466+
```powershell
467+
PS C:\> get-process | select-first 5 -Property WS -Descending
468+
469+
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
470+
------- ------ ----- ----- ------ -- -- -----------
471+
696 89 615944 426852 391.97 7352 0 sqlservr
472+
541 78 262532 274576 278.41 6208 8 Code
473+
1015 70 227824 269504 137.39 16484 8 powershell_ise
474+
1578 111 204852 254640 98.58 21332 8 firefox
475+
884 44 221872 245712 249.23 12456 8 googledrivesync
476+
```
477+
478+
## New-PSDriveHere
479+
480+
This function will create a new PSDrive at the specified location. The default is the current location, but you
481+
can specify any PSPath. The function will take the last word of the path and use it as the name of the new
482+
PSDrive.
483+
484+
```powershell
485+
PS C:\users\jeff\documents\Enterprise Mgmt Webinar> new-psdrivehere
486+
487+
Name Used (GB) Free (GB) Provider Root CurrentLocation
488+
---- --------- --------- -------- ---- ---------------
489+
Webinar 146.57 FileSystem C:\users\jeff\Documents\Enter...
490+
```
491+
492+
## Get-MyVariable
493+
494+
This function will return all variables not defined by PowerShell or by this function itself. The default is to
495+
return all user-created variables from the global scope but you can also specify a scope such as script, local or
496+
a number 0 through 5.
497+
498+
```powershell
499+
PS C:\> Get-MyVariable
500+
501+
NName Value Type
502+
---- ----- ----
503+
a bits ServiceController
504+
dt 10/22/2018 10:49:38 AM DateTime
505+
foo 123 Int32
506+
r {1, 2, 3, 4...} Object[]
507+
...
508+
```
509+
510+
Depending on the value and how PowerShell chooses to display it, you may not see the type.
511+
463512
## Compatibility
464513

465514
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.
466515

467-
*last updated 12 October 2018*
516+
*last updated 22 October 2018*

SelectFunctions.ps1

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
Function Select-First {
2+
3+
4+
[CmdletBinding(DefaultParameterSetName = 'DefaultParameter')]
5+
[Alias("First")]
6+
param(
7+
[Parameter(
8+
ParameterSetName = 'DefaultParameter',
9+
Mandatory,
10+
ValueFromPipeline )]
11+
[psobject]$InputObject,
12+
13+
[Parameter(
14+
ParameterSetName = 'DefaultParameter',
15+
Position = 0,
16+
Mandatory,
17+
HelpMessage = "How many items do you want to select?")]
18+
[ValidateRange(0, 2147483647)]
19+
[int]$First,
20+
21+
[Parameter(
22+
Position = 1,
23+
ParameterSetName = 'DefaultParameter')]
24+
[ValidateNotNullOrEmpty()]
25+
[string]$Property,
26+
27+
[Parameter(ParameterSetName = 'DefaultParameter')]
28+
[ValidateRange(0, 2147483647)]
29+
[int]$Skip = 0,
30+
31+
[Parameter(ParameterSetName = 'DefaultParameter')]
32+
[switch]$Descending
33+
)
34+
35+
Begin {
36+
37+
Write-Verbose "[BEGIN ] Starting $($MyInvocation.Mycommand)"
38+
Write-Verbose "[BEGIN ] Using parameter set $($PSCmdlet.ParameterSetName)"
39+
if ($PSBoundParameters.ContainsKey("Property")) {
40+
$sortParams = @{Property = $Property}
41+
write-verbose "Sorting on $Property"
42+
}
43+
if ($PSBoundParameters.ContainsKey("Descending")) {
44+
if ($sortParams) {
45+
$sortParams.Add("Descending", $True)
46+
}
47+
else {
48+
#it is possible to sort without a property say on a string or number
49+
$sortParams = @{"Descending" = $True}
50+
}
51+
}
52+
53+
$data = @()
54+
55+
Write-Verbose "Selecting first $First, skipping $skip."
56+
57+
} #begin
58+
59+
Process {
60+
61+
#save input objects so they can be sorted
62+
$data += $InputObject
63+
64+
} #process
65+
66+
End {
67+
If ($sortparams) {
68+
Write-Verbose ($sortParams | out-string)
69+
$data = $data | Sort-Object @sortparams
70+
}
71+
$data | Microsoft.PowerShell.Utility\Select-object -First $first -Skip $skip
72+
73+
Write-Verbose "[END ] Ending $($MyInvocation.Mycommand)"
74+
75+
} #end
76+
77+
} #end function Select-First
78+
79+
Function Select-Last {
80+
81+
82+
[CmdletBinding(DefaultParameterSetName = 'DefaultParameter')]
83+
[Alias("Last")]
84+
param(
85+
[Parameter(
86+
ParameterSetName = 'DefaultParameter',
87+
Mandatory,
88+
ValueFromPipeline )]
89+
[psobject]$InputObject,
90+
91+
[Parameter(
92+
ParameterSetName = 'DefaultParameter',
93+
Position = 0,
94+
Mandatory,
95+
HelpMessage = "How many items do you want to select?")]
96+
[ValidateRange(0, 2147483647)]
97+
[int]$Last,
98+
99+
[Parameter(
100+
Position = 1,
101+
ParameterSetName = 'DefaultParameter')]
102+
[ValidateNotNullOrEmpty()]
103+
[string]$Property,
104+
105+
[Parameter(ParameterSetName = 'DefaultParameter')]
106+
[ValidateRange(0, 2147483647)]
107+
[int]$Skip = 0,
108+
109+
[Parameter(ParameterSetName = 'DefaultParameter')]
110+
[switch]$Descending
111+
)
112+
113+
Begin {
114+
115+
Write-Verbose "[BEGIN ] Starting $($MyInvocation.Mycommand)"
116+
Write-Verbose "[BEGIN ] Using parameter set $($PSCmdlet.ParameterSetName)"
117+
if ($PSBoundParameters.ContainsKey("Property")) {
118+
$sortParams = @{Property = $Property}
119+
write-verbose "Sorting on $Property"
120+
}
121+
if ($PSBoundParameters.ContainsKey("Descending")) {
122+
if ($sortParams) {
123+
$sortParams.Add("Descending", $True)
124+
}
125+
else {
126+
#it is possible to sort without a property say on a string or number
127+
$sortParams = @{"Descending" = $True}
128+
}
129+
}
130+
131+
$data = @()
132+
133+
Write-Verbose "Selecting Last $Last, skipping $skip."
134+
135+
} #begin
136+
137+
Process {
138+
139+
#save input objects so they can be sorted
140+
$data += $InputObject
141+
142+
} #process
143+
144+
End {
145+
If ($sortparams) {
146+
Write-Verbose ($sortParams | out-string)
147+
$data = $data | Sort-Object @sortparams
148+
}
149+
$data | Microsoft.PowerShell.Utility\Select-object -Last $Last -Skip $skip
150+
151+
Write-Verbose "[END ] Ending $($MyInvocation.Mycommand)"
152+
153+
} #end
154+
155+
} #end function Select-Last
156+
157+
158+

0 commit comments

Comments
 (0)