• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >PowerShell > Windows Powershell方法(对象能做什么)

Windows Powershell方法(对象能做什么)

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了windows powershell,windowspowershell2.0,windows powershell64,windows10 powershell,windows7 powershell等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

方法定义了一个对象可以做什么事情。当你把一个对象输出在控制台时,它的属性可能会被转换成可视的文本。但是它的方法却不可见。列出一个对象的所有方法可是使用Get-Member命令,给“MemeberType”参数 传入“Method”:

PS C:Powershell> $Host | Get-Member -MemberType Method

   TypeName: System.Management.Automation.Internal.Host.InternalHost

Name                     MemberType Definition
----                     ---------- ----------
EnterNestedPrompt       Method     System.Void EnterNestedPrompt()
Equals                   Method     bool Equals(System.Object obj)
ExitNestedPrompt        Method     System.Void ExitNestedPrompt()
GetHashCode             Method     int GetHashCode()
GetType                  Method     type GetType()
NotifyBeginApplication  Method     System.Void NotifyBeginApplication()
NotifyEndApplication    Method     System.Void NotifyEndApplication()
PopRunspace             Method     System.Void PopRunspace()
PushRunspace            Method     System.Void PushRunspace(runspace runspace)
SetShouldExit            Method     System.Void SetShouldExit(int exitCode)
ToString                 Method     string ToString()
</div>

过滤内部方法

Get-Memeber列出了一个对象定义的所有方法,但并不是所有的方法都有用,有些方法的的用处非常有限。

Get_ 和 Set_ 方法

所有名称以”get_”打头的方法都是为了给对应的属性返回一个值。例如”get_someInfo()”方法的作用就是返回属性someInfo的值,因此可以直接通过属性调用。

PS C:Powershell> $Host.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1

PS C:Powershell> $Host.get_Version()

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1
</div>

类似的象”set_someinfo”一样,该方法只是为了给属性someinfo赋值,可以直接通过属性赋值调用。如果一个对象中只有”get_someinfo”,没有对应的”set_someinfo”,说明someinfo这个属性为只读属性。

标准方法

几乎每个对象都有一些继承自父类的方法,这些方法并不是该对象所特有的方法,而是所有对象共有的方法。
Equals 比较两个对象是否相同
GetHashCode 返回一个对象的数字格式的指纹
GetType 返回一个对象的数据类型
ToString 将一个对象转换成可读的字符串

过滤包含了下划线的方法可是使用操作符 -notlike 和 通配符 *

PS C:Powershell> $Host.UI.RawUI | Get-Member -me method | where {$_.Name -notlike '*_*'}

   TypeName: System.Management.Automation.Internal.Host.InternalHostRawUserInterface

Name                 MemberType Definition
----                 ---------- ----------
Equals               Method     bool Equals(System.Object obj)
FlushInputBuffer      Method     System.Void FlushInputBuffer()
GetBufferContents    Method     System.Management.Automation.Host.BufferCell[,] GetBufferCo
GetHashCode           Method     int GetHashCode()
GetType               Method     type GetType()
LengthInBufferCells  Method     int LengthInBufferCells(string str), int LengthInBufferCell
NewBufferCellArray  Method     System.Management.Automation.Host.BufferCell[,] NewBufferCe
ReadKey               Method     System.Management.Automation.Host.KeyInfo ReadKey(System.Ma
ScrollBufferContents Method     System.Void ScrollBufferContents(System.Management.Automati
SetBufferContents    Method     System.Void SetBufferContents(System.Management.Automation.
ToString              Method     string ToString()
</div>

调用方法

一定要注意,在调用一个方法前,必须知道这个方法的功能。因为有的命令可能比较危险,例如错误地修改环境变量。调用一个方法,通过圆点加圆括号:
$Host.GetType()

调用带参数的方法

UI对象有很多实用的方法,可以通过get-member预览

PS C:Powershell> $Host.UI | Get-Member -MemberType method

   TypeName: System.Management.Automation.Internal.Host.InternalHostUserInterface

Name                   MemberType Definition
----                   ---------- ----------
Equals                 Method     bool Equals(System.Object obj)
GetHashCode            Method     int GetHashCode()
GetType                Method     type GetType()
Prompt                 Method     System.Collections.Generic.Dictionary[string,psob
PromptForChoice        Method     int Pro

您可能想查找下面的文章:

  • PowerShell使用match操作符来筛选数组
  • PowerShell连接SQL SERVER数据库进行操作的实现代码
  • powershell远程管理服务器磁盘空间的实现代码
  • Powershell 查询 Windows 日志的方法
  • Powershell 查找用户的主SMTP地址
  • Powershell 获取特定的网页信息的代码
  • Powershell错误处理之what-if
  • PowerShell 4.0实现自动化设置服务器
  • 揭秘PowerShell 5.0新特性和新功能
  • 简单谈谈PowerShell 4.0中的新命令

相关文章

  • 用PowerShell删除N天前或指定日期(前后)创建(或修改)的文件
  • PowerShel程序执行完后删除脚本自身的方法
  • PowerShell中文件对象的属性方法总结
  • PowerShell复制命令行历史命令方法
  • Powershell中打开网页实例
  • PowerShell脚本开发之对指定IP进行端口扫描
  • PowerShell判断某天是星期几的方法
  • Powershell小技巧之获取字符串的行数
  • 使用PowerShell操作Windows服务的命令小结
  • PowerShell中调用外部程序和进程操作命令例子

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 自定义PowerShell控制台提示符风格的方法
    • PowerShell使用Clear-Content命令删除、清空文件内容的例子
    • powershell解决win10开始菜单和通知中心无法打开
    • 探索PowerShell (四) PowerShell的对象、格式与参数
    • Powershell互斥参数使用实例
    • PowerShell脚本源码输出到文件的最佳写法
    • Powershell小技巧之查询AD用户
    • PowerShell中使用Get-EventLog读取、筛选系统日志的例子
    • PowerShell函数参数设置成自动识别数据类型的例子
    • 探索PowerShell(六) 脚本基础简要

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有