هل يعادل أمر *Nix "أي" في PowerShell؟
-
09-06-2019 - |
سؤال
كيف أسأل PowerShell عن مكان وجود شيء ما؟
على سبيل المثال، "أي مفكرة" وتقوم بإرجاع الدليل الذي تم تشغيل ملف notepad.exe منه وفقًا للمسارات الحالية.
المحلول
أول اسم مستعار قمت بإنشائه عندما بدأت في تخصيص ملفي الشخصي في PowerShell كان "أيهما".
New-Alias which get-command
لإضافة هذا إلى ملف التعريف الخاص بك، اكتب هذا:
"`nNew-Alias which get-command" | add-content $profile
إن `n في بداية السطر الأخير هو التأكد من أنه سيبدأ كسطر جديد.
نصائح أخرى
هنا معادل فعلي لـ *nix، أي.فهو يعطي إخراجًا بنمط لا شىء.
Get-Command <your command> | Select-Object -ExpandProperty Definition
فقط استبدل بكل ما تبحث عنه.
PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty Definition
C:\Windows\system32\notepad.exe
عند إضافتها إلى ملف التعريف الخاص بك، ستحتاج إلى استخدام وظيفة بدلاً من الاسم المستعار لأنه لا يمكنك استخدام الأسماء المستعارة مع الأنابيب:
function which($name)
{
Get-Command $name | Select-Object -ExpandProperty Definition
}
الآن، عندما تقوم بإعادة تحميل ملف التعريف الخاص بك، يمكنك القيام بذلك:
PS C:\> which notepad
C:\Windows\system32\notepad.exe
عادةً ما أكتب فقط:
gcm notepad
أو
gcm note*
gcm هو الاسم المستعار الافتراضي لـ Get-Command.
على نظامي، تخرج ملاحظة gcm*:
[27] » gcm note*
CommandType Name Definition
----------- ---- ----------
Application notepad.exe C:\WINDOWS\notepad.exe
Application notepad.exe C:\WINDOWS\system32\notepad.exe
Application Notepad2.exe C:\Utils\Notepad2.exe
Application Notepad2.ini C:\Utils\Notepad2.ini
تحصل على الدليل والأمر الذي يطابق ما تبحث عنه.
جرب هذا المثال:
(Get-Command notepad.exe).Path
يبدو أن هذا يفعل ما تريد (لقد وجدته على http://huddledmasses.org/powershell-find-path/):
Function Find-Path($Path, [switch]$All = $false, [Microsoft.PowerShell.Commands.TestPathType]$type = "Any")
## You could comment out the function stuff and use it as a script instead, with this line:
#param($Path, [switch]$All = $false, [Microsoft.PowerShell.Commands.TestPathType]$type = "Any")
if($(Test-Path $Path -Type $type)) {
return $path
} else {
[string[]]$paths = @($pwd);
$paths += "$pwd;$env:path".split(";")
$paths = Join-Path $paths $(Split-Path $Path -leaf) | ? { Test-Path $_ -Type $type }
if($paths.Length -gt 0) {
if($All) {
return $paths;
} else {
return $paths[0]
}
}
}
throw "Couldn't find a matching path of type $type"
}
Set-Alias find Find-Path
افحص هذا بوويرشيل التي.
يشير الكود الموجود هناك إلى هذا:
($Env:Path).Split(";") | Get-ChildItem -filter notepad.exe
اقتراحي للوظيفة:
function which($cmd) { get-command $cmd | % { $_.Path } }
PS C:\> which devcon
C:\local\code\bin\devcon.exe
جرب ال where
الأمر على نظام التشغيل Windows 2003 أو الإصدارات الأحدث (أو Windows 2000/XP إذا قمت بتثبيت مجموعة أدوات الموارد).
راجع للشغل، وقد تلقى هذا المزيد من الإجابات في أسئلة أخرى:
مباراة سريعة وقذرة لنظام Unix which
يكون
New-Alias which where.exe
لكنها ترجع أسطرًا متعددة إذا كانت موجودة فهي كذلك
$(where.exe command | select -first 1)
انا يعجبني Get-Command | Format-List
, ، أو أقصر، باستخدام الأسماء المستعارة للاثنين وفقط لـ powershell.exe
:
gcm powershell | fl
يمكنك العثور على أسماء مستعارة مثل هذا:
alias -definition Format-List
يعمل استكمال علامة التبويب مع gcm
.
لدي هذا which
الوظيفة المتقدمة في ملفي الشخصي في PowerShell:
function which {
<#
.SYNOPSIS
Identifies the source of a PowerShell command.
.DESCRIPTION
Identifies the source of a PowerShell command. External commands (Applications) are identified by the path to the executable
(which must be in the system PATH); cmdlets and functions are identified as such and the name of the module they are defined in
provided; aliases are expanded and the source of the alias definition is returned.
.INPUTS
No inputs; you cannot pipe data to this function.
.OUTPUTS
.PARAMETER Name
The name of the command to be identified.
.EXAMPLE
PS C:\Users\Smith\Documents> which Get-Command
Get-Command: Cmdlet in module Microsoft.PowerShell.Core
(Identifies type and source of command)
.EXAMPLE
PS C:\Users\Smith\Documents> which notepad
C:\WINDOWS\SYSTEM32\notepad.exe
(Indicates the full path of the executable)
#>
param(
[String]$name
)
$cmd = Get-Command $name
$redirect = $null
switch ($cmd.CommandType) {
"Alias" { "{0}: Alias for ({1})" -f $cmd.Name, (. { which cmd.Definition } ) }
"Application" { $cmd.Source }
"Cmdlet" { "{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { "in module {0}" -f $cmd.Source} else { "from unspecified source" } } ) }
"Function" { "{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { "in module {0}" -f $cmd.Source} else { "from unspecified source" } } ) }
"Workflow" { "{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { "in module {0}" -f $cmd.Source} else { "from unspecified source" } } ) }
"ExternalScript" { $cmd.Source }
default { $cmd }
}
}
يستخدم:
function Which([string] $cmd) {
$path = (($Env:Path).Split(";") | Select -uniq | Where { $_.Length } | Where { Test-Path $_ } | Get-ChildItem -filter $cmd).FullName
if ($path) { $path.ToString() }
}
# Check if Chocolatey is installed
if (Which('cinst.bat')) {
Write-Host "yes"
} else {
Write-Host "no"
}
أو هذا الإصدار يستدعي الأمر الأصلي حيث.
يعمل هذا الإصدار أيضًا بشكل أفضل، لأنه لا يقتصر على ملفات الخفافيش:
function which([string] $cmd) {
$where = iex $(Join-Path $env:SystemRoot "System32\where.exe $cmd 2>&1")
$first = $($where -split '[\r\n]')
if ($first.getType().BaseType.Name -eq 'Array') {
$first = $first[0]
}
if (Test-Path $first) {
$first
}
}
# Check if Curl is installed
if (which('curl')) {
echo 'yes'
} else {
echo 'no'
}