[英]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傳遞字符串時需要\\
。 簡化示例(從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%"
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.