收藏 分享(赏)

PowerShell实例教程.doc

上传人:scg750829 文档编号:8202779 上传时间:2019-06-13 格式:DOC 页数:530 大小:1.99MB
下载 相关 举报
PowerShell实例教程.doc_第1页
第1页 / 共530页
PowerShell实例教程.doc_第2页
第2页 / 共530页
PowerShell实例教程.doc_第3页
第3页 / 共530页
PowerShell实例教程.doc_第4页
第4页 / 共530页
PowerShell实例教程.doc_第5页
第5页 / 共530页
点击查看更多>>
资源描述

1、PowerShell实例教程认识PowerShell介绍和安装Powershell 是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境。你可以把它看成是命令行提示符cmd.exe的扩充,不对,应当是颠覆。 powershell需要.NET环境的支持,同时支持.NET对象。微软之所以将Powershell 定位为Power,并不是夸大其词,因为它完全支持对象。其可读性,易用性,可以位居当前所有shell之首。 当前powershell有四版本,分别为1.0,2.0,3.0 ,4.0 如果您的系统是window7或者Windows Server 2008,那么PowerShe

2、ll 2.0已经内置了,可以升级为3.0,4.0。 如果您的系统是Windows 8 或者Windows server 2012,那么PowerShell 3.0已经内置了,可以升级为4.0。 如果您的系统为Windows 8.1或者Windows server 2012 R2,那默认已经是4.0了。自定义控制台右击标题栏选择”属性”弹出powershell控制台对话框。在这里有四个选项卡:选项、字体、布局和颜色。“选项”设置光标,历史记录,编辑模式的切换。控制台选项“字体”设置字体的名称和大小控制台字体“布局”设置窗口的缓冲区、窗口的大小、窗口起始坐标控制台布局“颜色”设置屏幕和对话框的背景

3、色和前景色。控制台颜色快速编辑模式和标准模式powershell控制台有两种模式,一个是快速编辑模式,一个是标准模式。快速编辑模式和标准模式的切换可以通过控制台标题栏-鼠标右击-属性-选项-编辑选项 。 Powershell标准模式鼠标右击选择标记后才能实现复制和粘切功能。 Powershell快速编辑模式可以通过鼠标左键选择任意矩形区域内的文本,并且鼠标右击实现复制功能。快捷键Powershell的快捷键和cmd,linux中的shell,都比较像。ALT+F7 清除命令的历史记录PgUp PgDn 显示当前会话的第一个命令和最后一个命令Enter 执行当前命令End 将光标移至当前命令的末

4、尾Del 从右开始删除输入的命令字符Esc 清空当前命令行F2自动补充历史命令至指定字符(例如历史记录中存在Get-Process,按F2,提示“Enter char to copy up to“,键入s,自动补齐命令:Get-Proce)F4 删除命令行至光标右边指定字符处F7 对话框显示命令行历史记录F8 检索包含指定字符的命令行历史记录F9 根据命令行的历史记录编号选择命令,历史记录编号可以通过F7查看左/右方向键左右移动光标上/下方向键切换命令行的历史记录Home 光标移至命令行最左端Backspace 从右删除命令行字符Ctrl+C 取消正在执行的命令Ctrl+左/右方向键在单词之间

5、移动光标Ctrl+Home 删除光标最左端的所有字符Tab 自动补齐命令或者文件名管道和重定向管道把上一条命令的输出作为下一条命令的输入。PowerShell管道例如通过ls获取当前目录的所有文件信息,然后通过Sort -Descending对文件信息按照Name降序排列,最后将排序好的文件的Name和Mode格式化成Table输出。PS C:PStest ls | sort -Descending Name | Format-Table Name,ModeName Mode- -d.txt -a-c.txt -a-b.txt -a-ABC d-a.txt -a-重定向把命令的输出保存到文件中

6、,为覆盖,追加。PS C:PStest “Powershell Routing“ test.txtPS C:PStest Get-Content .test.txtPowershell RoutingPS C:PStest “Powershell Routing“ test.txtPS C:PStest “Powershell Routing“ test.txtPS C:PStest “Powershell Routing“ test.txtPS C:PStest “Powershell Routing“ test.txtPS C:PStest “Powershell Routing“ tes

7、t.txtPS C:PStest Get-Content .test.txtPowershell RoutingPowershell RoutingPowershell RoutingPowershell RoutingPowershell RoutingPowershell RoutingPS C:PStestPowershell交互式数学运算我们可以把powershell当成一个计算器。象键入命令行那样输入数学表达式,回车,powershell会自动计算并把结果输出。常用的加减乘除模(+,-,*,/,%)运算和小括号表达式都支持。PS C:pstest 1+2+36PS C:pstest

8、0xABCD43981PS C:pstest 3.14*10*10314PS C:pstest 1+3-(2.4-5)*(7.899-4.444)12.983PowerShell也能自动识别计算机容量单位,包括KB,MB,GB,TB,PBPS C:pstest 1pb/1tb1024PS C:pstest 1tb/1gb1024PS C:pstest 1gb/1kb1048576PS C:pstest 1gb/20mb*10kb524288假如一个网站每个页面大小为80kb,统计显示每天的PV操作为800,1个月下来占用的带宽:PS C:pstest 80kb*800*30/1gb1.8310

9、546875假如一个网站的每天人均PV操作为5,页面大小为80Kb,主机提供商限制的总流量为10G,那平均每天的最大访客数为:PS C:pstest 10GB/(80KB*5)/30873.813333333333执行外部命令Powershell 能够像CMD一样很好的执行外部命令。通过netstat查看网络端口状态PS C:PS netstatActive ConnectionsProto Local Address Foreign Address StateTCP 192.168.0.100:3049 192.168.0.88:7575 ESTABLISHEDTCP 192.168.0.1

10、00:3052 192.168.0.88:7575 ESTABLISHEDTCP 192.168.0.100:3061 192.168.0.88:7575 ESTABLISHED通过IPConfig 查看自己的网 络配置PS C:PS ipconfigWindows IP ConfigurationEthernet adapter Local Area Connection:Connection-specific DNS Suffix . : Link-local IPv6 Address . . . . . : fe80:b9dd:91e33:33f0:7885%10IPv4 Address

11、. . . . . . . . . . . : 192.168.140.100Subnet Mask . . . . . . . . . . . : 255.255.252.0Default Gateway . . . . . . . . . : 192.168.140.1Tunnel adapter :Connection-specific DNS Suffix . : Link-local IPv6 Address . . . . . : fe80:5efe:192.168.140.100%11Default Gateway . . . . . . . . . :Tunnel adapte

12、r Teredo Tunneling Pseudo-Interface:Media State . . . . . . . . . . . : Media disconnectedConnection-specific DNS Suffix . :route print查看路由信息PS C:PS route printIPv4 Route Table=Active Routes:Network Destination Netmask Gateway Interface Metric0.0.0.0 0.0.0.0 192.168.140.1 192.168.140.100 20192.0.0.0

13、 255.0.0.0 On-link 192.0.0.1 306192.0.0.1 255.255.255.255 On-link 192.0.0.1 306192.255.255.255 255.255.255.255 On-link 192.0.0.1 306192.168.140.0 255.255.252.0 On-link 192.168.140.100 276192.168.140.100 255.255.255.255 On-link 192.168.140.100 276192.168.143.255 255.255.255.255 On-link 192.168.140.10

14、0 276224.0.0.0 240.0.0.0 On-link 192.0.0.1 306224.0.0.0 240.0.0.0 On-link 192.168.140.100 276255.255.255.255 255.255.255.255 On-link 192.0.0.1 306255.255.255.255 255.255.255.255 On-link 192.168.140.100 276=Persistent Routes:NoneIPv6 Route Table=Active Routes:If Metric Network Destination Gateway1 30

15、6 :1/128 On-link10 276 fe80:/64 On-link11 281 fe80:5efe:192.168.140.100/128On-link10 276 fe80:b965:91f3:33a0:7285/128On-link1 306 ff00:/8 On-link10 276 ff00:/8 On-link=Persistent Routes:None启动CMD控制台启动CMD控制台键入cmd或者cmd.exe,退出cmd可以通过命令exit。PS C:PS cmdMicrosoft Windows Version 6.1.7601Copyright (c) 2009

16、 Microsoft Corporation. All rights reserved.C:PSexitPS C:PS查找可用的Cmd控制台命令Cmd.exe 通过 /c 来接收命令参数,在Cmd中help可以查看可用的命令,所以可以通过Cmd /c help 查找可用的Cmd控制台命令PS C:PS cmd /c help有关某个命令的详细信息,请键入 HELP 命令名ASSOC 显示或修改文件扩展名关联。ATTRIB 显示或更改文件属性。BREAK 设置或清除扩展式 CTRL+C 检查。BCDEDIT 设置启动数据库中的属性以控制启动加载。CACLS 显示或修改文件的访问控制列表(ACL)

17、。CALL 从另一个批处理程序调用这一个。CD 显示当前目录的名称或将其更改。CHCP 显示或设置活动代码页数。CHDIR 显示当前目录的名称或将其更改。CHKDSK 检查磁盘并显示状态报告。CHKNTFS 显示或修改启动时间磁盘检查。CLS 清除屏幕。CMD 打开另一个 Windows 命令解释程序窗口。COLOR 设置默认控制台前景和背景颜色。COMP 比较两个或两套文件的内容。COMPACT 显示或更改 NTFS 分区上文件的压缩。CONVERT 将 FAT 卷转换成 NTFS。您不能转换当前驱动器。COPY 将至少一个文件复制到另一个位置。DATE 显示或设置日期。DEL 删除至少一个

18、文件。DIR 显示一个目录中的文件和子目录。DISKCOMP 比较两个软盘的内容。DISKCOPY 将一个软盘的内容复制到另一个软盘。DISKPART 显示或配置磁盘分区属性。DOSKEY 编辑命令行、调用 Windows 命令并创建宏。DRIVERQUERY 显示当前设备驱动程序状态和属性。ECHO 显示消息,或将命令回显打开或关上。ENDLOCAL 结束批文件中环境更改的本地化。ERASE 删除一个或多个文件。EXIT 退出 CMD.EXE 程序(命令解释程序)。FC 比较两个文件或两个文件集并显示它们之间的不同。FIND 在一个或多个文件中搜索一个文本字符串。FINDSTR 在多个文件中

19、搜索字符串。FOR 为一套文件中的每个文件运行一个指定的命令。FORMAT 格式化磁盘,以便跟 Windows 使用。FSUTIL 显示或配置文件系统的属性。FTYPE 显示或修改用在文件扩展名关联的文件类型。GOTO 将 Windows 命令解释程序指向批处理程序中某个带标签的行。GPRESULT 显示机器或用户的组策略信息。GRAFTABL 启用 Windows 在图形模式显示扩展字符集。HELP 提供 Windows 命令的帮助信息。ICACLS 显示、修改、备份或还原文件和目录的 ACL。IF 在批处理程序中执行有条件的处理过程。LABEL 创建、更改或删除磁盘的卷标。MD 创建一个目

20、录。MKDIR 创建一个目录。MKLINK 创建符号链接和硬链接MODE 配置系统设备。MORE 逐屏显示输出。MOVE 将一个或多个文件从一个目录移动到另一个目录。OPENFILES 显示远程用户为了文件共享而打开的文件。PATH 为可执行文件显示或设置搜索路径。PAUSE 停止批处理文件的处理并显示信息。POPD 还原由 PUSHD 保存的当前目录上一次的值。PRINT 打印一个文本文件。PROMPT 改变 Windows 命令提示。PUSHD 保存当前目录,然后对其进行更改。RD 删除目录。RECOVER 从损坏的磁盘中恢复可读取的信息。REM 记录批处理文件或 CONFIG.SYS 中

21、的注释。REN 重新命名文件。RENAME 重新命名文件。REPLACE 替换文件。RMDIR 删除目录。ROBOCOPY 复制文件和目录树的高级实用程序SET 显示、设置或删除 Windows 环境变量。SETLOCAL 开始用批文件改变环境的本地化。SC 显示或配置服务(后台处理)。SCHTASKS 安排命令和程序在一部计算机上按计划运行。SHIFT 调整批处理文件中可替换参数的位置。SHUTDOWN 让机器在本地或远程正确关闭。SORT 将输入排序。START 打开单独视窗运行指定程序或命令。SUBST 将驱动器号与路径关联。SYSTEMINFO 显示机器的具体的属性和配置。TASKLI

22、ST 显示包括服务的所有当前运行的任务。TASKKILL 终止正在运行的进程或应用程序。TIME 显示或设置系统时间。TITLE 设置 CMD.EXE 会话的窗口标题。TREE 以图形显示启动器或路径的目录结构。TYPE 显示文本文件的内容。VER 显示 Windows 的版本。VERIFY 告诉 Windows 验证文件是否正确写入磁盘。VOL 显示磁盘卷标和序列号。XCOPY 复制文件和目录树。WMIC 在交互命令外壳里显示 WMI 信息。启动外部程序为什么可以通过notpad打开记事本,不能通过wordpad打开写字板?因为notepad.exe位于C:Windowssystem32 这

23、个目录,而这个目录已经默认被包含在Powershell的环境变量$env:Path中。C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program Files (x86)Windows Kits8.1Windows Performance Toolkit;C:Program FilesMicrosoft SQL Server110ToolsBinn;C:Program Files (x86)Microsoft SDKsTypeScript1.0;C:Progr

24、am FilesMicrosoftWeb Platform Installer而wordpad.exe 所在的“%ProgramFiles%Windows NTAccessorieswordpad.exe“目录却没有包含,可以先进入这个目录,再运行wordpad,或者将wordpad所在的目录加入到环境变量中,$env:Path=$env:Path+”%ProgramFiles%Windows NTAccessories”。默认键入一个字符串,powershell会将它原样输出,如果该字符串是一个命令或者启动程序,在字符串前加DefaultParameterSetProperty System

25、.String DefaultParameterSet get;Definition Property System.String Definition get;HelpFile Property System.String HelpFile get;ImplementingTypeProperty System.Type ImplementingType get;Module Property System.Management.Automation.PSModuleInfo Module get;ModuleName Property System.String ModuleName ge

26、t;Name Propert System.String Name get;yNoun Property System.String Noun get;OutputType PropertySystem.Collections.ObjectModel.ReadOnlyCollection1System.Management.Automation.PSTypeName, System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 OutputType get;Par

27、ameters PropertySystem.Collections.Generic.Dictionary2System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,System.Management.Automation.ParameterMetadata, System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Parameters

28、get;ParameterSetsPropertySystem.Collections.ObjectModel.ReadOnlyCollection1System.Management.Automation.CommandParameterSetInfo, System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 ParameterSets get;PSSnapIn Property System.Management.Automation.PSSnapInIn

29、fo PSSnapIn get;Verb Property System.String Verb get;Visibility Property System.Management.Automation.SessionStateEntryVisibility Visibility get;set;DLL ScriptProperty System.Object DLL get=$this.ImplementingType.Assembly.Location;HelpUri ScriptPropertySystem.Object HelpUri get=try# ok to cast Comma

30、ndTypes enum to HelpCategory because string/indentifier for# cmdlet,function,filter,alias,externalscript is identical.# it is ok to fail for other enum values (i.e. for Application)$helpObject = get-help -Name ($this.Name) -Category (string($this.CommandType) -ErrorAction SilentlyContinue# return fi

31、rst non-null uri (and try not to hit any strict mode things)if ($helpObject -eq $null) return $null if ($helpObject.psobject.propertiesrelatedLinks -eq $null) return $null if ($helpObject.relatedLinks.psobject.propertiesnavigationLink -eq $null) return $null $helpUri = string$( $helpObject.relatedLi

32、nks.navigationLink | % if ($_.psobject.propertiesuri -ne $null) $_.uri | ? $_ | select -first 1 )return $helpUricatch ;下面是全部的Cmdlets命令每个命令有一个动词和名词组成,命令的作用一目了然。Name ModuleName HelpAdd-Computer Microsoft.PowerShell.Management helpAdd-Content Microsoft.PowerShell.Management helpAdd-History Microsoft.Po

33、werShell.Core helpAdd-Member Microsoft.PowerShell.Utility helpAdd-PSSnapin Microsoft.PowerShell.Core helpAdd-Type Microsoft.PowerShell.Utility helpCheckpoint-Computer Microsoft.PowerShell.Management helpClear-Content Microsoft.PowerShell.Management helpClear-EventLog Microsoft.PowerShell.Management

34、helpClear-History Microsoft.PowerShell.Core helpClear-Item Microsoft.PowerShell.Management helpClear-ItemProperty Microsoft.PowerShell.Management helpClear-Variable Microsoft.PowerShell.Utility helpCompare-Object Microsoft.PowerShell.Utility helpComplete-Transaction Microsoft.PowerShell.Management h

35、elpConnect-WSMan Microsoft.WSMan.Management helpConvertFrom-Csv Microsoft.PowerShell.Utility helpConvertFrom-SecureString Microsoft.PowerShell.Security helpConvertFrom-StringData Microsoft.PowerShell.Utility helpConvert-Path Microsoft.PowerShell.Management helpConvertTo-Csv Microsoft.PowerShell.Util

36、ity helpConvertTo-Html Microsoft.PowerShell.Utility helpConvertTo-SecureString Microsoft.PowerShell.Security helpConvertTo-Xml Microsoft.PowerShell.Utility helpCopy-Item Microsoft.PowerShell.Management helpCopy-ItemProperty Microsoft.PowerShell.Management helpDebug-Process Microsoft.PowerShell.Manag

37、ement helpDisable-ComputerRestore Microsoft.PowerShell.Management helpDisable-PSBreakpoint Microsoft.PowerShell.Utility helpDisable-PSSessionConfiguration Microsoft.PowerShell.Core helpDisable-WSManCredSSP Microsoft.WSMan.Management helpDisconnect-WSMan Microsoft.WSMan.Management helpEnable-Computer

38、Restore Microsoft.PowerShell.Management helpEnable-PSBreakpoint Microsoft.PowerShell.Utility helpEnable-PSRemoting Microsoft.PowerShell.Core helpEnable-PSSessionConfiguration Microsoft.PowerShell.Core helpEnable-WSManCredSSP Microsoft.WSMan.Management helpEnter-PSSession Microsoft.PowerShell.Core he

39、lpExit-PSSession Microsoft.PowerShell.Core helpExport-Alias Microsoft.PowerShell.Utility helpExport-Clixml Microsoft.PowerShell.Utility helpExport-Console Microsoft.PowerShell.Core helpExport-Counter Microsoft.PowerShell.Diagnostics helpExport-Csv Microsoft.PowerShell.Utility helpExport-FormatData M

40、icrosoft.PowerShell.Utility helpExport-ModuleMember Microsoft.PowerShell.Core helpExport-PSSession Microsoft.PowerShell.Utility helpForEach-Object Microsoft.PowerShell.Core helpFormat-Custom Microsoft.PowerShell.Utility helpFormat-List Microsoft.PowerShell.Utility helpFormat-Table Microsoft.PowerShe

41、ll.Utility helpFormat-Wide Microsoft.PowerShell.Utility helpGet-Acl Microsoft.PowerShell.Security helpGet-Alias Microsoft.PowerShell.Utility helpGet-AuthenticodeSignature Microsoft.PowerShell.Security helpGet-ChildItem Microsoft.PowerShell.Management helpGet-Command Microsoft.PowerShell.Core helpGet

42、-ComputerRestorePoint Microsoft.PowerShell.Management helpGet-Content Microsoft.PowerShell.Management helpGet-Counter Microsoft.PowerShell.Diagnostics helpGet-Credential Microsoft.PowerShell.Security helpGet-Culture Microsoft.PowerShell.Utility helpGet-Date Microsoft.PowerShell.Utility helpGet-Event

43、 Microsoft.PowerShell.Utility helpGet-EventLog Microsoft.PowerShell.Management helpGet-EventSubscriber Microsoft.PowerShell.Utility helpGet-ExecutionPolicy Microsoft.PowerShell.Security helpGet-FormatData Microsoft.PowerShell.Utility helpGet-Help Microsoft.PowerShell.Core helpGet-History Microsoft.P

44、owerShell.Core helpGet-Host Microsoft.PowerShell.Utility helpGet-HotFix Microsoft.PowerShell.Management helpGet-Item Microsoft.PowerShell.Management helpGet-ItemProperty Microsoft.PowerShell.Management helpGet-Job Microsoft.PowerShell.Core helpGet-Location Microsoft.PowerShell.Management helpGet-Mem

45、ber Microsoft.PowerShell.Utility helpGet-Module Microsoft.PowerShell.Core helpGet-PfxCertificate Microsoft.PowerShell.Security helpGet-Process Microsoft.PowerShell.Management helpGet-PSBreakpoint Microsoft.PowerShell.Utility helpGet-PSCallStack Microsoft.PowerShell.Utility helpGet-PSDrive Microsoft.PowerShell.Management helpGet-PSProvider Microsoft.PowerShell.Management helpGet-PSSession Microsoft.PowerShell.Core helpGet-PSSessionConfiguration Microsoft.PowerShell.Core helpGet-PSSnapin Microsoft.PowerShell.Core helpGet-Random Microsoft.P

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报