2016-06-23 196 views
2

我想使用Powershell 2.0找到文件的父目录。Powershell分割路径失败,路径变量源自get-itemproperty

我从注册表项找到文件的位置,如下所示:

(get-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\services\CCUMessageConnectionServer").ImagePath 

"O:\Program Files (x86)\CosmoCom\Server Components\Message Connection Server.exe" 

我想这条道路上使用split-path,获取父目录。我试试下面这失败:

PS C:> $a = (get-itemproperty `-path "HKLM:\SYSTEM\CurrentControlSet\services\`CCUMessageConnectionServer").ImagePath 

PS C:> split-path $a 

Split-Path : Cannot find drive. A drive with the name '"O' does not exist. 
At line:1 char:11 
+ split-path <<<< $a 
    + CategoryInfo   : ObjectNotFound: ("O:String) [Split-Path], `DriveNotFoundException` 
    + FullyQualifiedErrorId : `DriveNotFound,Microsoft.PowerShell.Commands.SplitPathCommand` 

如果我手动分配的get-itemproperty返回,如下然后分离路径文字作品

PS C:> $a = "O:\Program Files (x86)\CosmoCom\Server Components\Message Connection Server.exe" 

PS C:> split-path $a 
O:\Program Files (x86)\CosmoCom\Server Components 

PS C:> 

我已经管道都$aget-itemproperty命令为get-member和都返回相同的成员:

TypeName: System.String 

Name    MemberType   Definition 
----    ----------   ---------- 
Clone   Method    System.Object Clone() 
CompareTo  Method    int CompareTo(System.Object value), int CompareTo(string strB) 
Contains   Method    bool Contains(string value) 
CopyTo   Method    System.Void CopyTo(int sourceIndex, char[] destination, int destinationIndex,... 
EndsWith   Method    bool EndsWith(string value), bool EndsWith(string value, System.StringCompari... 
Equals   Method    bool Equals(System.Object obj), bool Equals(string value), bool Equals(string... 
GetEnumerator Method    System.CharEnumerator GetEnumerator() 
GetHashCode  Method    int GetHashCode() 
GetType   Method    type GetType() 
GetTypeCode  Method    System.TypeCode GetTypeCode() 
IndexOf   Method    int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf... 
IndexOfAny  Method    int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), i... 
Insert   Method    string Insert(int startIndex, string value) 
IsNormalized  Method    bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normaliz... 
LastIndexOf  Method    int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int... 
LastIndexOfAny Method    int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startI... 
Normalize  Method    string Normalize(), string Normalize(System.Text.NormalizationForm normalizat... 
PadLeft   Method    string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar) 
PadRight   Method    string PadRight(int totalWidth), string PadRight(int totalWidth, char padding... 
Remove   Method    string Remove(int startIndex, int count), string Remove(int startIndex) 
Replace   Method    string Replace(char oldChar, char newChar), string Replace(string oldValue, s... 
Split   Method    string[] Split(Params char[] separator), string[] Split(char[] separator, int... 
StartsWith  Method    bool StartsWith(string value), bool StartsWith(string value, System.StringCom... 
Substring  Method    string Substring(int startIndex), string Substring(int startIndex, int length) 
ToCharArray  Method    char[] ToCharArray(), char[] ToCharArray(int startIndex, int length) 
ToLower   Method    string ToLower(), string ToLower(System.Globalization.CultureInfo culture) 
ToLowerInvariant Method    string ToLowerInvariant() 
ToString   Method    string ToString(), string ToString(System.IFormatProvider provider) 
ToUpper   Method    string ToUpper(), string ToUpper(System.Globalization.CultureInfo culture) 
ToUpperInvariant Method    string ToUpperInvariant() 
Trim    Method    string Trim(Params char[] trimChars), string Trim() 
TrimEnd   Method    string TrimEnd(Params char[] trimChars) 
TrimStart  Method    string TrimStart(Params char[] trimChars) 
Chars   ParameterizedProperty char Chars(int index) {get;} 
Length   Property    System.Int32 Length {get;} 
+0

为什么不尝试将注册表值转换为字符串? '$ a = [string](get-itemproperty -path“HKLM:\ SYSTEM \ CurrentControlSet \ services \'CCUMessageConnectionServer”)。ImagePath' – sodawillow

+0

感谢您的建议。我试过了,但现在失败了,如下所示:PS C:> $ a = [string](get-itemproperty -path“HKLM:\ SYSTEM \ CurrentControlSet \ services \ CCUMessageConnectionServer”)。ImagePath PS C:> $ a “ O:\ Program Files(x86)\ CosmoCom \ Server Components \ Message Connection Server.exe“ PS C:>分割路径$ a 分割路径:无法找到驱动器。名称为“O”的驱动器不存在。 在线:1 char:11 +分割路径<<<< $ a + CategoryInfo:(“O:String)[Split-Path],DriveNotFoundException + FullyQualifiedErrorId:DriveNotFound,Microsoft.PowerShell.Commands.SplitPathCommand – HBrax64

回答

0

您可以使用.NET方法GetDirectoryName

[io.path]::GetDirectoryName($a) 
+0

感谢您的建议。不幸的是,它失败如下: PS C:> [io.path] :: GetDirectoryName($ a) 调用具有“1”参数的“GetDirectoryName”的异常:“路径中的非法字符。 在行:1字符:28 + [io.path] :: GetDirectoryName <<<<($ A) + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:DotNetMethodException PS C:> $ a “O:\ Program Files(x86)\ CosmoCom \ Server Components \ Message Connection Server.exe” – HBrax64