我已经开始“玩弄”PowerShell,并试图让它“正常运转”。

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

快速概述一下它们的作用:

特点|描述
百万美元 与当前驱动器号关联的远程名称,如果当前驱动器不是网络驱动器,则为空字符串。
$p 当前驱动器和路径
$_ 回车换行
$+ 零个或多个加号 (+) 字符,具体取决于深度 推送 目录堆栈,每一层压入一个字符
$克 >(大于号)

所以最终的输出是这样的:

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

但其余的还不是我能够复制的......

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

有帮助吗?

解决方案

看看这是否符合您的要求:

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

其他提示

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

$(get-location -Stack).count

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

以下将为您提供相当于 $m 的金额。

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

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

感谢以下提示:

在PowerShell中,如何确定当前驱动器是否是网络驱动器?
在PowerShell中,如何确定驱动器的根(假设它是网络驱动器)

我已经成功让它发挥作用了。

我的完整个人资料是:

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)>"
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top