ICode9

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

Windows powershell 常用代码段

2021-07-23 12:31:15  阅读:197  来源: 互联网

标签:Windows Object System 代码段 example Content Net password powershell


变量与环境变量设置

# 变量设置
$Username=user
$Password=password

# 临时环境变量设置,powershell窗口关闭则失效
$env:Username="user"
$env:FadadaPassword="password"

# 永久生效的环境变量,系统级别
[environment]::SetEnvironmentvariable("JRE_HOME","C:\Program Files\Java\jdk1.7.0_80\jre","Machine")

# 永久生效的环境变量,当前用户
[environment]::SetEnvironmentvariable("JRE_HOME","C:\Program Files\Java\jdk1.7.0_80\jre","User")

# 通配查看环境变量
ls Env:test-*

下载不保存powershell脚本直接运行

$Username=user
$password=password
$Script="https://example.com/example.ps1"
# 下载地址为http时,去掉下面一行
[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12
$Wc=new-object System.Net.WebClient
$CredCache=new-object System.Net.CredentialCache
$Creds=new-object System.Net.NetworkCredential($env:ExampleUsername,$password)
$Wc.Credentials=$CredCache.Remove("$Script","Basic")
$CredCache.Add("$Script", "Basic", $Creds)
$Wc.Credentials=$CredCache
iex $Wc.DownloadString($Script)

一个简单的带时间戳的日志输出函数

function LogTime($Log)
{
    Get-Date -Format 'yyyy-MM-dd-HH_hh:mm:ss' | Tee-Object -Variable NowTime
    Write-Host "$NowTime $Log"
    "$NowTime $Log"|Out-File -Append "C:\info.log"
}

# 使用方法
LogTime($Log)

http状态码检测函数,状态码大于400表示网络联通性无误

function TestHttpStatusCode($url)
{
	$req = [system.Net.WebRequest]::Create($url)

	try {
		$res = $req.GetResponse()
	} 
	catch [System.Net.WebException] {
		$res = $_.Exception.Response
	}
    $HttpStatusCode=[int]$res.StatusCode
    if ( $HttpStatusCode -gt 400 -and $HttpStatusCode -ne 401) {
        LogTime("${url}无法访问,状态码为${HttpStatusCode}!!!")
        exit
    }
    else {
        LogTime("${url}连通性检测通过!!!")
    }
}

# 使用方法
$Addrs="https://baidu.com","http://qq.com"
foreach ($Addr in $Addrs) {
    TestHttpStatusCode("$Addr")
}

下载文件函数

function DownloadFile($Rfile)
{
    $Tempdir=C:\Tempdir
    $Username=user
    $Password=password
    # 访问地址为http时,去掉下面一行
    [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12
    $Filename=$Rfile.Split("/")[$Rfile.Split("/").Length-1]
    $Username=$env:ExampleUsername
    $Password=SystemTo-SecureString $env:ExamplePassword -AsPlainText -Force
    $Redential=New-Object System.Management.Automation.PSCredential($Username,$Password)
    LogTime("正在下载文件:${Rfile}")
    Invoke-WebRequest -Uri $Rfile -OutFile "${Tempdir}\${Filename}" -Credential $Redential
    if (-not $?){
        exit
    }
}

# 使用方法
$Files = $Hashtable.ExampleSystemSignPkg,"https://example.com/1.txt","https://example.com/2.txt"
foreach ($File in $Files) {
    DownloadFile("$File")
}

创建程序快捷方式至桌面函数

$Desktop = [System.Environment]::GetFolderPath('Desktop')
function SaveDesktopShortcut([string]${TargetFile}, [string]${ShortcutName}, [string]${IconLocation})
{
$Shell = New-Object -ComObject WScript.Shell
Remove-Item ${Desktop}\${ShortcutName}'.lnk' -Recurse -ErrorAction SilentlyContinue
$Shortcut = $Shell.CreateShortcut("${Desktop}\${ShortcutName}"+'.lnk')
$Shortcut.TargetPath = "$TargetFile"
$Shortcut.IconLocation = "$IconLocation"
$Shortcut.Save()
}

# 使用方法
SaveDesktopShortcut "C:\navicat\Navicat for MySQL\navicat.exe" "NavicatForMySQL" "C:\navicat\Navicat for MySQL\navicat.exe"

读取配置文件

# example.conf内容
k1=123
k2=456

$Hashtable = @{}
$Payload = Get-Content -Path 'example.conf' |
Where-Object { $_ -like '*=*' } |
ForEach-Object {
    $Infos = $_ -split '='
    $Key = $Infos[0].Trim()
    $Value = $Infos[1].Trim()
    $Hashtable.$Key = $Value
}

# 使用方法,k1为example.conf中的键
$Hashtable.k1
$Hashtable.k2

获取系统CPU、内存信息

$SysMemory=$([Math]::Round((Get-WmiObject -Class Win32_ComputerSystem).TotalPhysicalMemory /1gb))
$Cpu=get-wmiobject win32_processor
$SysCpuProcessors=@($Cpu).count*$Cpu.NumberOfLogicalProcessors

for循环

for($t=5; $t -ge 0; $t=$t-1)
{
    LogTime("${t}秒后检查系统资源(CPU、内存、磁盘)是否充足!!!")
    Start-Sleep -Seconds 1
}

防火墙管理

# 启用Windows防火墙
Set-NetFirewallProfile -All -Enabled true

# 清理法大大历史防火墙规则
Remove-NetfirewallRule -DisplayName "Tcp8080" -ErrorAction SilentlyContinue
Remove-NetfirewallRule -DisplayName "Tcp8887" -ErrorAction SilentlyContinue

# 创建防火墙规则:Tcp8080 Tcp8887
New-NetFirewallRule -Name Tcp8080 -Direction Inbound -DisplayName 'Tcp8080' -LocalPort 8080 -Protocol 'TCP' -ErrorAction SilentlyContinue
New-NetFirewallRule -Name Tcp8887 -Direction Inbound -DisplayName 'Tcp8887' -LocalPort 8887 -Protocol 'TCP' -ErrorAction SilentlyContinue

文本字符串替换

$ConfigFile=a.conf
$CatalinaFile=catalina.bat
SystemConfig=System.conf

# 简单字符串替换,
(Get-Content "$ConfigFile") | Foreach-Object {$_ -replace '256',"${SystemMaxMemoryMB}"} | Set-Content "$ConfigFile"
# 字符串替换,带字符串拼接
(Get-Content -Encoding utf8 "$CatalinaFile") | Foreach-Object {$_ -replace 'Xmx3g',"Xmx${SystemMaxMemory}g"} | Set-Content -Encoding utf8 "$CatalinaFile"
# 字符串替换,以test开头的全部替换
(Get-Content -Encoding utf8 "$SystemConfig") | Foreach-Object {$_ -replace '(?<=^)test.*',"qcloud.secretkey=$Hashtable.ExampleQcloudSecretKey"} | Set-Content -Encoding utf8 "$SystemConfig"
# 字符串替换,带转义的
(Get-Content -Encoding utf8 "$SystemConfig") | Foreach-Object {$_ -replace '=D\\:','=C:'}  | Set-Content -Encoding utf8 "$SystemConfig"

标签:Windows,Object,System,代码段,example,Content,Net,password,powershell
来源: https://www.cnblogs.com/erbiao/p/15048348.html

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

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

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

ICode9版权所有