簡體   English   中英

“無法找到指定的文件” - Powershell

[英]"Unable to find specified file" - Powershell

我在我的 powershell 配置文件中寫了這個函數:

function start {
    if ($args[0] == "blender") {
        Invoke-Item 'C:\Program Files\Blender Foundation\Blender 2.90\blender.exe'
    }
}

它在第 1 行的第 1 個字符處給了我以下錯誤

找不到指定的文件

文件路徑是正確的,因為如果我復制並粘貼代碼Invoke-Item ...在我的 powershell 中,它會正常打開 Blender。 問題是什么?

謝謝!

發生這種情況,因為您的函數名稱與Start-Process沖突。:

get-alias -Name start

CommandType     Name                               Version    Source
-----------     ----                               -------    ------
Alias           start -> Start-Process

重命名函數是像這樣更正的第一步,

function start-myApps {
    if ($args[0] == "MyApplication") {
        Invoke-Item 'C:\Program Files\MyApplication\MyApplication.exe'
    }
}

但這還不夠,因為它給出了另一個錯誤:

Start-MyApps "MyApplication"
= : The term '=' is not recognized as the name of a cmdlet, function, script file, or operable program. 
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:2 char:19
+     if ($args[0] == "MyApplication") {
+                   ~
    + CategoryInfo          : ObjectNotFound: (=:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CommandNotFoundException

這是因為 Powershell 的相等比較運算符-eq ,而不是==

工作版本有不同的名稱和操作符,就像這樣,

function start-myApps {
    if ($args[0] -eq "MyApplication") {
        Invoke-Item 'C:\Program Files\MyApplication\MyApplication.exe'
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM