2008-10-01 54 views
2

我已經開始與PowerShell「玩耍」,並試圖讓它「表現」。

一個我想要做的事情是自定義提示是「相似」到什麼「$ M $ P $ _ $ + $ G」做MS-DOS:

快速這些做什麼的簡化:

字符|描述
$ m如果當前驅動器不是網絡驅動器,則與當前驅動器號或空字符串關聯的遠程名稱。
$ P電流驅動器和路徑
$ _ ENTER-LINEFEED
$ +零個或多個加號(+),這取決於PUSHD目錄堆棧的深度字符,一個字符的每個平推
$ 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 
>" 
} 

但剩下的就是沒有的東西我已經成功地複製...

非常感謝您的提示,一定會來!

回答

1

看看這個你想要做什麼:

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

這將讓你在PUSHD棧上的位置的計數:

$(get-location -Stack).count 
0

由於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)>" 
} 
+0

忘記乘以字符串。那裏好抓。今天早上太多的VBScript。 :(。 – EBGreen 2008-10-01 15:33:03

0

以下將給你相當於$ m。

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

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

多虧提示:

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)

我已經成功地得到它的工作。

我的完整個人資料是:

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