PowerShell プロンプトのカスタマイズ - CMD の $M$P$_$+$G に相当しますか?
-
03-07-2019 - |
質問
私は PowerShell を「いじって」始めて、PowerShell が「動作」するようにしようとしています。
私がやりたいことの 1 つは、MS-Dos での "$M$P$_$+$G" の動作と "同様" になるように PROMPT をカスタマイズすることです。
これらの機能を簡単にまとめると、次のようになります。
キャラクター|説明
$m 現在のドライブ文字に関連付けられたリモート名、または現在のドライブがネットワーク ドライブでない場合は空の文字列。
$p 現在のドライブとパス
$_ 改行を入力してください
$+ 深さに応じて 0 個以上のプラス記号 (+) 文字 プッシュされた ディレクトリスタック、プッシュされたレベルごとに 1 文字
$g > (大なり記号)
最終的な出力は次のようになります。
\\spma1fp1\JARAVJ$ H:\temp
++>
を追加することができました $M
そして $_
次のようにプロンプトに機能 (および気の利いた履歴機能) を追加します。
function prompt
{
## Get the history. Since the history may be either empty,
## a single item or an array, the @() syntax ensures
## that PowerShell treats it as an array
$history = @(get-history)
## If there are any items in the history, find out the
## Id of the final one.
## PowerShell defaults the $lastId variable to '0' if this
## code doesn't execute.
if($history.Count -gt 0)
{
$lastItem = $history[$history.Count - 1]
$lastId = $lastItem.Id
}
## The command that we're currently entering on the prompt
## will be next in the history. Because of that, we'll
## take the last history Id and add one to it.
$nextCommand = $lastId + 1
## Get the current location
$currentDirectory = get-location
## Set the Windows Title to the current location
$host.ui.RawUI.WindowTitle = "PS: " + $currentDirectory
## And create a prompt that shows the command number,
## and current location
"PS:$nextCommand $currentDirectory
>"
}
しかし、残りはまだ再現できていません...。
きっと得られるヒントをありがとう!
解決
これが希望どおりに動作するかどうかを確認してください。
function prompt
{
## Get the history. Since the history may be either empty,
## a single item or an array, the @() syntax ensures
## that PowerShell treats it as an array
$history = @(get-history)
## If there are any items in the history, find out the
## Id of the final one.
## PowerShell defaults the $lastId variable to '0' if this
## code doesn't execute.
if($history.Count -gt 0)
{
$lastItem = $history[$history.Count - 1]
$lastId = $lastItem.Id
}
## The command that we're currently entering on the prompt
## will be next in the history. Because of that, we'll
## take the last history Id and add one to it.
$nextCommand = $lastId + 1
## Get the current location
$currentDirectory = get-location
## Set the Windows Title to the current location
$host.ui.RawUI.WindowTitle = "PS: " + $currentDirectory
##pushd info
$pushdCount = $(get-location -stack).count
$pushPrompt = ""
for ($i=0; $i -lt $pushdCount; $i++)
{
$pushPrompt += "+"
}
## And create a prompt that shows the command number,
## and current location
"PS:$nextCommand $currentDirectory `n$($pushPrompt)>"
}
他のヒント
これにより、pushd スタック上の場所の数が取得されます。
$(get-location -Stack).count
EBGreens の回答のおかげで、私の「プロンプト」はスタックの深さを表示できるようになりました。
function prompt
{
## Initialize vars
$depth_string = ""
## Get the Stack -Pushd count
$depth = (get-location -Stack).count
## Create a string that has $depth plus signs
$depth_string = "+" * $depth
## Get the history. Since the history may be either empty,
## a single item or an array, the @() syntax ensures
## that PowerShell treats it as an array
$history = @(get-history)
## If there are any items in the history, find out the
## Id of the final one.
## PowerShell defaults the $lastId variable to '0' if this
## code doesn't execute.
if($history.Count -gt 0)
{
$lastItem = $history[$history.Count - 1]
$lastId = $lastItem.Id
}
## The command that we're currently entering on the prompt
## will be next in the history. Because of that, we'll
## take the last history Id and add one to it.
$nextCommand = $lastId + 1
## Get the current location
$currentDirectory = get-location
## Set the Windows Title to the current location
$host.ui.RawUI.WindowTitle = "PS: " + $currentDirectory
## And create a prompt that shows the command number,
## and current location
"PS:$nextCommand $currentDirectory `n$($depth_string)>"
}
以下では $m に相当する金額が得られます。
$mydrive = $pwd.Drive.Name + ":";
$networkShare = (gwmi -class "Win32_MappedLogicalDisk" -filter "DeviceID = '$mydrive'");
if ($networkShare -ne $null)
{
$networkPath = $networkShare.ProviderName
}
以下のヒントのおかげで:
PowerShell で、現在のドライブがネットワーク ドライブであるかどうかを確認するにはどうすればよいですか?
PowerShell で、ドライブのルートを確認するにはどうすればよいですか (ネットワーク ドライブであると仮定して)
なんとか動作させることができました。
私の完全なプロフィールは次のとおりです。
function prompt
{
## Initialize vars
$depth_string = ""
## Get the Stack -Pushd count
$depth = (get-location -Stack).count
## Create a string that has $depth plus signs
$depth_string = "+" * $depth
## Get the history. Since the history may be either empty,
## a single item or an array, the @() syntax ensures
## that PowerShell treats it as an array
$history = @(get-history)
## If there are any items in the history, find out the
## Id of the final one.
## PowerShell defaults the $lastId variable to '0' if this
## code doesn't execute.
if($history.Count -gt 0)
{
$lastItem = $history[$history.Count - 1]
$lastId = $lastItem.Id
}
## The command that we're currently entering on the prompt
## will be next in the history. Because of that, we'll
## take the last history Id and add one to it.
$nextCommand = $lastId + 1
## Get the current location
$currentDirectory = get-location
## Set the Windows Title to the current location
$host.ui.RawUI.WindowTitle = "PS: " + $currentDirectory
## Get the current location's DRIVE LETTER
$drive = (get-item ($currentDirectory)).root.name
## Make sure we're using a path that is not already UNC
if ($drive.IndexOf(":") -ne "-1")
{
$root_dir = (get-wmiobject Win32_LogicalDisk | ? {$_.deviceid -eq $drive.Trim("\") } | % { $_.providername })+" "
}
else
{
$root_dir=""
}
## And create a prompt that shows the command number,
## and current location
"PS:$nextCommand $root_dir$currentDirectory `n$($depth_string)>"
}