【问题标题】:Customizing PowerShell Prompt - Equivalent to CMD's $M$P$_$+$G?自定义 PowerShell 提示 - 相当于 CMD 的 $M$P$_$+$G?
【发布时间】:2010-09-14 13:10:15
【问题描述】:

我已经开始“玩弄” PowerShell,并试图让它“表现”。

我想做的一件事是将 PROMPT 自定义为“类似于”在 MS-Dos 上执行的“$M$P$_$+$G”:

这些功能的简要说明:

人物|说明
$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
>"
}

但其余的还不是我设法复制的东西......

非常感谢您肯定会提供的提示!

【问题讨论】:

    标签: c# powershell command prompt customization


    【解决方案1】:

    感谢以下提示:

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

    【讨论】:

      【解决方案2】:

      以下将为您提供 $m 的等价物。

      $mydrive = $pwd.Drive.Name + ":";
      $networkShare = (gwmi -class "Win32_MappedLogicalDisk" -filter "DeviceID = '$mydrive'");
      
      if ($networkShare -ne $null)
      {
          $networkPath = $networkShare.ProviderName
      }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 忘了乘以字符串。那里很好抓。今天早上太多vbscript。 :(。
        【解决方案4】:

        看看这是不是你想要的:

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

        【讨论】:

          【解决方案5】:

          这将为您提供 pushd 堆栈上的位置计数:

          $(get-location -Stack).count
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-11-23
            • 2018-05-27
            • 2019-05-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多