powershell完全学习手册_第1页
powershell完全学习手册_第2页
powershell完全学习手册_第3页
powershell完全学习手册_第4页
powershell完全学习手册_第5页
已阅读5页,还剩86页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

精品文档 1欢迎下载 Powershell 定义变量 Powershell 程序设计 添加评论 十二 052011 变量可以临时保存数据 因此可以把数据保存在变量中 以便进一步操作 帮助 01 02 03 04 05 06 07 08 09 10 11 定义变量 a 10 b 4 计算变量 result a b msg 保存文本 输出变量 result msg 40 保存文本 powershell 不需要显示地去声明 可以自动创建变量 只须记住变量的前缀为 创建好了变量后 可以通过变量名输出变量 也可以把变量名存在字符串中 但是有个例 外单引号中的字符串不会识别和处理变量名 选择变量名选择变量名 在 powershell 中变量名均是以美元符 开始 剩余字符可以是数字 字母 下划线的任 意字符 并且 powershell 变量名大小写不敏感 a 和 A 是同一个变量 某些特殊的字符在 powershell 中有特殊的用途 一般不推荐使用这些字符作为变量名 当 然你硬要使用 请把整个变量名后缀用花括号括起来 PS C I like mossfly PS C I like 精品文档 2欢迎下载 mossfly 赋值和返回值赋值和返回值 赋值操作符为 几乎可以把任何数据赋值给一个变量 甚至一条 cmdlet 命令 为什么 因为 Powershell 支持对象 对象可以包罗万象 PS C item Get ChildItem PS C item Directory C Mode LastWriteTime Length Name d 2011 11 23 17 25 ABC a 2011 11 24 18 30 67580 a html a 2011 11 24 20 04 26384 a txt a 2011 11 24 20 26 12060 alias a 2011 11 24 20 27 12060 alias ps1 a 2011 11 23 17 25 0 b txt a 2011 11 23 17 25 0 c txt 精品文档 3欢迎下载 a 2011 11 23 17 25 0 d txt a 2011 11 25 11 20 556 employee xml a 2011 11 24 17 37 7420 name html a 2011 11 28 15 30 63 ping bat a 2011 11 24 17 44 735892 Powershell Cmdlets html a 2011 11 28 17 03 60 test ps1 a 2011 11 23 17 37 242 test txt a 2011 11 28 16 42 170 test vbs PS C result 3000 1 12 0 0075 PS C result 272 5 给多个变量同时赋值给多个变量同时赋值 赋值操作符不仅能给一个变量赋值 还可以同时给多个变量赋相同的值 PS C a b c 123 PS C a 123 PS C b 精品文档 4欢迎下载 123 PS C c 123 交换变量的值交换变量的值 要交换两个变量的值 传统的程序语言至少需要三步 并且还需定义一个中间临时变量 Value1 10 Value2 20 Temp Value1 Value1 Value2 Value2 Temp 在 powershell 中 交换两个变量的值 这个功能变得非常简单 PS C value1 10 PS C value2 20 PS C value1 value2 value2 value1 PS C value1 20 PS C value2 精品文档 5欢迎下载 10 查看正在使用的变量查看正在使用的变量 Powershell 将变量的相关信息的记录存放在名为 variable 的驱动中 如果要查看所有定 义的变量 可以直接遍历 variable PS C ls variable Name Value I like mossfly cls True cls 1 1 a 123 args b 123 c 123 精品文档 6欢迎下载 ConfirmPreference High ConsoleFileName DebugPreference SilentlyContinue 查找变量查找变量 因为有虚拟驱动 variable 的存在 可以象查找文件那样使用通配符查找变量 例如要查 询以 value 打头的变量名 PS C ls variable value Name Value value1 20 value2 10 验证变量是否存在验证变量是否存在 验证一个变量是否存在 仍然可以象验证文件系统那样 使用 cmdlet Test Path 为什么 因为变量存在变量驱动器中 PS C Test Path variable value1 True 精品文档 7欢迎下载 PS C Test Path variable value2 True PS C Test Path variable valueUnkonw False 删除变量删除变量 因为变量会在 powershell 退出或关闭时 自动清除 一般没必要删除 但是你非得删除 也可以象删除文件那样删除它 PS C Test Path variable value1 True PS C del variable value1 PS C Test Path variable value1 False 使用专用的变量命令使用专用的变量命令 为了管理变量 powershell 提供了五个专门管理变量的命令 Clear Variable Get Variable New Variable Remove Variable Set Variable 因为虚拟驱动器 variable 的存在 clear remove set 打头的命令可以被代替 但是 Get Variable New Variable 却非常有用 new variable 可以在定义变量时 指定变量的一些其它属性 比如 访问权限 同样 Get Variable 也可以获取这些附加信息 精品文档 8欢迎下载 变量写保护变量写保护 可以使用 New Variable 的 option 选项 在创建变量时 给变量加上只读属性 这样就不 能给变量重新赋值了 PS C New Variable num Value 100 Force Option readonly PS C num 101 Cannot overwrite variable num because it is read only or constant At line 1 char 5 num del Variable num Remove Item Cannot remove variable num because it is constant or read only If the variable is read only ration again specifying the Force option At line 1 char 4 del del Variable num Force PS C num 101 PS C num 101 有没有权限更高的变量 有 那就是 选项 Constant 常量一旦声明 不可修改 PS C new variable num Value strong Option constant PS C num why can not delete it Cannot overwrite variable num because it is read only or constant At line 1 char 5 num del Variable num Force Remove Item Cannot remove variable num because it is constant or read only If the variable is read only ration again specifying the Force option At line 1 char 4 del new variable name Value me Description This is my name PS C ls Variable name fl PSPath Microsoft PowerShell Core Variable name PSDrive Variable PSProvider Microsoft PowerShell Core Variable PSIsContainer False Name name Description This is my name Value me Visibility Public Module 精品文档 11欢迎下载 ModuleName Options None Attributes Powershell 自动化变量 Powershell 程序设计 添加评论 十二 072011 Powershell 自动化变量 是那些一旦打开 Powershell 就会自动加载的变量 这些变量一般存放的内容包括 用户信息用户信息 例如用户的根目录 home 配置信息配置信息 例如 powershell 控制台的大小 颜色 背景等 运行时信息运行时信息 例如一个函数由谁调用 一个脚本运行的目录等 PS C PowerShell HOME C Users test PS C PowerShell currentProcessID pid PS C PowerShell currentProcessID 5356 PS C PowerShell Get Process Id pid Handles NPM K PM K WS K VM M CPU s Id ProcessName 精品文档 12欢迎下载 390 10 30604 33100 172 1 11 5356 powershell PS C PowerShell PROFILE C Users test Documents WindowsPowerShell Microsoft PowerShell profil e ps1 powershell 中的某些自动化变量只能读 不能写 例如 Pid 可以通过 Get Help about Automatic variables 查看 Automatic variables 的帮助 TOPIC about Automatic Variables 主题主题 about Automatic Variables 简短说明简短说明 说明存储 Windows PowerShell 状态信息的变量 这些变量由 Windows PowerShell 创建并维护 详细说明详细说明 下面是 Windows PowerShell 中的自动变量的列表 包含会话所收到的最后一行中的最后一个令牌 包含最后一个操作的执行状态 如果最后一个操作成功 则包含 TRUE 失败则包含 FALSE 包含会话所收到的最后一行中的第一个令牌 精品文档 13欢迎下载 包含管道对象中的当前对象 在对管道中的每个对象或所选对象执行操作的命令中 可以 使用此变量 Args Args 包含由未声明参数和 或传递给函数 脚本或脚本块的参数值组成的数组 在创建函数时可以声明参数 方法是使用 param 关键字或在函数名称后添加以圆括号括起 逗号 分隔的参数列表 ConsoleFileName ConsoleFileName 包含在会话中最近使用的控制台文件 psc1 的路径 在通过 PSConsoleFile 参数启动 Windows PowerShell 或使用 Export Console cmdlet 将管理单元名称导出到控制台文件 时 将填充此变量 在使用不带参数的 Export Console cmdlet 时 它自动更新在会话中最近使用的控制台文 件 可以使用此自动变量确定要更新的文件 Error Error 包含错误对象的数组 这些对象表示最近的一些错误 最近的错误是该数组中的第一个错 误对象 Error 0 Event Event 包含一个 PSEventArgs 对象 该对象表示一个正在被处理的事件 此变量只在事件注册命令 例如 Register ObjectEvent 的 Action 块内填充 此变量的值是 Get Event cmdlet 返回的同一个对象 因此 可以在 Action 脚本块中使用 Event 变量的属性 例如 Event TimeGenerated EventSubscriber EventSubscriber 包含一个 PSEventSubscriber 对象 该对象表示正在被处理的事件的事件订阅者 此变量只在事件注册命令的 Action 块内填充 此变量的值 是 Get EventSubscriber cmdlet 返回的同一个对象 精品文档 14欢迎下载 ExecutionContext ExecutionContext 包含一个 EngineIntrinsics 对象 该对象表示 Windows PowerShell 主机的执行上下文 可以使用此变量来查找可用于 cmdlet 的执行对象 False False 包含 FALSE 可以使用此变量在命令和脚本中表示 FALSE 而不是使用字符串 false 如 果 该字符串转换为非空字符串或非零整数 则可将该字符串解释为 TRUE ForEach ForEach 包含 ForEach Object 循环的枚举数 可以对 ForEach 变量的值使用枚举数的属性和方 法 此变量仅在运行 For 循环时存在 循环完成即会删除 Home Home 包含用户的主目录的完整路径 此变量等效于 homedrive homepath 环境变量 Host Host 包含一个对象 该对象表示 Windows PowerShell 的当前主机应用程序 可以使用此变量 在命 令中表示当前主机 或者显示或更改主机的属性 如 Host version Host CurrentCulture 或 host ui rawui setbackgroundcolor Red Input Input 一个枚举数 它包含传递给函数的输入 Input 变量区分大小写 只能用于函数和脚本块 脚 本块本质上是未命名的函数 在函数的 Process 块中 Input 变量包含当前位于管道 中的对 象 在 Process 块完成后 Input 的值为 NULL 如果函数没有 Process 块 则 Input 的值可用于 End 块 它包含函数的所有输入 LastExitCode LastExitCode 包含运行的最后一个基于 Windows 的程序的退出代码 精品文档 15欢迎下载 Matches Matches Matches 变量与 match 和 not match 运算符一起使用 将标量输入提交给 match 或 notmatch 运算符时 如果检测到匹配 则会返回一个布尔 值 并使用由所有匹配字符串值组成的哈希表填充 Matches 自动变量 有关 match 运算符 的详细 信息 请参阅 about comparison operators MyInvocation MyInvocation 包含一个对象 该对象具有有关当前命令 如脚本 函数或脚本块 的信息 可以使用该 对象中的 信息 如脚本的路径和文件名 myinvocation mycommand path 或函数的名称 myinvocation mycommand name 来标识当前命令 对于查找正在运行的脚本的名称 这非常有用 NestedPromptLevel NestedPromptLevel 包含当前提示级别 值 0 指示原始提示级别 该值在进入嵌套级别时递增 在退出嵌套级 别时递减 例如 在使用 Host EnterNestedPrompt 方法时 Windows PowerShell 会出现嵌套命令 提示符 在 Windows PowerShell 调试程序中到达断点时 Windows PowerShell 也会出现 嵌 套命令提示符 在进入嵌套提示时 Windows PowerShell 暂停当前命令 保存执行上下文 并递增 NestedPromptLevel 变量的值 要创建更多嵌套命令提示符 最多 128 级 或返回到原 始命 令提示符 请完成命令 或键入 exit NestedPromptLevel 变量有助于跟踪提示级别 可以创建包含此值的备用 Windows PowerShell 命令提示符 以使此值始终可见 NULL NULL 包含 NULL 或空值 可以在命令和脚本中使用此变量表示 NULL 而不是使用字符串 NULL 如果该字符串转换为非空字符串或非零整数 则可将该字符串解释为 TRUE 精品文档 16欢迎下载 PID PID 包含承载当前 Windows PowerShell 会话的进程的进程标识符 PID Profile 包含当前用户和当前主机应用程序的 Windows PowerShell 配置文件的完整路径 可以在 命令 中使用此变量表示配置文件 例如 可以在命令中使用此变量确定是否已创建某个配置文 件 test path profile 也可以在命令中使用此变量创建配置文件 new item type file path pshome force 此外 还可以在命令中使用此变量在记事本中打开配置文件 notepad profile PSBoundParameters 包含活动参数及其当前值的字典 只有在声明参数的作用域 如脚本或函数 中 此变量才有值 可以使用此变量显示或更改参数的当前值 也可以将参数值传递给 其他脚本或函数 例如 function test param a b Display the parameters in dictionary format psboundparameters Call the Test1 function with a and b test1 psboundparameters 精品文档 17欢迎下载 PsCmdlet PsCmdlet 包含一个对象 该对象表示正在运行的 cmdlet 或高级函数 可以在 cmdlet 或函数代码中使用该对象的属性和方法来响应使用的条件 例如 ParameterSetName 属性包含正在使用的参数集的名称 而 ShouldProcess 方法将 WhatIf 和 Confirm 参数动态添加到 cmdlet 有关 PSCmdlet 自动变量的详细信息 请参阅 about Functions Advanced PsCulture PsCulture 包含操作系统中当前所用的区域性的名称 区域性确定数字 货币和日期等项的显示格式 这是系 统的 System Globalization CultureInfo CurrentCulture Name 属性的值 要获取系统 的 System Globalization CultureInfo 对象 请使用 Get Culture cmdlet PSDebugContext PSDebugContext 在调试期间 此变量包含有关调试环境的信息 在其他时间 此变量包含 NULL 值 因此 可以使 用此变量指示调试程序是否拥有控制权 填充之后 此变量包含一个具有 Breakpoints 和 InvocationInfo 属性的 PsDebugContext 对象 InvocationInfo 属性有多个十分有用的 属性 包括 Location 属性 Location 属性指示正在调试的脚本的路径 PsHome PsHome 包含 Windows PowerShell 的安装目录的完整路径 通常为 windir System32 WindowsPowerShell v1 0 可以在 Windows PowerShell 文件 的路径中使用此变量 例如 下面的命令在概念性帮助主题中搜索 variable 一词 select string pattern variable path pshome txt PSScriptRoot PSScriptRoot 包含要从中执行脚本模块的目录 通过此变量 脚本可以使用模块路径来访问其他资源 PsUICulture PsUICulture 包含操作系统中当前所用的用户界面 UI 区域性的名称 UI 区域性确定哪些文本字符串 用于用户 精品文档 18欢迎下载 界面元素 如菜单和消息 这是系统的 System Globalization CultureInfo CurrentUICulture Name 属性的值 要获取系统 的 System Globalization CultureInfo 对象 请使用 Get UICulture cmdlet PsVersionTable PsVersionTable 包含一个只读哈希表 该哈希表显示有关在当前会话中运行的 Windows PowerShell 版本 的详 细信息 该表包括下列项 CLRVersion 公共语言运行时 CLR 的版本 BuildVersion 当前版本的内部版本号 PSVersion Windows PowerShell 版本号 WSManStackVersion WS Management 堆栈的版本号 PSCompatibleVersions 与当前版本兼容的 Windows PowerShell 版本 SerializationVersion 序列化方法的版本 PSRemotingProtocolVersion Windows PowerShell 远程管理协议的版本 Pwd Pwd 包含一个路径对象 该对象表示当前目录的完整路径 Sender Sender 包含生成此事件的对象 此变量只在事件注册命令的 Action 块内填充 此变量的值也可在 Get Event 返回的 PSEventArgs System Management Automation PSEventArgs 对象的 Sender 属性中找到 ShellID ShellID 包含当前 shell 的标识符 精品文档 19欢迎下载 SourceArgs SourceArgs 包含表示正在被处理的事件的事件参数的对象 此变量只在事件注册命令的 Action 块内填充 此变量的值也可在 Get Event 返回的 PSEventArgs System Management Automation PSEventArgs 对象的 SourceArgs 属性中找到 SourceEventArgs SourceEventArgs 包含一个对象 该对象表示从正在被处理的事件的 EventArgs 中派生出的 第一个事件参数 此变量只在事件注册命令的 Action 块内填充 此变量的值也可在 Get Event 返回的 PSEventArgs System Management Automation PSEventArgs 对象的 SourceArgs 属性中找到 This This 在定义脚本属性或脚本方法的脚本块中 This 变量引用要扩展的对象 True True 包含 TRUE 可以在命令和脚本中使用此变量表示 TRUE 另请参阅另请参阅 about Hash Tables about Preference Va riables about Variables Powershell 通过函数扩展别名 Powershell 程序设计 添加评论 十一 252011 在 Powershell 中设置别名的确方便快捷 但是在设置别名的过程中并设置参数的相关信息 尽管别名会自动识别参数 但是如何把经常使用的参数默认设定在别名里面呢 例如 Test Connection Count 2 ComputerName 让 Count 2 固化在别名中 这时简单的别名无法完成上述需求 可以通过函数来完成它 并且一旦把函数拉过来 定 义别名会变得更加灵活 精品文档 20欢迎下载 PS C function test conn Test Connection Count 2 ComputerName args PS C Set Alias tc test conn PS C tc localhost Source Destination IPV4Address IPV6Address Bytes Time ms test me 01 localhost 127 0 0 1 1 32 0 test me 01 localhost 127 0 0 1 1 32 0 有了函数牵线 别名可以完成更高级更强大的功能 其中 args 为参数的占位符 经测试 发现这个占位符必须以 args 命名 否则不能识别 会抛出异常 Cannot validate argument on parameter ComputerName The argument is null or empty Supply an arg nt that is not null or empty and then try the command again Powershell 快速编辑模式和标准模式 Powershell 程序设计 添加评论 十一 232011 powershell 控制台有两种模式 一个是快速编辑模式 一个是标准模式 快速编辑模式和标准模式的切换可以通过控制台标题栏 鼠标右击 属性 选项 编辑选 项 powershell 标准模式 精品文档 21欢迎下载 鼠标右击选择标记后才能实现复制和粘切功能 powershell 快速编辑模式 可以通过鼠标右键选择任意矩形区域内的文本 并且鼠标右击实现复制功能 Powershell 自定义控制台 Powershell 程序设计 添加评论 十一 232011 右击标题栏选择 属性 弹出 powershell 控制台对话框 在这里有三个四个选项卡 选项 字体 布局和颜色 选项选项 设置光标 历史记录 编辑模式的切换 字体字体 设置字体的名称和大小 布局布局 设置窗口的缓冲区 窗口的大小 窗口起始坐标 精品文档 22欢迎下载 颜色颜色 设置屏幕和对话框的背景色和前景色 Powershell Foreach 循环 Powershell 程序设计 添加评论 一 232012 Foreach object 为 cmdlet 命令 使用在管道中 对管道结果逐个处理 foreach 为遍历 集合的关键字 下面举两个例子 帮助 01 02 03 04 05 06 07 08 09 10 11 12 array 7 10 foreach n in array n n 49 64 81 100 foreach file in dir c windows 精品文档 23欢迎下载 13 14 15 16 17 18 19 20 21 if file Length gt 1mb File Name explorer exe WindowsUpdate log 这里只为了演示 foreach 其实上面的第二个例子可以用 Foreach Object 更简洁 PS C Powershell dir C Windows where length gt 1mb foreach object Name explorer exe WindowsUpdate log Powershell 条件操作符 Powershell 程序设计 添加评论 一 162012 Powershell 中的比较运算符 eq eq 等于 ne ne 不等于 gt gt 大于 ge ge 大于等于 lt lt 小于 le le 小于等于 contains contains 包含 notcontains notcontains 不包含 精品文档 24欢迎下载 进行比较进行比较 可以将比较表达式直接输入进 Powershell 控制台 然后回车 会自动比较并把比较结果返 回 PS C Powershell 3 4 5 contains 2 False PS C Powershell 3 4 5 contains 5 True PS C Powershell 3 4 5 notcontains 6 True PS C Powershell 2 eq 10 False PS C Powershell A eq a True PS C Powershell A ieq a True PS C Powershell A ceq a False PS C Powershell 1gb lt 1gb 1 True 精品文档 25欢迎下载 PS C Powershell 1gb lt 1gb 1 False 求反求反 求反运算符为 not 但是像高级语言一样 也支持求反 PS C Powershell a 2 eq 3 PS C Powershell a False PS C Powershell not a True PS C Powershell a True 布尔运算布尔运算 and and 和 or or 或 xor xor 异或 not not 逆 PS C Powershell true and true True 精品文档 26欢迎下载 PS C Powershell true and false False PS C Powershell true or true True PS C Powershell true or false True PS C Powershell true xor false True PS C Powershell true xor true False PS C Powershell not true False 比较数组和集合比较数组和集合 过滤数组中的元素过滤数组中的元素 PS C Powershell 1 2 3 4 3 2 1 eq 3 3 3 精品文档 27欢迎下载 PS C Powershell 1 2 3 4 3 2 1 ne 3 1 2 4 2 1 验证一个数组是否存在特定元素验证一个数组是否存在特定元素 PS C Powershell help man ls PS C Powershell 1 9 4 5 contains 9 True PS C Powershell 1 9 4 5 contains 10 False PS C Powershell 1 9 4 5 notcontains 10 True Powershell Where Object 条件过滤 Powershell 程序设计 添加评论 一 172012 精品文档 28欢迎下载 本篇会对条件判断进行实际应用 在管道中可以通过条件判断过滤管道结果 Where Object 会对集合逐个过滤 将符合条件的结果保留 过滤管道结果过滤管道结果 使用 Get Process 返回所有的当前进程 但是你可能并不对所有的进程感兴趣 然后通过 每个 Process 对象的属性进行过滤 首先得知道每个对象支持那些属性 PS C Powershell Get Process select First 1 fl NounName Process Name AcroRd32 Handles 287 VM 234819584 WS 32616448 PM 63488000 NPM 14584 Path C Program Files Adobe Reader 10 0 Reader AcroRd32 exe Company Adobe Systems Incorporated CPU 96 5334188 FileVersion 10 1 2 45 精品文档 29欢迎下载 ProductVersion 10 1 2 45 Description Adobe Reader Product Adobe Reader Id 4820 PriorityClass Normal HandleCount 287 WorkingSet 32616448 PagedMemorySize 63488000 PrivateMemorySize 63488000 VirtualMemorySize 234819584 TotalProcessorTime 00 01 36 5334188 BasePriority 8 ExitCode HasExited False ExitTime Handle 3568 MachineName MainWindowHandle 198686 MainWindowTitle Mastering PowerShell Adobe Reader 精品文档 30欢迎下载 MainModule System Diagnostics ProcessModule AcroRd32 exe MaxWorkingSet 1413120 MinWorkingSet 204800 Modules System Diagnostics ProcessModule AcroRd32 exe System Diagnostics ProcessModule ntdll dll Syst em Diagnostics ProcessModule kernel32 dll Syste m Diagnostics ProcessModule KERNELBASE dll NonpagedSystemMemorySize 14584 NonpagedSystemMemorySize64 14584 PagedMemorySize64 63488000 PagedSystemMemorySize 302460 PagedSystemMemorySize64 302460 PeakPagedMemorySize 75399168 PeakPagedMemorySize64 75399168 PeakWorkingSet 87871488 PeakWorkingSet64 87871488 精品文档 31欢迎下载 PeakVirtualMemorySize 257703936 PeakVirtualMemorySize64 257703936 PriorityBoostEnabled True PrivateMemorySize64 63488000 PrivilegedProcessorTime 00 00 27 7057776 ProcessName AcroRd32 ProcessorAffinity 3 Responding True SessionId 1 StartInfo System Diagnostics ProcessStartInfo StartTime 2012 1 13 10 25 34 SynchronizingObject Threads 4376 6636 8096 5136 UserProcessorTime 00 01 08 8276412 VirtualMemorySize64 234819584 EnableRaisingEvents False StandardInput StandardOutput StandardError 精品文档 32欢迎下载 WorkingSet64 32616448 Site Container 根据进程名过滤所有记事本进程 PS C Powershell Get Process Where Object Name eq notepad Handles NPM K PM K WS K VM M CPU s Id ProcessName 158 7 8800 37264 114 18 41 6204 notepad 根据进程名过滤所有 IE 进程 PS C Powershell Get Process Where Object Name eq iexplore Handles NPM K PM K WS K VM M CPU s Id ProcessName 710 23 12832 18160 175 10 51 4204 iexplore 971 39 81000 107580 399 22 20 6764 iexplore 336 13 28516 20096 187 0 34 6792 iexplore 精品文档 33欢迎下载 929 35 51020 46568 314 10 42 7192 iexplore 835 26 49200 32360 308 7 82 7952 iexplore 根据 company 过滤所有产品发布者以 Microsoft 打头的进程 PS C Powershell Get Process Where Object company like Microsoft select Name Description Company msseces Microsoft Security Clie Microsoft Corporation notepad 记事本 Microsoft Corporation ONENOTEM Microsoft OneNote Quick Microsoft Corporation OUTLOOK Microsoft Outlook Microsoft Corporation powershell Windows PowerShell Microsoft Corporation prevhost Preview Handler Surroga Microsoft Corporation RDCMan RDCMan Microsoft Corporation SearchProtocolHost Microsoft Windows Searc Microsoft Corporation 精品文档 34欢迎下载 taskhost Windows 任务的主机进程 Microsoft Corporation 使用别名使用别名 因为 Where Object 的使用概率比较高 所以有一个很形象的别名 可以使用 PS C Powershell Get Service Name like B Status Name DisplayName Running BDESVC BitLocker Drive Encryption Service Running BFE Base Filtering Engine Running BITS Background Intelligent Transfer Ser Stopped Browser Computer Browser Stopped bthserv Bluetooth Support Service Powershell ForEach Object 循环 Powershell 程序设计 添加评论 一 222012 Powershell 管道就像流水线 对于数据的处理是一个环节接着一个环节 如果你想在某一 环节对流进来的数据逐个细致化的处理 可是使用 ForEach Object 代表当前的数据 精品文档 35欢迎下载 对管道对象逐个处理对管道对象逐个处理 如果使用 Get WmiObject 获取系统中的服务 为了排版可能会也会使用 Format Table 对 结果进行表格排版 PS C Powershell Get WmiObject Win32 Service Format Table status DisplayName AutoSize status DisplayName OK Adobe Acrobat Update Service OK Application Experience OK Application Layer Gateway Service OK Application Host Helper Service OK Application Identity OK Application Information OK Application Management OK ASP NET State Service 但是如果想对每个服务进行更定制化的处理可是使用 ForEach Object 精品文档 36欢迎下载 PS C Powershell Get WmiObject Win32 Service ForEach Object Name Disp layName Is ProcessId more than 100 ProcessId gt 100 Name Adobe Acrobat Update Service Is ProcessId more than 100 True Name Application Experience Is ProcessId more than 100 False Name Application Layer Gateway Service Is ProcessId more than 100 False Name Application Host Helper Service Is ProcessId more than 100 True Name Application Identity Is ProcessId more than 100 True Name Application Information Is ProcessId more than 100 True Name Application Management Is ProcessId more than 100 False Name ASP NET State Service Is ProcessId more than 100 False 结合条件处理结合条件处理 ForEach Object 的处理可以包含任意 Powershell 脚本 当然也包括条件语句 帮助 1 2 3 4 Get WmiObject Win32 Service ForEach Object if ProcessId gt 3000 0 1 f DisplayName ProcessID Windows Presentation Foundation Font Cache 3 0 0 0 5408 精品文档 37欢迎下载 Microsoft Network Inspection 5260 BranchCache 4112 Windows Modules Installer 7656 调用方法调用方法 在 ForEach Object 中 代表当前对象 当然也允许通过 调用该对象支持的方法 下面的例子杀死所有 IE

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论