Personnalisation de l’invite PowerShell - Équivalent à la valeur de $ M $ P $ _ $ + $ G?
-
03-07-2019 - |
Question
J'ai commencé à "jouer" avec PowerShell et j'essaie de le faire "se comporter".
L’une des choses que je voudrais faire est de personnaliser le PROMPT pour qu’il soit "similaire". à quoi & $ M $ P $ _ $ + $ G " faire sur MS-Dos:
Un bref aperçu de ce qu’ils font:
Caractère | Description
$ m Nom distant associé à la lettre du lecteur actuel ou à la chaîne vide si le lecteur actuel n'est pas un lecteur réseau.
$ p Lecteur et chemin actuels
$ _ ENTER-LINEFEED
$ + Zéro ou plusieurs caractères du signe plus (+) en fonction de la profondeur de la pile de répertoires pushd , un caractère pour chaque niveau poussé
$ g > (signe supérieur à)
Le résultat final ressemble donc à quelque chose comme:
\\spma1fp1\JARAVJ$ H:\temp
++>
J'ai pu ajouter les fonctionnalités $ M
et $ _
(ainsi qu'une fonctionnalité intéressante de l'historique) à mon invite, comme suit:
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
>"
}
Mais le reste n'est pas encore quelque chose que j'ai réussi à dupliquer ....
Merci beaucoup pour les conseils qui viendront sûrement!
La solution
Voir si cela fait ce que vous voulez:
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)>"
}
Autres conseils
Cela vous donnera le nombre d'emplacements sur la pile pushd:
$(get-location -Stack).count
Grâce à la réponse d'EBGReens, mon "invite" est maintenant capable de montrer la profondeur de la pile:
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)>"
}
Ce qui suit vous donnera l'équivalent de $ m.
$mydrive = $pwd.Drive.Name + ":";
$networkShare = (gwmi -class "Win32_MappedLogicalDisk" -filter "DeviceID = '$mydrive'");
if ($networkShare -ne $null)
{
$networkPath = $networkShare.ProviderName
}
Merci aux astuces de:
Dans PowerShell, comment puis-je déterminer si le lecteur actuel est un lecteur en réseau ou non?
Dans PowerShell , comment puis-je déterminer la racine d’un lecteur (s’il s’agit d’un lecteur réseau)
J'ai réussi à le faire fonctionner.
Mon profil complet est:
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 | ? { Merci aux astuces de:
Dans PowerShell, comment puis-je déterminer si le lecteur actuel est un lecteur en réseau ou non?
Dans PowerShell , comment puis-je déterminer la racine d’un lecteur (s’il s’agit d’un lecteur réseau)
J'ai réussi à le faire fonctionner.
Mon profil complet est:
<*>.deviceid -eq $drive.Trim("\") } | % { Merci aux astuces de:
Dans PowerShell, comment puis-je déterminer si le lecteur actuel est un lecteur en réseau ou non?
Dans PowerShell , comment puis-je déterminer la racine d’un lecteur (s’il s’agit d’un lecteur réseau)
J'ai réussi à le faire fonctionner.
Mon profil complet est:
<*>.providername })+" "
}
else
{
$root_dir=""
}
## And create a prompt that shows the command number,
## and current location
"PS:$nextCommand $root_dir$currentDirectory `n$($depth_string)>"
}