Powershell的一些运用
设置环境变量
调用以下函数,设置变量名,变量值,即可设置系统的环境变量。另外,“Machine”表示设置的是系统环境变量
[Environment]::SetEnvironmentVariable( $VariableName, $Value, "Machine" )
stopped和disabled service
主要使用Get-Service, Stop-Service, Set-Service来直接对Service操作,有时直接通过Set-Service来stopped和disabled service,但常常会有no permission或者dependency的问题,这时可以先Stop-Service,然后通过Set-Service来disable service
$service = $service_name
$ServiceResult = Get-Service -Name $service -ErrorAction Stop | select -Property Name, StartType, Status
if ($ServiceResult.Status -ne "Stopped"){
try{
Stop-Service -Name $ServiceResult.Name -ErrorAction Stop
$ServiceStatus = (Get-Service -Name $service -ErrorAction Stop | select -Property Name, StartType, Status).Status
Write-Host "$service status is $ServiceStatus"
} catch {
Write-Error "$service cannot stop, it may be default and no permission"
}
} else {
Write-Host "$service status is already Stopped"
}
if ($ServiceResult.StartType -ne "Disabled"){
try{
Set-Service -Name $ServiceResult.Name -StartupType disabled -ErrorAction Stop
$ServiceStartType = (Get-Service -Name $service -ErrorAction Stop | select -Property Name, StartType, Status).StartType
Write-Host "$service StartType is $ServiceStartType"
} catch{
Write-Error "$service cannot update StartpType, it may be default and no permission"
}
} else {
Write-Host "$service StartType is already Disabled"
}
设置注册表
比如设置自动登录,使用Set-ItemProperty
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "autoadminlogon" -Value 1 -Type "dword"
设置电源计划PowerPlan
调用了win32_PowerPlan类,获取对应PowerPlan的id,然后进行设置
$plan = Get-WmiObject -Namespace root\cimv2\power -Class win32_PowerPlan -Filter "ElementName = 'High performance'"
$guid = $plan.InstanceID.tostring()
# find index of sub string to get instance id
$indexOfLeft = $guid.indexOf("{") + 1
$idLength = $guid.indexOf("}") - $indexOfLeft
$instanceID = $guid.Substring($indexOfLeft, $idLength)
# Active power plan
if ($Plan.IsActive -eq $false){
powercfg -setactive $instanceID
}
# "Changing Display Settings to Never Turn Off and Never Sleep"
powercfg -change -monitor-timeout-ac 0
powercfg -change -monitor-timeout-dc 0
powercfg -change -standby-timeout-ac 0
powercfg -change -standby-timeout-dc 0
使用chocolatey来安装软件
chocolatey是一个用来安装软件的辅助工具,在自动化部署中,非常方便,只需简单的命令,不仅能够将软件下载,还能相应设置环境变量,可以参考其官网的介绍和使用。
1、安装chocolatey,打开powershell,执行以下代码就可以安装chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
2、其安装软件的命令,简单的如下,只需“”choco install 软件名”就可以安装,比如安装Python,在cmd或者powershell中,执行如下命令,即可实现安装,同时还会设置好环境变量,你也可以加上--version 来设置指定版本下载
choco install python
3、自动化安装软件的部署,我们会将chocolatey安装软件,写成函数的形式,实现判断本地是否已经安装,是否指定软件版本,然后进行安装。函数如下:
# 安装函数
Function ChocoInstall([string]$package, [string]$version){
# get result that package whether has been installed
$result = clist -lo -e $package
if ("$result" -match ".*(\d+) packages installed."){
# 0 means that package doesn't install
if ($matches[1] -eq 0){
if ($version -eq ""){
choco install -y $package
} else {
choco install -y $package --version $version
}
} elseif ($matches[1] -eq 1){
Write-Host "$package already installed"
}
}
}
# 调用函数,安装python 3.6.1版本
ChocoInstall "python" "3.6.1"