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

所属分类: 脚本专栏 / PowerShell 阅读数: 1901
收藏 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遍历文件、文件夹的方法

这篇文章主要介绍了PowerShell遍历文件、文件夹的方法,本文使用Get-ChildItem命令实现,需要的朋友可以参考下
收藏 0 赞 0 分享

使用PowerShell操作Windows服务的命令小结

这篇文章主要介绍了使用PowerShell操作Windows服务的命令小结,本文只是做了一个命令列表,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中调用外部程序和进程操作命令例子

这篇文章主要介绍了PowerShell中调用外部程序和进程操作命令例子,给出了进程操作的一些命令和调用外部应用程序的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中正则表达式使用例子

这篇文章主要介绍了PowerShell中正则表达式使用例子,本文用实例来说明如何使用正则匹配到想要的内容,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell是什么?

这篇文章主要介绍了PowerShell是什么?本文解读了PowerShell的一些术语,对PowerShell做了一个完全介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell直接脚本时出现无法加载文件因为禁止执行脚本

Powershell直接脚本时出现无法加载文件因为在此系统中禁止执行脚本,有关此问题的解决方法如下
收藏 0 赞 0 分享

Tornado中database模块被取消的替代方法

这篇文章主要介绍了Tornado中database模块被取消的替代方法,新的方法是使用torndb模块,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell学习笔记--使用正则表达式查找文件

本文介绍PowerShell中使用正则表达式的查找文件的方法,PowerShell的正则表达式与微软其它语言的正则表达式是一样的,使用非常方便。
收藏 0 赞 0 分享

Windows Powershell 介绍和安装

Powershell 是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境。微软之所以将Powershell 定位为Power,并不是夸大其词,因为它完全支持对象。其可读性,易用性,可以位居当前所有shell之首。
收藏 0 赞 0 分享

Windows Powershell 自定义控制台

这篇文章主要介绍了Windows Powershell 自定义控制台,包括选项、字体、布局和颜色四个方面的自定义风格,希望对大家有所帮助
收藏 0 赞 0 分享
查看更多