PowerShell 类 grep工具
PowerShell 开源 跨平台
Linux中 grep过滤查询实在是太方便了
windows 没有grep 但 有替代的方案 命令 findstr
cat .\create.tf |findstr alicloud_slb
cat .\create.tf |where {$_ -match "alicloud_slb"}
Select-String "alicloud_slb" .\create.tf
PowerShell 更好的批处理脚本语言
Windows 7 操作系统 预装PowersShell 2
Windows 8 内置 PowerShell 3.0
Windows 8.1 对应着 PowerShell 4.0
PowerShell 已经 PowerShell 6.0
强大的开源支持 https://github.com/powershell/powershell
跨平台 意味着 编写好的代码 管理 Windows Macintosh 甚至是 Linux 操作系统
是 Windows 下的原始批处理文件(batch file)所不能比的
PowerShell短命令
cmdlet 是 PowerShell 命令体系的基础动词-名词
cmdlet 基本语法结构
cmdlet 是一个内置到 shell 中的简单的单一功能命令行工具
可以就像使用传统的命令和实用工具那样使用 cmdlet
首先在 Windows PowerShell 命令提示符下键入 cmdlet 的名称
Windows PowerShell 不区分大小写 $PSVersionTable
Name Value
CLRVersion 2.0.50727.5466
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
PowerShell内置帮助描述了$PSVersionTable 详细信息看命令 Get-Help about_Automatic_Variables
如果以前没有运行过 可能需要先运行 Update-Help
PowerShell 兼容 Linux 命令
PowerShell 将一些命令做了别名处理 以取悦那些 Linux 专家的习惯 常见的如 ls 命令 也可以用 dir gci 命令进行调用PS F:\> ls
目录: F:\
Mode LastWriteTime Length Name
d---- 2019-08-04 18:48 go_spider
PS F:\> tree 命令将结果以文件树的形式输出
F:.
└─go_spider
├─core
│ ├─common
│ │ ├─com_interfaces
│ │ ├─config
│ │ ├─etc_config
│ │ ├─mlog
│ │ ├─page
│ │ ├─page_items
│ │ ├─request
│ │ ├─resource_manage
│ │ └─util
│ ├─downloader
│ ├─page_processer
│ ├─pipeline
│ ├─scheduler
│ └─spider
├─example
│ ├─baidu_baike_page_processor
│ ├─github_repo_page_processor
│ ├─login_profile_page_processor
│ ├─redis_scheduler
│ ├─sina_stock_json_processor
│ ├─sohu_gossip_page_json_processor
│ └─weixin_sogou_cookie_processor
└─extension
├─downloader
├─other
├─pipeline
└─scheduler
PowerShell 帮助文档
类似 Linux 的 man 命令 PowerShell 有 get-help 命令PS F:\> get-help get-help
名称 Get-Help
摘要 显示有关 Windows PowerShell 命令和概念的信息
语法 Get-Help [-Full] [[-Name] <string>] [-Category <string[]>] [-Component <str
ing[]>] [-Functionality <string[]>] [-Online] [-Path <string>] [-Role <stri
ng[]>] [<CommonParameters>]
Get-Help [-Detailed] [[-Name] <string>] [-Category <string[]>] [-Component
<string[]>] [-Functionality <string[]>] [-Online] [-Path <string>] [-Role <
string[]>] [<CommonParameters>]
Get-Help [-Examples] [[-Name] <string>] [-Category <string[]>] [-Component
<string[]>] [-Functionality <string[]>] [-Online] [-Path <string>] [-Role <
string[]>] [<CommonParameters>]
Get-Help [-Parameter <string>] [[-Name] <string>] [-Category <string[]>] [-
Component <string[]>] [-Functionality <string[]>] [-Online] [-Path <string>
] [-Role <string[]>] [<CommonParameters>]
说明 Get-Help cmdlet 显示有关 Windows PowerShell 概念和命令(包括 cmdlet、提供程
序、函数和脚本)的信息。要获取所有 cmdlet 帮助主题标题的列表,请键入“get-help *”
PowerShell 列出进程或服务
使用 get-process 段命令列出本地机器上的所有进程 是 ps 命令的别名PS F:\> get-process
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
24 4 2392 1452 38 0.28 9628 cmd
26 5 2828 364 40 0.31 10628 cmd
31 5 1440 320 41 0.02 820 conhost
32 4 1100 236 22 0.02 1612 conhost
507 12 2228 700 79 1.09 544 csrss
使用 get-service 短命令列出本地机器上的所有服务 包括停止和正在运行的
PS F:\> get-service
Status Name DisplayName
------ ---- -----------
Stopped AcPrfMgrSvc AcPrfMgrSvc
Stopped AcSvc AcSvc
Stopped AeLookupSvc Application Experience
Stopped ALG Application Layer Gateway Service
Stopped AppIDSvc Application Identity
PowerShell 筛选结果
类似 Linux 的 grep 命令 PowerShell 有 findstr 命令PS F:\> get-service|findstr Running
Running AeLookupSvc Application Experience
Running AudioEndpointBu... Windows Audio Endpoint Builder
Running AudioSrv Windows Audio
类似 Linux 的 grep 命令 PowerShell 有 select-string 命令
PS F:\> netstat -an|select-string udp
UDP 0.0.0.0:500 *:*
UDP 0.0.0.0:4500 *:*
UDP 169.254.121.177:137 *:*
PowerShell 清屏
clear-host 短命令cls 命令
clear 命令
进行终端清屏
PowerShell 新建项目
PowerShell 一个特色命令 可以 创建文件 文件夹还可以创建制定类型的文件系统
在注册表中 New-Item 创建注册表项和条目
PS F:\> New-Item test.txt
PowerShell 脚本
PowerShell 脚本的钦定的文件后缀名为 .ps1s 后面跟的是数字 1
psname.ps1
操作主机安装了 PowerShell 就可以执行运行
如果直接 .\文件名 会报错 因为 PowerShell 默认禁止在终端运行脚本程序
有一条命令用来绕过执行该脚本
powershell.exe -ExecutionPolicy Bypass -File psname.ps1
CMD下亦可执行 命令前缀 powershell.exe 必不可少
如果嫌麻烦 可以永久的改变本机的 PowerShell 执行策略
Set-ExecutionPolicy Bypass
和上面的单独绕过不同 该操作必须具有管理员权限 否则会报错
powershell注册表操作
PowerShell 一个吸引人的特性是 允许你直接在终端里 像访问普通的文件系统那样访问 Windows 注册表直接访问的是键 相当于普通文件系统的目录 而它其下包含的值和普通意义上的文件有一些区别
所谓的值显示在属性栏 与其他属性一同组成一个完整的值
文件系统的文件对象具有三大属性
所谓读 写 执行权限
只要是和键的读取有关的 在 PowerShell 的注册表交互中都可以像普通文件一样的交互
读的属性 如 列出某键下的所有子键 这时候 就应该想到 ls 命令 当然更规范的写法应该是 get-childitem 短命令
短命令 作用 别名
Get-ChildItem 列出某键的所有子键 ls, dir
Get-ItemProperty 列出某键的所有属性 -
Set-Location 更改当前操作的键 cd
Set-ItemProperty 更改某键的某值 -
New-ItemProperty 给某键新建一个值 -
Clear-ItemProperty 删除某键的值的内容 -
Remove-ItemProperty 删除某键的值 -
New-Item 创建一个新键 md
Remove-Item 删除一个键 del
Test-Path 验证某键是否存在 -
PowerShell常用命令
查看PowerShell的版本PS F:\>$PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
PS F:\>$PSVersionTablePS F:\>get-hostName : ConsoleHost
Version : 2.0
InstanceId : 653f588f-7361-4b0a-bc8b-0cb3bd578371
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : zh-CN
CurrentUICulture : zh-CN
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
查看操作系统
PS F:\> wmic os get captionCaption
Microsoft Windows 7 Ultimate
查看架构
PS F:\> wmic os get osarchitectureOSArchitecture
64-bit
查找Service
format-table可以确保Name被完整显示PS F:\>get-service | format-table servicename,displayname -autosize
ServiceName DisplayName
----------- -----------
AcPrfMgrSvc AcPrfMgrSvc
AcSvc AcSvc
AeLookupSvc Application Experience
ALG Application Layer Gateway Service
AppIDSvc Application Identity
PS F:\> Get-Service | Where-Object {$_.displayName.Contains("File")} | Select name,DisplayName
Name DisplayName
---- -----------
CscService Offline Files
EFS Encrypting File System (EFS)
查看环境变量
PS F:\>[environment]::ExpandEnvironmentVariables("%HomeDrive%%HomePath%")PS F:\>[environment]::ExpandEnvironmentVariables("%Home%")
查看文件内容并进行筛选
PS F:\>get-content updatelist.txt | findstr "29849"http://support.microsoft.com/?kbid=2984972 WASYGSHA01-1050 Security Update KB2984972 WASYGSHA01-1050\adm-bchen 5/9/2017
查找字符串findstr
或者可以给此命令起一个别名grep来使用PS F:\> new-alias grep findstr
PS F:\> ls | grep -I -N exe
电脑关机和重启
PS F:\>Stop-ComputerPS F:\>Restart-Computer
时间相关的操作
PS F:\> Set-Date -Date (Get-Date).AddDays(3)在后台打开进程
PS F:\>Start-Process .\HeroesSwitcher_x64.exe -WindowStyle Hidden查看文件hash
PS F:\> Get-FileHash -Path .\flashplayer28_xa_install.exe | Format-ListPS F:\> Get-FileHash -Path .\flashplayer28_xa_install.exe -Algorithm SHA1 | Format-List
Algorithm : SHA1
Hash : F35F8093A98752B48F5D9FCDF5A57A6C23558174
Path : D:\ChuckLu\ProgramFiles\flashplayer28_xa_install.exe
获取电脑信息
查看computer namePS F:\> Get-ComputerInfo -Property "CsName" | Format-List
查看location
PS F:\>Push-Location
PS F:\>Pop-Location
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/pop-location?view=powershell-6
Changes the current location to the location most recently pushed onto the stack.
get the key of parameters of a specified command
((Get-Command -Name Start-Resgen).Parameters).Keys
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
New-Item
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item?view=powershell-6
在当前目录下创建一个文件
PS F:\>New-Item post-build.bat
Get the absolute path of current location
PS F:\>(Get-Item .).FullName
https://github.com/chucklu/Scripts/blob/master/Powershell/chuck.psm1
获取cpu信息
PS F:\> Get-WMIObject win32_ProcessorPS F:\> Get-WmiObject –class Win32_processor | ft systemname,Name,DeviceID,NumberOfCores,NumberOfLogicalProcessors, Addresswidth
获取显卡信息 PS F:\> Get-WmiObject Win32_VideoController
尊贵的董事大人
英文标题不为空时 视为本栏投稿
需要关键字 描述 英文标题