PowerShell的帮助系统指南
PowerShell分对象,属性和方法。PowerShell中的命令由“动词”+“名词”组成。帮助系统主要由 Get-Help 和 Get-Member 和 Get-Command 组成。
1、层级。模块由多个对象组成,对象由包含属性和方法。
2、所有命令的解释可由 Get-Help 完成,如: Get-Help Get-ChildItem
3、利用Get系列命令获得对象后,用 Get-Member 获得该对象的属性和方法。如 Get-ChildItem | Get-Member
PS C:\Users\jack\test> Get-ChildItem | Get-Member TypeName: System.IO.FileInfo Name MemberType Definition ---- ---------- ---------- Target AliasProperty Target = LinkTarget LinkType CodeProperty System.String LinkType{get=GetLinkType;} Mode CodeProperty System.String Mode{get=Mode;} ModeWithoutHardLink CodeProperty System.String ModeWithoutHardLink{get=ModeWithoutHardLink;} AppendText Method System.IO.StreamWriter AppendText() CopyTo Method System.IO.FileInfo CopyTo(string destFileName), System.IO.FileInfo CopyTo(str… Create Method System.IO.FileStream Create() CreateAsSymbolicLink Method void CreateAsSymbolicLink(string pathToTarget) CreateText Method System.IO.StreamWriter CreateText() Decrypt Method void Decrypt() Delete Method void Delete() Encrypt Method void Encrypt() 。。。省略
看到上面就知道,利用 Get-ChildItem 获得的是 System.IO.FileInfo 对象。
4、那么我想查看FileInfo对象里面的CopyTo方法,怎么用,该怎么查?
PS C:\Users\jack\test> (Get-ChildItem).CopyTo | Get-Member TypeName: System.Management.Automation.PSMethod`1[[System.Management.Automation.MethodGroup`2[[System.Func`3[[System .Array, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],这个TypeName很长,省略。。。 Name MemberType Definition ---- ---------- ---------- Copy Method System.Management.Automation.PSMemberInfo Copy() Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() Invoke Method System.Object Invoke(Params System.Object[] arguments) ToString Method string ToString() IsInstance Property bool IsInstance {get;} MemberType Property System.Management.Automation.PSMemberTypes MemberType {get;} Name Property string Name {get;} OverloadDefinitions Property System.Collections.ObjectModel.Collection[string] OverloadDefinitions {get;} TypeNameOfValue Property string TypeNameOfValue {get;} Value Property System.Object Value {get;set;}
5、看到上面显示了CopyTo里面仍有很多属性Property和方法Method,可以给 Get-Member 添加参数来查看指定内容
先看一下 Get-Member 命令的帮助,查看下它有什么参数可用
PS C:\Users\jack\test> Get-Help Get-Member NAME Get-Member SYNTAX Get-Member [[-Name] <string[]>] [-InputObject <psobject>] [-MemberType {AliasProperty | CodeProperty | Property | NoteProperty | ScriptProperty | PropertySet | Method | CodeMethod | ScriptMethod | Methods | ParameterizedProperty | MemberSet | Event | Dynamic | InferredProperty | Properties | All}] [-View {Extended | Adapted | Base | All}] [-Static] [-Force] [<CommonParameters>] ALIASES gm
找到了,Property参数,Method参数。试一试。
PS C:\Users\jack\test> Get-ChildItem | Get-Member -MemberType Property TypeName: System.IO.FileInfo Name MemberType Definition ---- ---------- ---------- Attributes Property System.IO.FileAttributes Attributes {get;set;} CreationTime Property datetime CreationTime {get;set;} CreationTimeUtc Property datetime CreationTimeUtc {get;set;} Directory Property System.IO.DirectoryInfo Directory {get;} DirectoryName Property string DirectoryName {get;} Exists Property bool Exists {get;} Extension Property string Extension {get;} FullName Property string FullName {get;} IsReadOnly Property bool IsReadOnly {get;set;} LastAccessTime Property datetime LastAccessTime {get;set;} LastAccessTimeUtc Property datetime LastAccessTimeUtc {get;set;} LastWriteTime Property datetime LastWriteTime {get;set;} LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;} Length Property long Length {get;} LinkTarget Property string LinkTarget {get;} Name Property string Name {get;}
这时成员的基本属性,还有其他的属性,要用 Properties 来查
PS C:\Users\jack\test> Get-ChildItem | Get-Member -MemberType Properties TypeName: System.IO.FileInfo Name MemberType Definition ---- ---------- ---------- Target AliasProperty Target = LinkTarget LinkType CodeProperty System.String LinkType{get=GetLinkType;} Mode CodeProperty System.String Mode{get=Mode;} ModeWithoutHardLink CodeProperty System.String ModeWithoutHardLink{get=ModeWithoutHardLink;} PSChildName NoteProperty string PSChildName=t1-ro.txt PSDrive NoteProperty PSDriveInfo PSDrive=C PSIsContainer NoteProperty bool PSIsContainer=False PSParentPath NoteProperty string PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\Users\jack\test PSPath NoteProperty string PSPath=Microsoft.PowerShell.Core\FileSystem::C:\Users\jack\test\t1-ro.txt PSProvider NoteProperty ProviderInfo PSProvider=Microsoft.PowerShell.Core\FileSystem Attributes Property System.IO.FileAttributes Attributes {get;set;} CreationTime Property datetime CreationTime {get;set;} CreationTimeUtc Property datetime CreationTimeUtc {get;set;} Directory Property System.IO.DirectoryInfo Directory {get;} DirectoryName Property string DirectoryName {get;} Exists Property bool Exists {get;} Extension Property string Extension {get;} FullName Property string FullName {get;} IsReadOnly Property bool IsReadOnly {get;set;} LastAccessTime Property datetime LastAccessTime {get;set;} LastAccessTimeUtc Property datetime LastAccessTimeUtc {get;set;} LastWriteTime Property datetime LastWriteTime {get;set;} LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;} Length Property long Length {get;} LinkTarget Property string LinkTarget {get;} Name Property string Name {get;} BaseName ScriptProperty System.Object BaseName {get=if ($this.Extension.Length -gt 0){$this.Name.Remove($this.Name.Length - $this.Extension.Length)}else{$this.Name};} VersionInfo ScriptProperty System.Object VersionInfo {get=[System.Diagnostics.FileVersionInfo]::GetVersionInfo($this.FullName);}
看到没,多个几个其他属性。
6、有时显示一堆很长,查找指定的属性。下例查找,再Get-Process获得进程中,查找名字是MachineName的进程的介绍。
Get-Process | Get-Member -Name MachineName
更复杂一点,查找多个:
$list = "Get-Process", "Get-Service", "Get-Culture", "Get-PSDrive", "Get-ExecutionPolicy" foreach ($cmdlet in $list) {& $cmdlet | Get-Member -Name MachineName}
7、可以直接查看直接量或变量的属性
PS C:\Users\jack\test> 1 | Get-Member TypeName: System.Int32 Name MemberType Definition ---- ---------- ---------- CompareTo Method int CompareTo(System.Object value), int CompareTo(int value), int IComparable.CompareTo(System.… Equals Method bool Equals(System.Object obj), bool Equals(int obj), bool IEquatable[int].Equals(int other) GetHashCode Method int GetHashCode() GetType Method type GetType() GetTypeCode Method System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode() ToBoolean Method bool IConvertible.ToBoolean(System.IFormatProvider provider) ToByte Method byte IConvertible.ToByte(System.IFormatProvider provider) ToChar Method char IConvertible.ToChar(System.IFormatProvider provider) ToDateTime Method datetime IConvertible.ToDateTime(System.IFormatProvider provider) ToDecimal Method decimal IConvertible.ToDecimal(System.IFormatProvider provider) ToDouble Method double IConvertible.ToDouble(System.IFormatProvider provider) ToInt16 Method short IConvertible.ToInt16(System.IFormatProvider provider) ToInt32 Method int IConvertible.ToInt32(System.IFormatProvider provider) ToInt64 Method long IConvertible.ToInt64(System.IFormatProvider provider) ToSByte Method sbyte IConvertible.ToSByte(System.IFormatProvider provider) ToSingle Method float IConvertible.ToSingle(System.IFormatProvider provider) ToString Method string ToString(), string ToString(string format), string ToString(System.IFormatProvider provi… ToType Method System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider) ToUInt16 Method ushort IConvertible.ToUInt16(System.IFormatProvider provider) ToUInt32 Method uint IConvertible.ToUInt32(System.IFormatProvider provider) ToUInt64 Method ulong IConvertible.ToUInt64(System.IFormatProvider provider) TryFormat Method bool TryFormat(System.Span[char] destination, [ref] int charsWritten, System.ReadOnlySpan[char]…
PS C:\Users\jack\test> $a = "hello" PS C:\Users\jack\test> $a | Get-Member TypeName: System.String Name MemberType Definition ---- ---------- ---------- Clone Method System.Object Clone(), System.Object ICloneable.Clone() CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB), int ICompar… Contains Method bool Contains(string value), bool Contains(string value, System.StringCompa… CopyTo Method void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int … EndsWith Method bool EndsWith(string value), bool EndsWith(string value, System.StringCompa… EnumerateRunes Method System.Text.StringRuneEnumerator EnumerateRunes() Equals Method bool Equals(System.Object obj), bool Equals(string value), bool Equals(stri… GetEnumerator Method System.CharEnumerator GetEnumerator(), System.Collections.IEnumerator IEnum… GetHashCode Method int GetHashCode(), int GetHashCode(System.StringComparison comparisonType) GetPinnableReference Method System.Char&, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, Pub… GetType Method type GetType() GetTypeCode Method System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode() IndexOf Method int IndexOf(char value), int IndexOf(char value, int startIndex), int Index… IndexOfAny Method int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex),… Insert Method string Insert(int startIndex, string value) IsNormalized Method bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normal… LastIndexOf Method int LastIndexOf(string value, int startIndex), int LastIndexOf(string value… LastIndexOfAny Method int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int star… Normalize Method string Normalize(), string Normalize(System.Text.NormalizationForm normaliz… PadLeft Method string PadLeft(int totalWidth), string PadLeft(int totalWidth, char padding… PadRight Method string PadRight(int totalWidth), string PadRight(int totalWidth, char paddi… Remove Method string Remove(int startIndex, int count), string Remove(int startIndex) Replace Method string Replace(string oldValue, string newValue, bool ignoreCase, culturein… ReplaceLineEndings Method string ReplaceLineEndings(), string ReplaceLineEndings(string replacementTe… Split Method string[] Split(char separator, System.StringSplitOptions options), string[]… StartsWith Method bool StartsWith(string value), bool StartsWith(string value, System.StringC… Substring Method string Substring(int startIndex), string Substring(int startIndex, int leng… ToBoolean Method bool IConvertible.ToBoolean(System.IFormatProvider provider) ToByte Method byte IConvertible.ToByte(System.IFormatProvider provider) ToChar Method char IConvertible.ToChar(System.IFormatProvider provider) ToCharArray Method char[] ToCharArray(), char[] ToCharArray(int startIndex, int length) ToDateTime Method datetime IConvertible.ToDateTime(System.IFormatProvider provider) ToDecimal Method decimal IConvertible.ToDecimal(System.IFormatProvider provider) ToDouble Method double IConvertible.ToDouble(System.IFormatProvider provider) ToInt16 Method short IConvertible.ToInt16(System.IFormatProvider provider) ToInt32 Method int IConvertible.ToInt32(System.IFormatProvider provider) ToInt64 Method long IConvertible.ToInt64(System.IFormatProvider provider) ToLower Method string ToLower(), string ToLower(cultureinfo culture) ToLowerInvariant Method string ToLowerInvariant() ToSByte Method sbyte IConvertible.ToSByte(System.IFormatProvider provider) ToSingle Method float IConvertible.ToSingle(System.IFormatProvider provider) ToString Method string ToString(), string ToString(System.IFormatProvider provider), string… ToType Method System.Object IConvertible.ToType(type conversionType, System.IFormatProvid… ToUInt16 Method ushort IConvertible.ToUInt16(System.IFormatProvider provider) ToUInt32 Method uint IConvertible.ToUInt32(System.IFormatProvider provider) ToUInt64 Method ulong IConvertible.ToUInt64(System.IFormatProvider provider) ToUpper Method string ToUpper(), string ToUpper(cultureinfo culture) ToUpperInvariant Method string ToUpperInvariant() Trim Method string Trim(), string Trim(char trimChar), string Trim(Params char[] trimCh… TrimEnd Method string TrimEnd(), string TrimEnd(char trimChar), string TrimEnd(Params char… TrimStart Method string TrimStart(), string TrimStart(char trimChar), string TrimStart(Param… TryCopyTo Method bool TryCopyTo(System.Span[char] destination) Chars ParameterizedProperty char Chars(int index) {get;} Length Property int Length {get;}
8、Get-Member 关于Force参数。
将内部成员和编译器生成的get_和set_方法添加到显示中。以下列表描述了使用 Force 参数时添加的属性: PSBase:这些是为对象类定义的属性。 PSAdapted:PowerShell 扩展类型系统中定义的属性和方法。 PSExtended:添加到文件中或使用 cmdlet 添加的属性和方法。 PSObject:将基本对象转换为 PowerShell PSObject 对象的适配器。 PSTypeNames:按特定性顺序描述对象的对象类型列表。设置对象格式时,PowerShell 会在 PowerShell 安装目录 () 的文件中搜索类型。它使用找到的第一个类型的格式设置定义。