2008-10-01 7 views
2

J'ai commencé à "jouer" avec PowerShell et j'essaye de le faire "se comporter".

L'une des choses que je voudrais faire est de personnaliser le prédisposent aux « similaire » à ce « $ M $ P $ _ $ + G » faire sur MS-Dos:

Un rapide aperçu de ce que ces derniers font:

Caractère| Description
$ m Le nom distant associé à la lettre de lecteur actuelle ou à la chaîne vide si le lecteur actuel n'est pas un lecteur réseau.
$ p lecteur et chemin
$ _ ENTRER-LINEFEED
$ + zéro ou plus signe plus (+) caractères en fonction de la profondeur de la pushd pile de répertoire, un caractère pour chaque niveau poussé
$ g> (signe supérieur)

Ainsi, le résultat final est quelque chose comme:

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

Je suis en mesure d'ajouter la fonctionnalité $M et $_ (et une fonctionnalité intéressante d'histoire) à mon message 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 est pas encore quelque chose que j'ai réussi à reproduire ....

Merci beaucoup pour les conseils qui viendront sûrement!

Répondre

1

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

Cela vous obtenir le nombre des emplacements sur la pile pushd:

$(get-location -Stack).count 
0

Merci à la réponse de EBGReens, mon « prompt » 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)>" 
} 
+0

oublié la multiplication des chaînes. Bonne prise là-bas. Trop de vbscript ce matin. :(. – EBGreen

0

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

Merci aux conseils en:

In PowerShell, how can I determine if the current drive is a networked drive or not?
In PowerShell, how can I determine the root of a drive (supposing it's a networked drive)

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 | ? {$_.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)>" 
} 
Questions connexes