常用
详情
- 正则提取字符串 123
([regex]'\{\{[\w|\W]+\}\}').Match("aaa123qwe{}").Value
- 判断变量a是否为空
[String]::IsNullOrEmpty($a)
- 变量url编码
[uri]::EscapeDataString($a)
- 生成GUID
[System.Guid]::NewGuid().toString()
- 等待输入
$str = [System.Console]::ReadLine()
- 关机
Stop-Computer
- 强制重启
Restart-Computer -Force
- 注册表添加启动项地址
计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
- 开机自动执行脚本目录
C:\Users\hsianglee\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
- 修改远程连接端口号
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -name "PortNumber" -Value 6666
- 设置服务自启
Set-Service sshd -StartupType Automatic
- cmd中以管理员权限运行Windows Terminal
mshta vbscript:createobject("shell.application").shellexecute("C:\Users\user\AppData\Local\Microsoft\WindowsApps\wt.exe","","","runas",1)(close)
- 睡眠
start-sleep -seconds 1
- 测试端口
(new-object Net.Sockets.TcpClient).Connect("127.0.0.1",4000)
- 查看端口信息
netstat -ano | findstr 8080
- 默认浏览器打开网址
Start-Process "http://localhost:3000"
- 设置脚本可执行
Set-ExecutionPolicy -executionpolicy RemoteSigned
- 设置程序后台执行
Start-Process npm 'run dev' -WindowStyle Hidden
版本
ps1
$PSVersionTable # 查看版本
iex "& { $(irm <https://aka.ms/install-powershell.ps1>) } -UseMSI" # 更新版本
Install-Module PSReadLine -Repository PSGallery -Force # 安装模块
Invoke-WebRequest 发送请求
ps1
Invoke-WebRequest -Uri url -Method Post -ContentType 'application/json' -body '{}' # 发送请求
Invoke-WebRequest -Uri url -OutFile 1.jpg # 保存图片
字符串
ps1
"" # 多行文本
"$a $b" # 字符串拼接变量
$a.$b # 动态属性
数组
数组方法:
ps1@(1,2,3) -join '-' # 转字符串 @("aa-qq", "bb-qq") -replace "qq","ww" # 字符替换 @('red','green','blue') -contains 'red' # 是否包含某一项 'red' -in @('red','green','blue') # 是否包含某一项 @('a-q','b-q','c-c') -match 'q' # 查找包含字符串的项 @(1,2) + @(3,4,5) # 数组合并
ForEach-Object 遍历($PSItem为遍历的当前对象,或者$_)
ps1@(1,2,3) | ForEach-Object {$PSItem * 3} @(1,2,3).ForEach({$PSItem * 3}) @("a", "b", "c") | ForEach-Object {$PSItem + ": " + $PSItem} ForEach-Object {Get-Service | Where-Object name -Like m*} | Sort-Object -Property status -Descending
排序并去重
$arr | Sort-Object {$_.PID} -Unique
// 排序并去重`去重
1,2,3,2 | select -Unique
定义数组对象
bash
$data = @(
[pscustomobject]@{a="a1";b="b1";c="c1";}
[pscustomobject]@{a="a2";b="b2";c="c2";}
)
$data[0] # 取某行
$data |foreach-object {$_.c} # 取某列
$data | Where-Object {$_.c -eq 'c2'} # 查询某一行,且当前行的c为c2
$data | where-object c -eq 'c2' # 查询某一行,且当前行的c为c2
$data.Where({$_.c -eq 'c2'}) # 查询某一行,且当前行的c为c2
- 数组统计
ps1
1..10 | Measure-Object -Sum # 求和
get-process | Measure-Object -Property PM -Sum # 根据数组属性计算
1..10 | Measure-Object -AllStats # 计算所有值
- 遍历添加数组
ps1
$arr=@()
16..20 | ForEach-Object {
$arr += @(
[PSCustomObject]@{
Name = 'aaa'
Age = $_
male = 'man'
}
)
}
$arr
文件管理
Get-Item
ps1
Get-Item . # 获取当前目录
Get-Item * # 获取当前目录的所有项
Get-Item * -Exclude 'L*' # 获取当前目录的所有项,排除某些项
Get-Item * -Include 'L*' # 获取当前目录中以L开头的文件
Get-Item * -Force # 获取当前目录包含隐藏项
New-Item
ps1
New-Item -Path . -Name 'a.txt' -ItemType 'file' -Value 'aaaaa' # 当前目录创建名称为a.txt的文件,文件内容为aaaaa
New-Item -Path . -ItemType 'directory' -Name 'aa' # 创建文件夹
New-Item -Path . -ItemType 'file' -Name 'a.txt' -Force # 创建文件,如果已存在则覆盖原文件
New-Item -Path . -ItemType 'file' -Name 'a.txt' -Force -WhatIf # 显示该命令会发生什么
1..5 | ForEach-Object {New-Item . -Name ($PSItem.ToString()+".txt")} # 一次性创建多个文件
New-Item -ItemType SymbolicLink -Path ./ -Name PSCustomTools -Target C:\drive\OneDrive\tools\PSCustomTools # 创建软连接
Remove-Item
ps1
Remove-Item *-Include '*.txt' # 删除当前目录下所有的txt文件
Remove-Item *-Include '*.txt' -Force # 删除当前目录下所有的txt文件,包括隐藏文件
Get-ChildItem *-Include "*.txt" -Recurse | Remove-Item # 递归删除文件,包含子文件夹
Copy-Item
ps1
Copy-Item .\1.txt -Destination .\a\ # 复制到子目录
Move-Item
ps1
Move-Item .\1.txt -Destination .\6.txt # 修改文件名
Move-Item .\1.txt -Destination .\a\ # 移动文件
Invoke-Item
ps1
Invoke-Item .\2.txt # 使用默认工具打开文件
Rename-Item
ps1
Rename-Item -Path .\2.txt -NewName .\7.txt # 修改文件名
Get-ChildItem "E:\aaa\*.txt" | Rename-Item -NewName {$_.Name -replace '.txt', '.png'} # 重命名多个文件
Get-ChildItem
ps1
Get-ChildItem e:\ # 获取子项
Get-ChildItem e:\ -Name # 获取子项的文件名
Get-ChildItem -Path E:\aaa\*.txt -Recurse -Force # 获取子项,递归,包含隐藏文件
Get-ChildItem -Path C:\Parent -Depth 2 # 获取子项,深度2层
Get-ChildItem -Path C:\Parent -Depth 2 # 获取子项,深度2层
Get-ChildItem -Recurse -Include "a.html" # 查找当前目录下的所有文件
Get-ChildItem -Recurse -Include "*.js" | %{Select-String -Path $_.FullName -Pattern "aaa|bbb"} # 文件夹下所有文件内容查找
Get-Content
ps1
Get-Content .\1.txt # 获取文本内容
Get-Content .\1.txt -TotalCount 5 # 获取前5行
[Get-Content .\1.txt](3) # 获取指定行
Get-Content .\1.txt -Tail 5 # 倒数5行
Add-Content
ps1
Add-Content .\1.txt -Value 'aaa' # 添加一行
Clear-Content
ps1
Clear-Content .\1.txt # 清空文件内容
Set-Content
ps1
Set-Content .\1.txt -Value 'aaa' # 清空文件并添加新内容
操作系统
Get-Process
ps1
Get-Process | Where-Object {$_.WorkingSet -gt 20000000} # 获取进程的工作集大于指定大小(子节)的所有进程
Get-Process -Name TIM # 获取tim程序信息
Get-Process -Name TIM -FileVersionInfo # 获取tim程序版本信息(包含文件路径)
Get-Process | Where-Object {$_.mainWindowTitle} # 获取具有主窗口标题的所有进程(在任务栏显示的进程
Get-Process -Name m*
Get-Process -Name TIM,mysqld
Get-Process -Id 1
Get-Process | Where-Object -FilterScript {$_.Responding -eq $false} # 获取所有无响应的应用程序
Get-CimInstance -ClassName Win32_Process
Get-CimInstance -ClassName Win32_Process -Filter "ProcessId='1352'"
- 停止进程
Stop-Process
Get-Service
ps1
Get-Service -Name mysql # 获取服务名为mysql的服务
Get-Service -Name mysql | Select-Object -Property * # 获取服务名为mysql的服务,并显示所有属性
Get-Service -Name mysql | Select-Object -Property * | Format-Table # 获取服务名为mysql的服务,并显示所有属性,格式化表格的形式
Get-Service | Where-Object name -Like m*| Select-Object -Property* | Format-Table # 获取服属性名以m开头,,并显示所有属性,格式化表格的形式
ForEach-Object {Get-Service | Where-Object name -Like m*} | Sort-Object -Property status -Descending # 获取服务属性名以m开头,,并按属性status倒序
Get-CimInstance -ClassName Win32_Service
Get-CimInstance -ClassName Win32_Service -Filter "ProcessId='1352'"
停止服务
Stop-Service
启动服务
Start-Service
重启服务
Restart-Service
获取磁盘驱动器信息
Get-PSDrive
Markdown预览
Show-Markdown -Path .\readme.md -UseBrowser
获取剪贴板的文件信息
Get-Clipboard -Format FileDropList
格式化json字符串
ConvertTo-Json(ConvertFrom-Json(s)) -Depth 2
获取本机已安装软件
ps1
Get-CimInstance -ClassName win32_product
Get-CimInstance -ClassName win32_product | ? name -Like 'Xshell*' | select -Property* // 获取Xshell软件的安装信息
弹窗: (New-Object -ComObject WScript.Shell).popup("你好吗?",0,"提示",0 + 48)
- 参数: 内容,多久关闭(秒),标题,类型(0 + 16)
- 类型:
- 0: 确认按钮
- 1: 确认按钮,取消按钮
- 2: 中止,重试,忽略
- 3: 是,否,取消
- 4: 是,否
- 5: 重试,取消
- 6: 取消,重试,继续
- 图标:
- 16: 错误
- 32: 问号
- 48: 感叹号
- 64: info
- 80: 没有图标
ftp
ps1
ftp
open ftp.com 19321
lcd # 打开本地文件夹
mget # 复制到ftp服务器
put # 下载到本地
正则表达式常用
- 不包含test1或test2的字符串
^((?!test1|test2).)*$
Hyper-V虚拟机
Hyper-V虚拟机端口映射,实现通过外网IP端口访问虚拟机(端口映射)
ps1
netsh interface portproxy show v4tov4 # 查询端口映射情况
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=29322 connectaddress=192.168.137.49 connectport=22 # 添加
netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=29322
hyper-v静态ip配置
编辑文件
/etc/sysconfig/network-scripts/ifcfg-eth0
bash# 修改(dhcp:自动获取ip) BOOTPROTO=static ONBOOT=yes # 新增 IPADDR=192.168.137.49 GATEWAY=192.168.137.1 DNS1=192.168.137.1 NETMASK=255.255.255.0 BROADCAST=192.168.137.255
虚拟机网卡ipv4修改固定ip 192.168.137.1,子网掩码 255.255.255.0
无线网属性,共享,选择虚拟机网卡共享,虚拟机重启或网卡重启(nmcli connection reload)
设置外部网络之后,网速变慢
- 打开网络适配器
- 在已启用,桥接的,右键=》属性=》配置=》高级,首选频带,选择5g
- 在桥接的那个网络,vThernet(),右键=》属性=》配置=》高级,选择Large Send Offload Version(ipv4,ipv6),设置为disabled
常用脚本
查找某个文件夹下的文件内容,并排除某些情况,并且该文件是谁提交的
ps1
$searchPath = "D:\epaas\epaas-pedpl-ui-web\src\views" # 替换为实际的文件夹路径
# $searchPath = "D:\epaas\epaas-platform-ui-web\src\views" # 替换为实际的文件夹路径
$searchPattern = "EpFormSearch ref='formSearchRef'"
$excludePattern = "data.formSearchRef = formSearchRef.value"
$excludePattern2 = "ref-list="
Set-Location $searchPath
function gitPath {
param (
[string] $pathItem = ""
)
Write-Output "File: $($pathItem)"
$auth = git blame -L 2,2 --line-porcelain -- $pathItem | Select-String "^author "
Write-Host "Auth: $($auth)"
}
Get-ChildItem -Path $searchPath -Recurse -File | ForEach-Object {
$matches = Select-String -Path $_.FullName -Pattern $searchPattern
if ($matches) {
$excludeMatches = Select-String -Path $_.FullName -Pattern $excludePattern
if (!$excludeMatches) {
$excludeMatches2 = Select-String -Path $_.FullName -Pattern $excludePattern2
if (!$excludeMatches2) {
$excludeMatches3 = Select-String -Path $_.FullName -Pattern "日志内容"
if (!$excludeMatches3) {
$matches | ForEach-Object {
$pathItem = $_.Path
if($_.Filename -eq 'search.vue') {
$pathItem = $pathItem.Replace("_components\search.vue", "index.vue")
gitPath($pathItem)
}else{
gitPath($pathItem)
}
Write-Output "--------------------------"
}
}
}
}
}
}