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"
0 commit comments