Domanda

Ho iniziato a " giocare con " con PowerShell e sto cercando di farlo "quotare comportati".

Una delle cose che mi piacerebbe fare è personalizzare PROMPT in modo che sia "simile" a cosa " $ M $ P $ _ $ + $ G " fare su MS-Dos:

Un rapido riassunto di ciò che fanno:

Carattere | Descrizione
$ m Il nome remoto associato alla lettera dell'unità corrente o alla stringa vuota se l'unità corrente non è un'unità di rete.
$ p Unità e percorso correnti
$ _ ENTER-LINEFEED
$ + Zero o più caratteri più (+) a seconda della profondità dello stack di directory pushd , un carattere per ogni livello spinto
$ g > (segno maggiore di)

Quindi l'output finale è qualcosa del tipo:

    \\spma1fp1\JARAVJ$ H:\temp
    ++>

Sono stato in grado di aggiungere al mio prompt le funzionalità $ M e $ _ (e un'elegante funzione Cronologia) come segue:

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
>"
}

Ma il resto non è ancora qualcosa che sono riuscito a duplicare ....

Grazie mille per i suggerimenti che verranno sicuramente!

È stato utile?

Soluzione

Vedi se questo fa quello che vuoi:

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)>"
}

Altri suggerimenti

Questo ti farà contare il numero di posizioni nello stack pushd:

$(get-location -Stack).count

Grazie alla risposta di EBGReens, il mio "prompt" è ora in grado di mostrare la profondità dello stack:

 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)>"
 }

Quanto segue ti darà l'equivalente di $ m.

$mydrive = $pwd.Drive.Name + ":";
$networkShare = (gwmi -class "Win32_MappedLogicalDisk" -filter "DeviceID = '$mydrive'");

if ($networkShare -ne $null)
{
    $networkPath = $networkShare.ProviderName
}

Grazie ai suggerimenti in:

In PowerShell, come posso determinare se l'unità corrente è un'unità di rete o no?
In PowerShell , come posso determinare la radice di un'unità (supponendo che sia un'unità di rete)

Sono riuscito a farlo funzionare.

Il mio profilo completo è:

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 | ? {

Grazie ai suggerimenti in:

In PowerShell, come posso determinare se l'unità corrente è un'unità di rete o no?
In PowerShell , come posso determinare la radice di un'unità (supponendo che sia un'unità di rete)

Sono riuscito a farlo funzionare.

Il mio profilo completo è:

<*>.deviceid -eq $drive.Trim("\") } | % {

Grazie ai suggerimenti in:

In PowerShell, come posso determinare se l'unità corrente è un'unità di rete o no?
In PowerShell , come posso determinare la radice di un'unità (supponendo che sia un'unità di rete)

Sono riuscito a farlo funzionare.

Il mio profilo completo è:

<*>.providername })+" " } else { $root_dir="" } ## And create a prompt that shows the command number, ## and current location "PS:$nextCommand $root_dir$currentDirectory `n$($depth_string)>" }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top