Personalización de la solicitud de PowerShell: ¿Equivalente a $ M $ P $ _ $ + $ G de CMD?
-
03-07-2019 - |
Pregunta
He comenzado a "jugar". con PowerShell y estoy tratando de que se "comporte".
Una de las cosas que me gustaría hacer es personalizar el PROMPT para que sea "similar". a qué " $ M $ P $ _ $ + $ G " hacer en MS-Dos:
Un resumen rápido de lo que hacen:
Carácter | Descripción
$ m El nombre remoto asociado con la letra de la unidad actual o la cadena vacía si la unidad actual no es una unidad de red.
$ p Unidad y ruta actuales
$ _ ENTER-LINEFEED
$ + Cero o más caracteres de signo más (+) dependiendo de la profundidad de la pila de directorios pushd , un carácter para cada nivel empujado
$ g > (signo mayor que)
Así que la salida final es algo así como:
\\spma1fp1\JARAVJ$ H:\temp
++>
He podido agregar la funcionalidad $ M
y $ _
(y una ingeniosa función de Historial) a mi mensaje de la siguiente manera:
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
>"
}
Pero el resto aún no es algo que logré duplicar ...
¡Muchas gracias por los consejos que seguramente vendrán!
Solución
Mira si esto hace lo que quieres:
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)>"
}
Otros consejos
Esto te dará el recuento de las ubicaciones en la pila pushd:
$(get-location -Stack).count
Gracias a la respuesta de EBGReens, mi " mensaje " ahora es capaz de mostrar la profundidad de la pila:
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)>"
}
Lo siguiente le dará el equivalente de $ m.
$mydrive = $pwd.Drive.Name + ":";
$networkShare = (gwmi -class "Win32_MappedLogicalDisk" -filter "DeviceID = '$mydrive'");
if ($networkShare -ne $null)
{
$networkPath = $networkShare.ProviderName
}
Gracias a los consejos en:
En PowerShell, ¿cómo puedo determinar si la unidad actual es una unidad en red o no?
En PowerShell , ¿cómo puedo determinar la raíz de una unidad (suponiendo que es una unidad en red)
He logrado que funcione.
Mi perfil completo es:
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 | ? { Gracias a los consejos en:
En PowerShell, ¿cómo puedo determinar si la unidad actual es una unidad en red o no?
En PowerShell , ¿cómo puedo determinar la raíz de una unidad (suponiendo que es una unidad en red)
He logrado que funcione.
Mi perfil completo es:
<*>.deviceid -eq $drive.Trim("\") } | % { Gracias a los consejos en:
En PowerShell, ¿cómo puedo determinar si la unidad actual es una unidad en red o no?
En PowerShell , ¿cómo puedo determinar la raíz de una unidad (suponiendo que es una unidad en red)
He logrado que funcione.
Mi perfil completo es:
<*>.providername })+" "
}
else
{
$root_dir=""
}
## And create a prompt that shows the command number,
## and current location
"PS:$nextCommand $root_dir$currentDirectory `n$($depth_string)>"
}