ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

PowerShell教程 - Cmdlet

2022-08-19 08:01:14  阅读:215  来源: 互联网

标签:教程 WhatIf Get 别名 命令 参数 Cmdlet PowerShell


更新记录
转载请注明出处:https://www.cnblogs.com/cqpanda/p/16589932.html
2022年8月19日 发布。
2022年8月15日 从笔记迁移到博客。

Cmdlet概念

Cmdlet说明

Windows PowerShell 引入了 cmdlet(读作“command-let”)的概念

它是内置于 Shell 的简单的单一函数命令行工具

可以分别使用每个 cmdlet,但只有组合使用这些简单的工具来执行复杂的任务时

Windows PowerShell 包括一百多个基本核心 cmdlet

可以编写自己的 cmdlet 并与其他用户共享

PowerShell 使用“动词-名词(verb-noun)”命名方式来命名各种命令,非常的好记

比如:

Get-Help

Cmdlet 与Command对比(Cmdlet vs Command)

Cmdlets are .NET Framework class objects; and not just stand-alone executables.

Cmdlets can be easily constructed from as few as a dozen lines of code.

Parsing, error presentation, and output formatting are not handled by cmdlets. It is done by the Windows PowerShell runtime.

Cmdlets process works on objects not on text stream and objects can be passed as output for pipelining.

Cmdlets are record-based as they process a single object at a time.

命令参数(Comlet Parameter)

格式:
image

第一个参数名称为-LogName,并赋值为Security

由于参数值中并不包含任何空格或标点符号,因此并不需要用引号括起来

第二个参数名称为-ComputerName

以逗号分隔列表的形式赋了两个值:Win8和Server1

由于这两个参数中都不包含空格或标点符号

因此这两个参数都不需要用引号括起来

最后一个参数是-Verbose,是一个开关参数

这意味着该参数无须赋值,仅仅指定参数即可

注意:

在命令名称和第一个参数之间必须有空格

参数名称总是以英文短横线(-)开头

参数名称之间必须有空格,多个参数值之间也必须有空格

PowerShell不区分大小写

实例:

Get-Help -Online 
Get-ChildItem -Path D:/

帮助参数:

在任何 cmdlet 上指定 -? 参数时

PowerShell 将显示该 cmdlet 的帮助

通用参数(Common parameters)

PowerShell 有几个通用参数

这些参数由 PowerShell 引擎控制

通用参数的行为方式始终相同

常用通用参数有:

-WhatIf、-Confirm、-Verbose、-Debug、-Warn、-ErrorAction
-ErrorVariable、-OutVariable、-OutBuffer

-Confirm

提示是否进行操作的确认:

New-Item 'text.txt' -Confirm 

禁用提示

Remove-Item .\IMadeThisUp.txt -Confirm:$false

注意:如果Confirm和WhatIf都与命令一起使用,则WhatIf优先

New-Item 'testForJi.txt' -Confirm -WhatIf 

确认提示的默认设置
如果未设置Confirm参数,则是否显示提示取决于$ConfirmPreference变量的值
默认情况下,提示等级为高
PS C:\Users\Administrator> $ConfirmPreference
High
$ConfirmPreference has four possible values:
High: Prompts when command impact is High (default)
Medium: Prompts when command impact is Medium or High
Low: Prompts when command impact is Low, Medium, or High
None: Never prompts

可以手动设置提示等级

$ConfirmPreference = 'Low'

-WhatIf

显示命令到底做了什么

New-Item 'text3.txt' -WhatIf 

注意:如果Confirm和WhatIf都与命令一起使用,则WhatIf优先

New-Item 'testForJi.txt' -Confirm -WhatIf

也可以设置为关闭

New-Item 'testForJi2.txt' -Confirm -WhatIf:$false

Whatif的默认设置
如果未设置-WhatIf参数,则是否显示提示取决于$WhatIfPreference变量的值

$WhatIfPreference = $true

-Verbose

显示详细的命令操作过程信息

New-Item 'text14.txt' -Verbose

查看所有的通用参数的帮助描述

Get-Help about_CommonParameters

可选位置参数(Optional positional parameters)

使用[]表示的参数是可选的

参数是按照顺序进行排列的,除了可以手动设置参数的key-value

还可以直接安装参数的顺序进行赋值

比如下面这个命令,可以直接使用位置参数:

Get-Process [[-Name] ] ...

可以使用以下方式进行使用:

Get-Process -Name powershell
Get-Process powershell

必须参数(Mandatory parameters)

没有[]包围的参数是必须参数

比如下面这个命令,就必须使用-Filter参数,否则会提示错误

Get-ADUser -Filter ...

使用方法:

Get-ADUser -Filter 'sAMAccountName -eq "SomeName"'

提示:可选的位置参数 可以和 必须参数一起混合使用

开关参数(Switch parameters)

部分参数是不需要值的,可以直接使用参数即可,当然你也可以值,但没有必要

比如下面这个命令

Get-ChildItem ... [-Recurse] ...

开启开关参数

Get-ChildItem -Recurse

强制指定值(不推荐)

Get-ChildItem -Recurse:$false

参数集(Parameter Sets)

命令可以有多个参数集

Stop-Process [-Id] <System.Int32[]> [-Force] [-PassThru] [-Confirm] [-WhatIf]
[<CommonParameters>]

Stop-Process [-InputObject] <System.Diagnostics.Process[]> [-Force] [-PassThr
u] [-Confirm] [-WhatIf] [<CommonParameters>]

Stop-Process [-Force] -Name <System.String[]> [-PassThru] [-Confirm] [-WhatIf] [<CommonParameters>]

获得全部参数

查询PowerShell文档

每个命令对应着一个.NET底层的数据类型,可以查看.PowerShell文档获得全部参数

使用.Parameters参数

(Get-Command StopProcess).Parameters

查看Cmdlet支持的动作词语

powershell
Get-Verb

注意:Each verb has a group, such as data, life cycle, or security

## 以GUI形式设置命令
```powershell
Show-Command

注意:不支持在没有GUI的系统上使用

实例:

Show-Command Get-Alias

运行外部命令
默认情况下,PowerShell可以自动处理外部命令
如果存在兼容问题
可以在命令后添加 -- 双破折号即可
命令 --

命令别名(Aliases)

PowerShell 支持别名以通过备用名称引用命令
别名允许具有其他 Shell 经验的用户使用其已知的常见命令名称在 PowerShell 中执行类似操作,比如:bash使用经验就可以使用别名

注意:别名并非相同命令,可能存在细节不同,最好还是少用别名
注意:别名只在当前Session有效

PowerShell下可以使用的Unix/Linux命令别名为:

cat、dir、mount、rm、cd、echo、move、rmdir、chdir、erase、popd、sleep、clear、h、ps、sort、cls、history、pushd、tee、copy、kill、pwd、type、del、lp、r、write、diff、ls、ren

使用Get-Alias可以获得这些别名对应的PowerShell真实命令
实例:

获得支持的所有别名

Get-Alias

获得指定命令别名

Get-Alias 命令

导出别名列表

Export-Alias

获得命令的别名

Get-Alias -Definition "Get-Service"

查看别名的帮助信息

Get-Help about_Aliases

PowerShell别名缩写规则:(不建议使用、影响清晰度)

名词或谓词 缩写
Get g
Set s
Item i
Location l
Command cm
Alias al

实例:

Cmdlet 名称 Alias
Get-Item gi
Set-Item si
Get-Location gl
Set-Location sl
Get-Command gcm
Get-Alias gal

创建新别名使用Set-Alias命令

Set-Alias

实例:

Set-Alias -Name gi -Value Get-Item
Set-Alias -Name np -Value 'notepad'

常用别名:

% for ForEach-Object
? for Where-Object
cd for Set-Location
gc or cat for Get-Content
ls or dir for Get-ChildItem
man for help (and then Get-Help)

命令跨行

使用反点号`即可跨行

实例:

Get-Service * | Sort-Object ServiceType `
| Format-Table Name, ServiceType, Status -AutoSize

PowerShell脚本文件扩展名

.ps1

获得命令历史

Get-History

清除历史命令

Clear-History

Splatting

Splatting说明

Splatting is a way of defining the parameters of a command before calling the command

作用:
避免参数过多造成书写麻烦,提高可维护性维护
避免重复书写参数

使用方法

Individual parameters are written in a hashtable (@{}), and then the @ symbol is used to tell PowerShell that the content of the hashtable should be read as parameters

实例:

简单实用

$getProcess = @{ Name = 'explorer' } 
Get-Process @getProcess

将命令作为字符串形式调用

$getProcess = @{
 Name = 'explorer'
}
& 'Get-Process' @getProcess

使用位置参数(Splatting and positional parameters)

除了可以使用命名的参数,还可以使用位置参数:

Rename-Item oldname.txt newname.txt
$renameItem = 'oldname.txt', 'newname.txt'
Rename-Item @renameItem

标签:教程,WhatIf,Get,别名,命令,参数,Cmdlet,PowerShell
来源: https://www.cnblogs.com/cqpanda/p/16589932.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有