Skip to content

Commit 8dace18

Browse files
author
Lee Holmes
committed
Initial commit
1 parent 6e15878 commit 8dace18

26 files changed

+3558
-0
lines changed

Inventory.ps1

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
##############################################################################
2+
##
3+
## Inventory
4+
##
5+
## From Windows PowerShell Cookbook (O'Reilly)
6+
## by Lee Holmes (http://www.leeholmes.com/guide)
7+
##
8+
##############################################################################
9+
10+
<#
11+
12+
.SYNOPSIS
13+
14+
Serves as the configuration script for a custom remoting endpoint that
15+
exposes only the Get-Inventory custom command.
16+
17+
.EXAMPLE
18+
19+
PS >Register-PsSessionConfiguration Inventory `
20+
-StartupScript 'C:\Program Files\Endpoints\Inventory.ps1'
21+
PS >Enter-PsSession leeholmes1c23 -ConfigurationName Inventory
22+
23+
[leeholmes1c23]: [Inventory] > Get-Command
24+
25+
CommandType Name Definition
26+
----------- ---- ----------
27+
Function Exit-PSSession [CmdletBinding()]...
28+
Function Get-Command [CmdletBinding()]...
29+
Function Get-FormatData [CmdletBinding()]...
30+
Function Get-Help [CmdletBinding()]...
31+
Function Get-Inventory ...
32+
Function Measure-Object [CmdletBinding()]...
33+
Function Out-Default [CmdletBinding()]...
34+
Function prompt ...
35+
Function Select-Object [CmdletBinding()]...
36+
37+
[leeholmes1c23]: [Inventory] > Get-Inventory
38+
39+
SystemDirectory : C:\Windows\system32
40+
Organization :
41+
BuildNumber : 6002
42+
RegisteredUser : Lee Holmes
43+
SerialNumber : 89580-433-1295803-71477
44+
Version : 6.0.6002
45+
46+
[leeholmes1c23]: [Inventory] > 1+1
47+
The syntax is not supported by this runspace. This might be because it is
48+
in no-language mode.
49+
+ CategoryInfo :
50+
+ FullyQualifiedErrorId : ScriptsNotAllowed
51+
52+
[leeholmes1c23]: [Inventory] > Exit-PsSession
53+
PS >
54+
55+
#>
56+
57+
Set-StrictMode -Off
58+
59+
## Create a new function to get inventory
60+
function Get-Inventory
61+
{
62+
Get-WmiObject Win32_OperatingSystem
63+
}
64+
65+
## Customize the prompt
66+
function Prompt
67+
{
68+
"[Inventory] > "
69+
}
70+
71+
## Remember which functions we want to expose to the user
72+
$exportedCommands = "Get-Inventory","Prompt"
73+
74+
## The System.Management.Automation.Runspaces.InitialSessionState class
75+
## has a CreateRestricted() method that creates a default locked-down
76+
## secure configuration for a remote session. This configuration only
77+
## supports the bare minimum required for interactive remoting.
78+
$issType = [System.Management.Automation.Runspaces.InitialSessionState]
79+
$iss = $issType::CreateRestricted("RemoteServer")
80+
81+
## Add the commands to a hashtable so that we can access them easily
82+
$issHashtable = @{}
83+
foreach($command in $iss.Commands)
84+
{
85+
$issHashtable[$command.Name + "-" + $command.CommandType] = $command
86+
}
87+
88+
## Go through all of functions built into the restricted runspace and add
89+
## them to this session. These are proxy functions to limit the functionality
90+
## of commands that we need (such as Get-Command, Select-Object, etc.)
91+
foreach($function in $iss.Commands |
92+
Where-Object { $_.CommandType -eq "Function" })
93+
{
94+
Set-Content "function:\$($function.Name)" -Value $function.Definition
95+
}
96+
97+
## Go through all of the commands in this session
98+
foreach($command in Get-Command)
99+
{
100+
## If it was one of our exported commands, keep it Public
101+
if($exportedCommands -contains $command.Name) { continue }
102+
103+
## If the current command is defined as Private in the initial session
104+
## state, mark it as private here as well.
105+
$issCommand = $issHashtable[$command.Name + "-" + $command.CommandType]
106+
if((-not $issCommand) -or ($issCommand.Visibility -ne "Public"))
107+
{
108+
$command.Visibility = "Private"
109+
}
110+
}
111+
112+
## Finally, prevent all access to the PowerShell language
113+
$executionContext.SessionState.Scripts.Clear()
114+
$executionContext.SessionState.Applications.Clear()
115+
$executionContext.SessionState.LanguageMode = "NoLanguage"

InvokeTemplateCmdletCommand.cs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Management.Automation;
4+
5+
/*
6+
To build and install:
7+
8+
1) Set-Alias csc $env:WINDIR\Microsoft.NET\Framework\v2.0.50727\csc.exe
9+
2) $ref = [PsObject].Assembly.Location
10+
3) csc /out:TemplateBinaryModule.dll /t:library InvokeTemplateCmdletCommand.cs /r:$ref
11+
4) Import-Module .\TemplateBinaryModule.dll
12+
13+
To run:
14+
15+
PS >Invoke-TemplateCmdlet
16+
*/
17+
18+
namespace Template.Commands
19+
{
20+
[Cmdlet("Invoke", "TemplateCmdlet")]
21+
public class InvokeTemplateCmdletCommand : Cmdlet
22+
{
23+
[Parameter(Mandatory=true, Position=0, ValueFromPipeline=true)]
24+
public string Text
25+
{
26+
get
27+
{
28+
return text;
29+
}
30+
set
31+
{
32+
text = value;
33+
}
34+
}
35+
private string text;
36+
37+
protected override void BeginProcessing()
38+
{
39+
WriteObject("Processing Started");
40+
}
41+
42+
protected override void ProcessRecord()
43+
{
44+
WriteObject("Processing " + text);
45+
}
46+
47+
protected override void EndProcessing()
48+
{
49+
WriteObject("Processing Complete.");
50+
}
51+
}
52+
}

LibraryDirectory.ps1

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## From Windows PowerShell Cookbook (O'Reilly)
2+
## by Lee Holmes (http://www.leeholmes.com/guide)
3+
4+
Set-StrictMode -Version 3
5+
6+
## Get the size of all the items in the current directory
7+
function Get-DirectorySize
8+
{
9+
<#
10+
11+
.EXAMPLE
12+
13+
PS > $DebugPreference = "Continue"
14+
PS > Get-DirectorySize
15+
DEBUG: Current Directory: D:\lee\OReilly\Scripts\Programs
16+
Directory size: 46,581 bytes
17+
PS > $DebugPreference = "SilentlyContinue"
18+
PS > $VerbosePreference = "Continue"
19+
PS > Get-DirectorySize
20+
VERBOSE: Getting size
21+
VERBOSE: Got size: 46581
22+
Directory size: 46,581 bytes
23+
PS > $VerbosePreference = "SilentlyContinue"
24+
25+
#>
26+
27+
Write-Debug "Current Directory: $(Get-Location)"
28+
29+
Write-Verbose "Getting size"
30+
$size = (Get-ChildItem | Measure-Object -Sum Length).Sum
31+
Write-Verbose "Got size: $size"
32+
33+
Write-Host ("Directory size: {0:N0} bytes" -f $size)
34+
}
35+
36+
## Get the list of items in a directory, sorted by length
37+
function Get-ChildItemSortedByLength($path = (Get-Location), [switch] $Problematic)
38+
{
39+
<#
40+
41+
.EXAMPLE
42+
43+
PS > Get-ChildItemSortedByLength -Problematic
44+
out-lineoutput : Object of type "Microsoft.PowerShell.Commands.Internal.Fo
45+
rmat.FormatEntryData" is not legal or not in the correct sequence. This is
46+
likely caused by a user-specified "format-*" command which is conflicting
47+
with the default formatting.
48+
49+
#>
50+
51+
if($Problematic)
52+
{
53+
## Problematic version
54+
Get-ChildItem $path | Format-Table | Sort Length
55+
}
56+
else
57+
{
58+
## Fixed version
59+
Get-ChildItem $path | Sort Length | Format-Table
60+
}
61+
}

LibraryInputComparison.ps1

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## From Windows PowerShell Cookbook (O'Reilly)
2+
## by Lee Holmes (http://www.leeholmes.com/guide)
3+
4+
Set-StrictMode -Version 3
5+
6+
## Process each element in the pipeline, using a
7+
## foreach statement to visit each element in $input
8+
function Get-InputWithForeach($identifier)
9+
{
10+
Write-Host "Beginning InputWithForeach (ID: $identifier)"
11+
12+
foreach($element in $input)
13+
{
14+
Write-Host "Processing element $element (ID: $identifier)"
15+
$element
16+
}
17+
18+
Write-Host "Ending InputWithForeach (ID: $identifier)"
19+
}
20+
21+
## Process each element in the pipeline, using the
22+
## cmdlet-style keywords to visit each element in $input
23+
function Get-InputWithKeyword($identifier)
24+
{
25+
begin
26+
{
27+
Write-Host "Beginning InputWithKeyword (ID: $identifier)"
28+
}
29+
30+
process
31+
{
32+
Write-Host "Processing element $_ (ID: $identifier)"
33+
$_
34+
}
35+
36+
end
37+
{
38+
Write-Host "Ending InputWithKeyword (ID: $identifier)"
39+
}
40+
}

LibraryInvocation.ps1

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## From Windows PowerShell Cookbook (O'Reilly)
2+
## by Lee Holmes (http://www.leeholmes.com/guide)
3+
4+
Set-StrictMode -Version 3
5+
6+
## Return the name of the currently executing script
7+
## By placing this in a function, we drastically simplify
8+
## the logic it takes to determine the currently running
9+
## script
10+
11+
function Get-ScriptName
12+
{
13+
$myInvocation.ScriptName
14+
}
15+
16+
function Get-ScriptPath
17+
{
18+
Split-Path $myInvocation.ScriptName
19+
}

LibraryPrompt.ps1

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
##############################################################################
2+
##
3+
## From Windows PowerShell Cookbook (O'Reilly)
4+
## by Lee Holmes (http://www.leeholmes.com/guide)
5+
##
6+
##############################################################################
7+
8+
Set-StrictMode -Version 3
9+
10+
function Prompt
11+
{
12+
$id = 1
13+
$historyItem = Get-History -Count 1
14+
if($historyItem)
15+
{
16+
$id = $historyItem.Id + 1
17+
}
18+
19+
Write-Host -ForegroundColor DarkGray "`n[$(Get-Location)]"
20+
Write-Host -NoNewLine "PS:$id > "
21+
$host.UI.RawUI.WindowTitle = "$(Get-Location)"
22+
23+
"`b"
24+
}

LibraryProperties.ps1

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
##############################################################################
2+
##
3+
## From Windows PowerShell Cookbook (O'Reilly)
4+
## by Lee Holmes (http://www.leeholmes.com/guide)
5+
##
6+
##############################################################################
7+
8+
filter Get-PropertyValue($property)
9+
{
10+
$_.$property
11+
}

PSGetModuleInfo.xml

11.8 KB
Binary file not shown.

PersistentState.psm1

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
##############################################################################
2+
##
3+
## PersistentState.psm1
4+
## Demonstrates persistent state through module-scoped variables
5+
##
6+
## From Windows PowerShell Cookbook (O'Reilly)
7+
## by Lee Holmes (http://www.leeholmes.com/guide)
8+
##
9+
##############################################################################
10+
11+
$SCRIPT:memory = $null
12+
13+
function Set-Memory
14+
{
15+
param(
16+
[Parameter(ValueFromPipeline = $true)]
17+
$item
18+
)
19+
20+
begin { $SCRIPT:memory = New-Object System.Collections.ArrayList }
21+
process { $null = $memory.Add($item) }
22+
}
23+
24+
function Get-Memory
25+
{
26+
$memory.ToArray()
27+
}
28+
29+
Set-Alias remember Set-Memory
30+
Set-Alias recall Get-Memory
31+
32+
Export-ModuleMember -Function Set-Memory,Get-Memory
33+
Export-ModuleMember -Alias remember,recall

0 commit comments

Comments
 (0)