تخصيص PowerShell موجه - أي ما يعادل CMD s $M$P$_$+$G ؟
-
03-07-2019 - |
سؤال
لقد بدأت "تلعب" مع PowerShell وأنا أحاول الحصول على "التصرف".
واحدة من الأشياء التي أود القيام به هو تخصيص موجه إلى "مماثلة" ما "$M$P$_$+$G" هل على MS-Dos:
لمحة سريعة عن ما تفعل هذه:
حرف| وصف
$m جهاز التحكم عن بعد اسم المرتبطة الحالي حرف محرك الأقراص أو سلسلة فارغة إذا كان محرك الأقراص الحالي هو ليس محرك أقراص شبكة.
$p محرك الأقراص الحالي والمسار
$_ ENTER-آلية
$+ صفر أو أكثر من علامة زائد (+) الشخصيات اعتمادا على عمق 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
>"
}
ولكن الباقي هو ليس بعد شيء لقد تمكنت من تكرار....
شكرا جزيلا على النصائح التي سوف تأتي بالتأكيد!
المحلول
نرى ما اذا كان هذا ما تريد:
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)>"
}