2015-05-21 106 views
0
gc "C:\folder1\folder2\MyService.exe.config" 

都好獲取內容無法讀取通過WMI檢索路徑

gwmi win32_service|?{$_.name -match "MyService"} | % {$_.pathname} 
"C:\folder1\folder2\MyService.exe.config" 

返回正確路徑

gwmi win32_service|?{$_.name -match "Mailing"} | % {$_.pathname} | % {$_.gettype()} 

返回類型,絕對是一個字符串

gwmi win32_service|?{$_.name -match "Mailing"} | % {$_.pathname} | % {gc -path $_} 
gc : Cannot find drive. A drive with the name '"C' does not exist. 
At line:1 char:71 
+ gwmi win32_service|?{$_.name -match "MyService"} | % {$_.pathname} | % {gc -path $ ... 
+                  ~~~~~~~~~~ 
    + CategoryInfo   : ObjectNotFound: ("C:String) [Get-Content], DriveNotFoundException 
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand 

我在這裏錯過了什麼?

+0

你有什麼想在這裏做什麼?返回的路徑名最有可能是一個exe文件,爲什麼你想在powershell中檢索它的內容?無論如何,試試這個:gwmi win32_service |?{$ _。name -match「IDLSemanticLogging」} |選擇-ExpandProperty路徑名| %{gc $ _} –

回答

3

細看的錯誤消息:

A drive with the name '"C' does not exist. 
        ^

注意在驅動器盤符前面的"

的路徑從WMI查詢返回的是雙引號之間,即雙引號不是劃定串作爲你的第一個聲明,但該字符串的一部分,所以Get-Content失敗,因爲它可以」 t找到一個驅動器"C:

示範:

PS C:\>$path = "C:\temp\web.config" 
PS C:\>$path 
C:\temp\web.config 
PS C:\>Get-Content $path 


... 

PS C:\>$path = '"C:\temp\web.config"' 
PS C:\>$path 
"C:\temp\web.config" 
PS C:\>Get-Content $path 
Get-Content : Cannot find drive. A drive with the name '"C' does not exist. 
At line:1 char:1 
+ Get-Content $path 
+ ~~~~~~~~ 
    + CategoryInfo   : ObjectNotFound: ("C:String) [Get-Content], DriveNotFoundException 
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand

從開始到字符串末尾刪除雙引號,問題就會消失:

Get-WmiObject Win32_Service | 
    ? { $_.Name -match "Mailing" } | 
    % { $_.PathName -replace '^"(.*)"$', '$1' } | 
    % { Get-Content -Path $_ }