Equivalente di *Nix 'che' comando PowerShell?
-
09-06-2019 - |
Domanda
Come faccio a chiedere a PowerShell in cui qualcosa?
Per esempio, "che blocco note" e restituisce la directory in cui il notepad.exe viene eseguito secondo i percorsi di corrente.
Soluzione
Il primo alias che ho fatto da quando ho iniziato a personalizzare il mio profilo in PowerShell è 'che'.
New-Alias which get-command
Per aggiungere questo al tuo profilo, tipo questo:
"`nNew-Alias which get-command" | add-content $profile
La `n all'inizio dell'ultima riga è quello di garantire che partirà come di una nuova linea.
Altri suggerimenti
Qui è una reale *nix equivalenti, cioèdà *nix stile di uscita.
Get-Command <your command> | Select-Object -ExpandProperty Definition
Sostituire solo con quello che stai cercando.
PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty Definition
C:\Windows\system32\notepad.exe
Quando si aggiunge al tuo profilo, è consigliabile utilizzare una funzione piuttosto che un alias, perché non è possibile utilizzare gli alias con i tubi:
function which($name)
{
Get-Command $name | Select-Object -ExpandProperty Definition
}
Ora, quando si ricarica il tuo profilo è possibile fare questo:
PS C:\> which notepad
C:\Windows\system32\notepad.exe
Io di solito basta digitare:
gcm notepad
o
gcm note*
gcm è l'impostazione predefinita alias per il Comando Get.
Sul mio sistema, gcm nota* uscite:
[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
Si ottiene la directory e il comando che corrisponde a quello che stai cercando.
Prova questo esempio:
(Get-Command notepad.exe).Path
Questo sembra fare quello che vuoi (l'ho trovato su 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
Controllare questo PowerShell Che.
Il codice fornito, ci suggerisce questo:
($Env:Path).Split(";") | Get-ChildItem -filter notepad.exe
La mia proposta per il Quale la funzione:
function which($cmd) { get-command $cmd | % { $_.Path } }
PS C:\> which devcon
C:\local\code\bin\devcon.exe
Provare l' where
comando in Windows 2003 o versione successiva (o Windows 2000/XP, se hai installato un Kit di Risorse).
BTW, questo ha ricevuto più risposte ad altre domande:
Un quick-and-dirty partita di Unix which
è
New-Alias which where.exe
Ma restituisce più righe, se esistenti, così poi diventa
$(where.exe command | select -first 1)
Mi piace Get-Command | Format-List
, o più breve, utilizzando gli alias per le due e solo per powershell.exe
:
gcm powershell | fl
Si possono trovare alias come questo:
alias -definition Format-List
Scheda lavori di completamento con gcm
.
Ho questo which
funzione avanzata nel mio PowerShell profilo:
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 }
}
}
Uso:
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"
}
O questa versione, chiamata originale dove comando.
Questa versione funziona anche meglio, perché non si limita a un file bat:
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'
}