Powershell中可以使用的.Net实用静态方法

所属分类: 脚本专栏 / PowerShell 阅读数: 1826
收藏 0 赞 0 分享

支持所有版本。

Powershell能够使用.net静态方法,比如下面有一些你常用的方法:

复制代码 代码如下:

[Math]::Round(7.9)
 
[Convert]::ToString(576255753217, 8)
 
[Guid]::NewGuid()
 
[Net.Dns]::GetHostByName('schulung12')
 
[IO.Path]::GetExtension('c:\test.txt')
 
[IO.Path]::ChangeExtension('c:\test.txt', 'bak')

要获得更多的方法,你可以删掉方括号后面的代码,再添加两个冒号,这时在Powershell_ISE中将会弹出这个类所有可选的方法及属性。在Powershell控制台中,你也可以使用TAB来获取选择方法属性。

当然你也可以通过管道符查看他所有类型:

复制代码 代码如下:

PS> [Math] | Get-Member -MemberType *Method -Static


   TypeName: System.Math

Name            MemberType Definition                                                                                    
----            ---------- ----------                                                                                    
Abs             Method     static sbyte Abs(sbyte value), static int16 Abs(int16 value), static int Abs(int value), sta...
Acos            Method     static double Acos(double d)          
Asin            Method     static double Asin(double d)           
Atan            Method     static double Atan(double d)           
Atan2           Method     static double Atan2(double y, double x)
BigMul          Method     static long BigMul(int a, int b)  
Ceiling         Method     static decimal Ceiling(decimal d), static double Ceiling(double a)
Cos             Method     static double Cos(double d)
Cosh            Method     static double Cosh(double value                                                            
DivRem          Method     static int DivRem(int a, int b, [ref] int result), static long DivRem(long a, long b, [ref] ...
Equals          Method     static bool Equals(System.Object objA, System.Object objB)
Exp             Method     static double Exp(double d)             
Floor           Method     static decimal Floor(decimal d), static double Floor(double d)
IEEERemainder   Method     static double IEEERemainder(double x, double y)
Log             Method     static double Log(double d), static double Log(double a, double newBase)
Log10           Method     static double Log10(double d)
Max             Method     static sbyte Max(sbyte val1, sbyte val2), static byte Max(byte val1, byte val2), static int1...
Min             Method     static sbyte Min(sbyte val1, sbyte val2), static byte Min(byte val1, byte val2), static int1...
Pow             Method     static double Pow(double x, double y)
ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System.Object objB)
Round           Method     static double Round(double a), static double Round(double value, int digits), static double ...
Sign            Method     static int Sign(sbyte value), static int Sign(int16 value), static int Sign(int value), stat...
Sin             Method     static double Sin(double a)
Sinh            Method     static double Sinh(double value)  
Sqrt            Method     static double Sqrt(double d)
Tan             Method     static double Tan(double a)
Tanh            Method     static double Tanh(double value)
Truncate        Method     static decimal Truncate(decimal d), static double Truncate(double d)

要查看方法的参数,省略掉他的括号即可:

复制代码 代码如下:

PS> Get-Something -Path test
You entered test.

PS> [Math]::Round

OverloadDefinitions                                                                                                      
-------------------                                                                                                      
static double Round(double a
static double Round(double value, int digits)
static double Round(double value, System.MidpointRounding mode)
static double Round(double value, int digits, System.MidpointRounding mode
static decimal Round(decimal d
static decimal Round(decimal d, int decimals)
static decimal Round(decimal d, System.MidpointRounding mode)
static decimal Round(decimal d, int decimals, System.MidpointRounding mode)


更多精彩内容其他人还在看

PowerShell操作Excel、CSV详细介绍

这篇文章主要介绍了PowerShell操作Excel、CSV详解,本文比较深入的探讨了PowerShell中如何操作Excel及CSV,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell中显示隐藏文件的方法

这篇文章主要介绍了Powershell中显示隐藏文件的方法,本文使用是是Get-ChildItem检索文件,加上-Hidden参数就可以显示隐藏文件了,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中iso8601格式日期和DateTime对象互转实例

这篇文章主要介绍了PowerShell中iso8601格式日期和DateTime对象互转实例,本文讲解了iso8601格式转换成DateTime对象、日期时间转换成iso8601格式两个方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell使用C#实现缩写路径

这篇文章主要介绍了Powershell使用C#实现缩写路径,缩写路径有时候是非常有用的,比如某些报表的路径太长会很难看,缩写后就会好看许多,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell截取字符串并添加省略号的例子

这篇文章主要介绍了Powershell截取字符串并添加省略号的例子,本文直接给出代码实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell读取本机注册表中的所有软件关联扩展名

这篇文章主要介绍了Powershell读取本机注册表中的所有软件关联扩展名,本文直接给出实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell实现按类型排序

这篇文章主要介绍了Powershell实现按类型排序,本文直接给出实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell中打开网页实例

这篇文章主要介绍了Powershell中打开网页实例,本文直接给出实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell实现获取进程所有者

这篇文章主要介绍了PowerShell实现获取进程所有者,本文直接给出实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Windows Powershell Foreach 循环

Foreach-object 为cmdlet命令,使用在管道中,对管道结果逐个处理,foreach为遍历集合的关键字。
收藏 0 赞 0 分享
查看更多