このエントリーをはてなブックマークに追加


PowerShellのFunctionの一覧および内容を表示する方法

PowerShellの関数である、Functionの一覧を表示する方法を以下に記します。
使用したPowerShellのバージョンは、以下の通りです。

PS C:\> $PSVersionTable.PSVersion
Major  Minor  Build  Revision
-----  -----  -----  --------
5      0      10586  63

Functionの一覧および内容を表示する方法

以下の構文でFunctionの一覧を表示することができます。

Get-ChildItem -path function:

また、以下のように操作しても同様の結果となります。

Set-Location function:
Get-ChildItem

元に戻すときには、Cドライブであれば、c:と入力しEnterキーを押せば戻ります。

Functionの内部(定義)を見る方法

以下のように操作すると、指定したFunctionのスクリプトを見ることができます。

clear-host

PS C:\Users\Sakura> Get-Item function:clear-host

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Clear-Host
PS C:\Users\Sakura> (Get-Item function:clear-host).Definition
$space = New-Object System.Management.Automation.Host.BufferCell
$space.Character = ' '
$space.ForegroundColor = $host.ui.rawui.ForegroundColor
$space.BackgroundColor = $host.ui.rawui.BackgroundColor
$rect = New-Object System.Management.Automation.Host.Rectangle
$rect.Top = $rect.Bottom = $rect.Right = $rect.Left = -1
$origin = New-Object System.Management.Automation.Host.Coordinates
$Host.UI.RawUI.CursorPosition = $origin
$Host.UI.RawUI.SetBufferContents($rect, $space)
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225747
# .ExternalHelp System.Management.Automation.dll-help.xml

prompt

PS C:\Users\Sakura> Get-Item Function:\prompt

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        prompt
PS C:\Users\Sakura> (Get-Item Function:\prompt).Definition
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

実際にFunctionを作って確認してみる

以下のようにメッセージを引数として受け取り表示する関数を作成し確認してみます。

PS C:\Users\Sakura> Function SayHi($msg="") { Write-Output "Hi! $msg" }
PS C:\Users\Sakura> SayHi Sakura
Hi! Sakura
PS C:\Users\Sakura> Get-ChildItem Function:\SayHi

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        SayHi


PS C:\Users\Sakura> (Get-ChildItem Function:\SayHi).Definition
param($msg="")
 Write-Output "Hi! $msg"

以上、PowerShellのFunction一覧の表示および、Functionのスクリプト内容を表示する方法でした。



トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2016-01-21 (木) 22:38:22