powershell-基础语法和常用函数说明

前言

日常开发过程中,经常需要做一些命令式的步骤,比如上传打包,上传服务器,运行程序等。这些步骤本身不难,如果重复做的话,容易出错。所以最好封装成命令文件,一键执行即可。命令行windows有cmd,linux有bash等,难道就没有一个跨平台的命令行吗?答案是有的,这就是powershell

powershell本来是windows下的脚本,2016年之后支持跨平台。

跨平台

打开powershell的安装文档,会发现powershell支持的平台蛮多:
在这里插入图片描述

当前版本

7.0.3

查看帮助

get-help *

或者(一点点看)

get-help * | more

语法

定义变量

变量以$开头

$name='abcdefgh'

定义函数

function Test-MrParameter {

    param (
        $ComputerName
    )

    Write-Output $ComputerName
	Write-Output ($ComputerName+$ComputerName)
	Write-Output ($ComputerName+$ComputerName+$ComputerName)
}

参数类型

使用[type]来定义参数的类型,放在参数的前面

function Test-MrParameter {

    param (
        [string]$ComputerName
    )

    Write-Output $ComputerName
	Write-Output ($ComputerName+$ComputerName)
	Write-Output ($ComputerName+$ComputerName+$ComputerName)
}

参数默认值

=来定义默认值。

function Test-MrParameter {

    param (
        [string]$ComputerName = 'eureka'
    )

    Write-Output $ComputerName
	Write-Output ($ComputerName+$ComputerName)
	Write-Output ($ComputerName+$ComputerName+$ComputerName)
}

查看和使用函数

Get-Command -Name Test-MrParameter -Syntax
Test-MrParameter -ComputerName 'this is a computer name'
pause

输出

Test-MrParameter [[-ComputerName] <Object>]

this is a computer name
按 Enter 键继续...:

定义数组

@(开头)结尾,中间,分隔(测试不用加@也可以)

$arr=@(1,2,3)

定义字典

@{开头}结尾,每个键占一行。

$map=@{a=1
b=2}

for循环

for($i=0;$i -le 100;$i++){Write-Host $i}

foreach循环

foreach($p in Get-Process){Write-Host $p.Name}

进程

Get-Process 查看现有进程

Get-Process

Start-Process 开启新的进程

Start-Process -FilePath 'docker' -ArgumentList 'stack','ls' -WorkingDirectory .
Start-Process -FilePath "notepad" -WindowStyle Maximized

Wait-Process 等待进程结束

Wait-Process -Id (Get-Process notepad | Select-Object -ExpandProperty Id)

Stop-Process 关闭进程

Stop-Process -Id (Get-Process notepad | Select-Object -ExpandProperty Id)

Debug-Process 调试进程

Debug-Process -Id (Get-Process notepad | Select-Object -ExpandProperty Id)

当前会话的后台任务

会话关闭之后,后台任务也关闭

Get-Job 查看任务

Get-Job

Start-Job 开始任务(类似nohup…&)

Start-Job -ScriptBlock { Start-Sleep -Seconds 60 }

Wait-Job 等待任务

Wait-Job -Id (Get-Job | Select-Object -First 1 -ExpandProperty Id)

Receive-Job 接收任务消息

Receive-Job -Id (Get-Job | Select-Object -Last 1 -ExpandProperty Id) -Wait

Stop-Job 停止任务

Stop-Job -Id (Get-Job | Select-Object -First 1 -ExpandProperty Id)

停止之后,任务的状态发生改变,还是在任务列表里

Remove-Job 移除任务

Remove-Job -Id (Get-Job | Select-Object -ExpandProperty Id)

当前会话的后台线程任务

Start-ThreadJob 开启线程任务

线程任务是后台任务的一种,任务在会话的线程里面运行。可以指定执行任务的线程数,超过最大任务数后,任务排队执行,类似任务队列。

Start-ThreadJob -ScriptBlock { 1..100 | % { Start-Sleep 1; "Output $_" } } -ThrottleLimit 2

控制台

Get-Host 控制台信息

Get-Host

Read-Host 读取控制台

$name=Read-Host 'enter your name';Write-Host $name

Write-Host 自定义输出到控制台

Write-Host 'hello world'

Out-Host 输出到控制台

支持分页

Get-Process | Out-Host -Paging

Clear-Host 清空控制台

Clear-Host

字符串

Select-String 字符串选择

选择字符串cmdlet在输入字符串和文件中搜索文本和文本模式。您可以使用与UNIX中的grep或Windows中的findstr.exe类似的选择字符串。

'Hello', 'HELLO', 'HELLO' | Select-String -Pattern 'HELLO' -CaseSensitive -SimpleMatch -AllMatches

Join-String 将管道中的对象组合为单个字符串

Get-Process | Join-String -Property Name -Separator '|' -OutputPrefix '<' -OutputSuffix '>'

Out-String 将对象作为一系列字符串发送到主机

Get-Process | Out-String

ConvertTo-SecureString 转换加密的标准字符串以保护字符串

$secure=ConvertTo-SecureString -String 'hello world' -AsPlainText

ConvertFrom-SecureString 将安全字符串转换为加密的标准字符串

ConvertFrom-SecureString -SecureString $secure -AsPlainText

ConvertFrom-StringData 将包含一个或多个键和值对的字符串转换为散列表

$Here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable does not exist.
'@
ConvertFrom-StringData -StringData $Here

Name                           Value
----                           -----
Msg3                           The specified variable does not exist.
Msg2                           Credentials are required for this command.
Msg1                           The string parameter is required.

网络

Invoke-WebRequest 从因特网上的网页获取内容

Invoke-WebRequest cmdlet向web页面或web服务发送HTTP和HTTPS请求。它解析响应并返回链接、图像和其他重要HTML元素的集合。

下载文件

Invoke-WebRequest -Uri https://github.com/PowerShell/PowerShell/releases/download/v7.0.3/PowerShell-7.0.3-win-x64.msi -OutFile ./PowerShell-7.0.3-win-x64.msi

Invoke-RestMethod 向RESTful web服务发送HTTP或HTTPS请求

Invoke-RestMethod cmdlet向返回丰富结构数据的具像状态传输(Representational State Transfer, REST) web服务发送HTTP和HTTPS请求。

工作目录

Get-Location 获取当前工作目录

Get-Location

Set-Location 设置当前工作目录

Set-Location -Path /root

Push-Location 将当前位置添加到位置堆栈的顶部

Push-Location -Path 'D:' -StackName 'a'
Push-Location -Path 'D:\Program Files\' -StackName 'a'
Push-Location -Path 'D:\Program Files\Java\' -StackName 'a'

Pop-Location 将当前位置更改为最近压入堆栈的位置

Pop-Location -StackName 'a'
Pop-Location -StackName 'a'
Pop-Location -StackName 'a'

路径

Test-Path 确定路径的所有元素是否存在

Test-Path -Path /a/b

Resolve-Path 解析路径中的通配符,并显示路径内容

Resolve-Path /root/javacv.pdf -Relative

Convert-Path 将路径从PowerShell路径转换为PowerShell提供程序路径

Convert-Path -Path ~

Join-Path 将路径和子路径组合为单个路径

Join-Path -Path /root -ChildPath /lll

Split-Path 返回路径的指定部分

父目录

Split-Path -Path 'C:\Program Files\Java\jre1.8.0_221\bin\java.exe' -Parent

文件名

Split-Path -Path 'C:\Program Files\Java\jre1.8.0_221\bin\java.exe' -Leaf

文件后缀名

Split-Path -Path 'C:\Program Files\Java\jre1.8.0_221\bin\java.exe' -Extension

IO

文件操作

Get-Item 获取位于指定位置的项

Get-Item -Path .\编解码.png

Get-ChildItem 获取一个或多个指定位置中的项和子项

Get-ChildItem -Path . -Filter .git -Recurse -Force

Remove-Item 删除指定的项

Remove-Item -Path . -Filter .git -Recurse -Force 

Move-Item 将项从一个位置移动到另一个位置

Move-Item -Path .\编解码.png -Destination 'C:\Users\zhang\Desktop\'

Copy-Item 将项目从一个位置复制到另一个位置

Copy-Item -Path .\codec.png -Destination 'C:\Users\zhang\Desktop\'

Rename-Item 重命名PowerShell提供程序名称空间中的项

Rename-Item -Path .\编解码.png -NewName codec.png

剪切板

Get-Clipboard 获取剪切板数据

Get-Clipboard

Set-Clipboard 设置剪切板数据

Set-Clipboard -Value "hello world"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值