PoshCode Archive  Artifact [5e651a9166]

Artifact 5e651a9166fd99e9532668e1708c4d2aacfe14a47acc8c5d14e9394f0652d79d:

  • File Enter-Module.ps1 — part of check-in [1f210a23c1] at 2018-06-10 13:05:46 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 1624)

# encoding: ascii
# api: powershell
# title: Enter-Module.ps1
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# type: module
# author: Lee Holmes
# license: CC0
# x-poshcode-id: 2142
# x-archived: 2016-05-17T13:37:38
# x-published: 2011-09-09T21:40:00
#
#
##############################################################################
##
## Enter-Module
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Lets you examine internal module state and functions by executing user
input in the scope of the supplied module.

.EXAMPLE

PS >Import-Module PersistentState
PS >Get-Module PersistentState

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Script     PersistentState           {Set-Memory, Get-Memory}


PS >"Hello World" | Set-Memory
PS >$m = Get-Module PersistentState
PS >Enter-Module $m
PersistentState: dir variable:\mem*

Name                           Value
----                           -----
memory                         {Hello World}

PersistentState: exit
PS >

#>

param(
    ## The module to examine
    [System.Management.Automation.PSModuleInfo] $Module
)

Set-StrictMode -Version Latest

$userInput = Read-Host $($module.Name)
while($userInput -ne "exit")
{
    $scriptblock = [ScriptBlock]::Create($userInput)
    & $module $scriptblock

    $userInput = Read-Host $($module.Name)
}