保存到OneDrive\Sysinternals\pws_profile.ps1
<# 首次使用准备,以管理员身份运行
Set-ExecutionPolicy RemoteSigned # 让powershell 可以不受限制地运行本地脚本,但从Internet下载的脚本,必须有签名才能运行。
if (!(Test-Path -Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force }
Rename-Item -Path $PROFILE -NewName PROFILE.old.ps1
New-Item -ItemType SymbolicLink -Path "$PROFILE" -Target "$env:OneDrive\Sysinternals\pws_profile.ps1" # 链接$PROFILE
#>
# 注意脚本是以 $PROFILE 文件运行,修改后运行【. $PROFILE】生效,$vmsetip也同理。
$script:Console = "$env:OneDrive\Sysinternals\Console"
# 不带参数的别名 Set-Alias -Name pslist -Value pslist64.exe
Set-Alias RunHidden "$Console\RunHiddenConsole.exe"
Set-Alias Edit "$Console\edit.exe"
Set-Alias NSSM "$Console\nssm.exe"
# 自定义函数,输入函数名就执行;一行多条命令,使用【;】分隔。
function pslist { & $Console\pslist64.exe -nobanner $args }
function pskill { & $Console\pskill64.exe -nobanner $args }
function handle { & $Console\handle64.exe -nobanner $args }
function sdelete { & $Console\sdelete64.exe -nobanner $args }
function bak-Full { & cmd /c $Console\Backups.bat full }
function tail {
if ($null -eq $args[0]) {
# Get-Date | Format-List 可以看到多信息
$LogFile = "C:\inetTmp\nginxLogs\access-$(Get-Date -Format "yyyyMM")-t725.cn.log"
} else {
$LogFile = $args[0]
}
Get-Content -Path $LogFile -tail 10
Write-Output ""
Write-Output "↗ 最后10行内容完成,或手工执行 Get-Content -Path $LogFile -tail 10"
}
function cd-Nginx {
$NginxDir = "C:\inetEnv\nginx"
cd $NginxDir
}
function NginxReload {
$cDir = $pwd.Path
cd $NginxDir
.\nginx.exe -t
if ( $? ) {
.\nginx.exe -s reload
Write-Output "↗ 重新加载成功"
} else {
Write-Output "↗ 出错了"
}
cd $cDir
}
# 脚本别名
function VmM { & "$Console\pws_vm.ps1" $args }
function Set-VmIp { & "$Console\pws_vmsetip.ps1"}
# CPU使用率,MEM可用量,C盘可用空间
function Get-CpuMemDisk {
$cpuUsage = $([Math]::Round($(Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue,0))
$ramAvailable = $(Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue
$cFree = $([Math]::Round($(Get-PSDrive|Where-Object {$_.Name -eq "C"}).Free/1GB,1))
Write-Host "Current CPU Usage: ${cpuUsage}%, Available RAM: ${ramAvailable}MB, Free space on the C drive: ${cFree}GB"
}
<#
Write-Output "脚本绝对路径: $PSCommandPath"
Write-Output "脚本目录名: $PSScriptRoot"
Write-Output "脚本文件名(含后缀): $(Split-Path $MyInvocation.MyCommand.Path -Leaf)"
Write-Output "脚本文件名(无后续): $([System.IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Path))"
$Env:PATH
$Env:PATH -split ";"
$Env:PATH += ";C:\NewPath"
获取特定路径 https://learn.microsoft.com/zh-cn/dotnet/api/system.environment.specialfolder
[System.Environment]::GetFolderPath("Desktop")
[Environment]::GetFolderPath("MyDocuments")
$Age = Read-Host "Please enter your age" # 将控制台输入,保存到变量Age
https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_automatic_variables
$args 包含传递给函数、脚本或脚本块的未声明参数的值数组。 创建函数时,可以使用 param 关键字声明参数,也可以在函数名称后面添加以逗号分隔的参数列表。
$PSItem 与 $_ 相同。 包含管道对象中的当前对象。 可以在对管道中每个对象执行操作的命令中使用此变量。
$PSVersionTable.PSVersion PowerShell 版本号
$? 最后一个操作的执行状态:成功TRUE,失败FALSE。
$LASTEXITCODE 返回上一次执行的退出码,0 表示执行成功。
#>
发表回复