gc "C:\folder1\folder2\MyService.exe.config"
Run Code Online (Sandbox Code Playgroud)
都好
gwmi win32_service|?{$_.name -match "MyService"} | % {$_.pathname}
"C:\folder1\folder2\MyService.exe.config"
Run Code Online (Sandbox Code Playgroud)
返回正确的路径
gwmi win32_service|?{$_.name -match "Mailing"} | % {$_.pathname} | % {$_.gettype()}
Run Code Online (Sandbox Code Playgroud)
返回类型肯定是一个字符串
gwmi win32_service|?{$_.name -match "Mailing"} | % {$_.pathname} | % {gc -path $_}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我在这里想念什么?
仔细查看错误消息:
名称为“ C”的驱动器不存在。
^
注意"驱动器号前面的。
WMI查询返回的路径在双引号之间,即双引号不像您的第一个语句中那样分隔字符串,而是字符串的一部分,因此Get-Content失败,因为找不到驱动器"C:。
示范:
PS C:\> $ path =“ C:\ temp \ web.config”
PS C:\> $ path
C:\ temp \ web.config
PS C:\> 获取内容$ path
<?xml version =“ 1.0” encoding =“ utf-8”吗?>
<配置>
...
</ configuration>
PS C:\> $ path ='“ C:\ temp \ web.config”'
PS C:\> $ path
“ C:\ temp \ web.config”
PS C:\> 获取内容$ path
获取内容:找不到驱动器。名称为“ C”的驱动器不存在。
在第1行:char:1
+获取内容$ path
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo:ObjectNotFound :(“ C:String)[Get-Content],DriveNotFoundException
+ FullyQualifiedErrorId:DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand
删除字符串开头和结尾的双引号,问题将消失:
A drive with the name '"C' does not exist.
^
| 归档时间: |
|
| 查看次数: |
3209 次 |
| 最近记录: |