문제

나는 PowerShell과 함께 "놀이"를 시작했고 그것을 "행동"하려고 노력하고 있습니다.

내가하고 싶은 일 중 하나는 MS-DOS에서 "$ m $ p $ _ $+$ g"와 "비슷한"프롬프트를 사용자 정의하는 것입니다.

이것들이하는 일에 대한 빠른 요약 :

성격| 설명
$ m 현재 드라이브가 네트워크 드라이브가 아닌 경우 현재 드라이브 문자 또는 빈 문자열과 관련된 원격 이름.
$ p 현재 드라이브 및 경로
$_ 엔터테인 피드
$+ 0 이상의 플러스 부호 (+) 문자의 깊이에 따라 푸시 디렉토리 스택, 각 레벨에 대한 하나의 문자가 푸시되었습니다
$ 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
>"
}

그러나 나머지는 아직 내가 복제 한 것이 아닙니다 ....

반드시 올 팁에 감사드립니다!

도움이 되었습니까?

해결책

이것이 당신이 원하는 것을하는지 확인하십시오.

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

다른 팁

이렇게하면 푸시 스택의 위치 수를 얻을 수 있습니다.

$(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