簡體   English   中英

在cmd和INFO下執行powershell命令:無法找到給定模式的文件

[英]Executing powershell command under cmd and INFO: Could not find files for the given pattern(s)

我試圖從CMD執行以下powershell命令,例如:

powershell Get-WmiObject Win32_PnPSignedDriver 

powershell Get-WmiObject Win32_PnPSignedDriver > test.txt 

兩者都正常工作。

但是當我進行查詢時,例如:

powershell (Get-WmiObject Win32_PnPSignedDriver | where {$_.location -like "*PCI bus 0, device 22, function 0*"}).DeviceName

我收到此消息的原因是我無法確定:

信息:無法找到給定模式的文件

這似乎對我有用(我看到了與你原來相同的錯誤):

powershell -command "(Get-WmiObject Win32_PnPSignedDriver | where {$_.location -like '*PCI bus 0, device 22, function 0*'}).DeviceName"

要使用cmd.exe (命令提示符) 將命令傳遞給powershell.exe 一般准則來補充現有答案:

  • 將整個PowerShell命令"..." (雙引號)中。

    • 這樣可以保護其內容免受cmd.exe不必要的cmd.exe解釋 - 與| 在這種情況下,正如aschipfl的回答中所解釋的那樣
    • 但是, 仍然會擴展cmd.exe樣式的環境變量引用(例如, %USERNAME% )。
  • 對於嵌入在PowerShell命令中的引用

    • 在可行的情況下使用'...' (單引號) ; 這就是Mark Wragg的有用答案 ,但如果引用的字符串是一個文字 (不包含變量引用或子表達式),它只是一個選項。

    • 如果您確實需要嵌入"..." ,轉義為\\"...\\"

      • 請注意,雖然 PowerShell 內部,它是用作轉義字符的` (反引號),當從外部 PowerShell傳遞字符串時需要\\

簡化示例(從cmd.exe運行):

請注意,傳遞沒有參數名稱的命令意味着-Command參數; 運行powershell -? 查看命令行語法。
您可能還希望使用-NoProfile以便不會每次都加載用戶配置文件。

不需要嵌入式引用的命令 - 只需雙引號:

 powershell -noprofile "get-date"
 powershell -noprofile "(get-date).Date"
 powershell -noprofile "get-date | get-member"

嵌入式引用的命令 - 使用'...'\\"...\\"

 powershell -noprofile "(get-date).Year -like '*17'"
 powershell -noprofile "$v=17; (get-date).Year -like \"*$v\""

包含cmd.exe環境變量值的命令:

 :: # Nothing special to do: cmd.exe expands the reference irrespective of quoting.
 set "v=17"
 powershell -noprofile "(get-date).Year -like '*%v%'"

 :: # More robust variant that passes the value as an argument to a script block.
 set "v=17"
 powershell -noprofile ". { param($v) (get-date).Year -like \"*$v\" }" "%v%"

可選讀取:從類似POSIX的shell調用powershell ,例如bash

上述規則類似地適用,除了您通常使用'...' (單引號)來封裝整個PowerShell命令,這會明確阻止shell的前期解釋。

如果明確需要預先擴展shell變量引用和命令替換,則使用"..."是一個選項,但是混淆的可能性很大,因為類似POSIX的shell和PowerShell都使用sigil $來引用變量 -什么是擴展時可能並不明顯。

類似POSIX的shell絕對不支持在'...' side '...'字符串中嵌入'實例'...' ,這需要一個有點尷尬的解決方法(見下文)。

簡化示例(從類似POSIX的shell運行,例如bash ):

不需要嵌入式引用的命令 - 只需單引號:

 powershell -noprofile 'get-date'
 powershell -noprofile '(get-date).Date'
 powershell -noprofile 'get-date | get-member'

嵌入式引用的命令 - 用'\\'' (原文如此)替換嵌入的'實例,並使用嵌入式"原樣:

 powershell -noprofile '(get-date).Year -like '\''*17'\'''
 powershell -noprofile '$v=17; (get-date).Year -like "*$v"'

包含shell / environment變量值的命令:

 # By using a double-quoted string, the shell expands $v up front.
 v=17
 powershell -noprofile "(get-date).Year -like '*$v'"

 # It gets trickier if you want to reference PS variables too.
 # Note how the PS variable's $ is \-escaped so that the shell doesn't interpret it.
 v=$HOME
 powershell -noprofile "\$HOME -eq '$v'"

 # More robust variant that passes the value as an argument to a script block.
 v=17
 powershell -noprofile '. { param($v) (get-date).Year -like "*$v" }' "$v"

我很確定管道字符| 是這里的問題,因為cmd試圖處理它。 只需通過前面的^逃避它:

powershell (Get-WmiObject Win32_PnPSignedDriver ^| where {$_.location -like "*PCI bus 0, device 22, function 0*"}).DeviceName

如果此代碼的代碼parenthesised塊內使用cmd ,您可能需要逃避關閉)以及(開幕(可得逃過一劫,但也沒有必要):

powershell ^(Get-WmiObject Win32_PnPSignedDriver ^| where {$_.location -like "*PCI bus 0, device 22, function 0*"}^).DeviceName

暫無
暫無

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

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