网站建设人员,现在最火的电商平台是什么,教育培训网络推广培训,个人主页设计代码poweshell介绍
官方介绍#xff1a;https://docs.microsoft.com/zh-cn/powershell/scripting/overview?viewpowershell-5.1
Windows PowerShell 是一种命令行外壳程序和脚本环境#xff0c;使命令行用户和脚本编写者可以利用 .NET Framework的强大功能。
.NET Framework …poweshell介绍官方介绍https://docs.microsoft.com/zh-cn/powershell/scripting/overview?viewpowershell-5.1Windows PowerShell 是一种命令行外壳程序和脚本环境使命令行用户和脚本编写者可以利用 .NET Framework的强大功能。.NET Framework 是管理面向 .NET Framework 的应用的运行时执行环境。 它包括公共语言运行时提供内存管理和其他系统服务和一个全面的类库使程序员能利用强大可靠的代码实现所有主要领域的应用开发。PowerShell ISE是基于图形用户界面的应用程序, 并且是Windows PowerShell的默认编辑器。 ISE代表集成脚本环境。它是一个接口, 我们可以在其中运行命令以及编写, 测试和调试PowerShell脚本, 而无需在命令行界面中编写所有命令启动powershell现在的Windows系统中一般都集成了PowerShell启动方法有如下几种a) 开始菜单 - 运行run - 输入”PowerShell”。启动成功后和上图一样。b) 命令提示符Command Prompt - PowerShell。启动后效果如下版本查询修改脚本执行策略windows系统默认禁止脚本运行以管理员身份运行先放开并允许所有的script运行程序、脚本、bat命令调用 C:\Users\Admin\AppData\Local\google\Chrome\Application\chrome.exe ./echohello.ps1 cmd /c whoamiPowerShell帮助PowerShell命令叫做cmdlet。所有的cmdlet命令规则都遵循动词-名词这种语法结构如Get-Command、Get-Content等如下Get-Command命令意思是获取包含get的所有命令集合获取命令帮助文档计算命令执行时间如果想计算一个命令执行时间可以使用Measure-Command命令如下命令的别名PowerShell一些内置命令都有别名方便记忆和输入可以用Get-Alias命令查看别名Get-Alias |Group-Object Definition |where {$_.Name -match get}对象类型、属性和方法网络和端口测试返回值判断与cmdlet命令使用$?对bat命令或者程序使用$LASTEXITCODEPS C:\Users\lixiang where.exe curl C:\Windows\System32\curl.exe PS C:\Users\lixiang $LASTEXITCODE 0 PS C:\Users\lixiang Write-Host HI HI PS C:\Users\lixiang $? True下载与解压## 适应于http下载原生server 2008 及以上 速度比invoke-webrequest 快 function Download-File($file_url, $file_path) { $OSversion [Environment]::OSVersion.Version if ($OSversion.Major -le 6) { $client New-Object -TypeName System.Net.WebClient $client.DownloadFile($file_url, $file_path) } else { #在server2012中NET Framework 小于4.6,使用Invoke-WebRequest https://example 出现“请求被中止: 未能创建 SSL/TLS 安全通道”的解决办法 [System.Net.ServicePointManager]::SecurityProtocol[System.Net.SecurityProtocolType]::Tls12 Invoke-WebRequest -Uri $file_url -OutFile $file_path -UseBasicParsing } if (! $?) { throw $file_url download to $file_path error } } Download-File $bvsshUrl $filePath function Unzip-File($file_path, $file_dir) { if (!(Test-Path $file_path)) { throw Not exist $file_path } if (!(Test-Path $file_dir)) { New-Item -ItemType Directory $file_dir -Force } $OSversion [Environment]::OSVersion.Version if ($OSversion.Major -lt 10) { # 此方法在wincore2004 上无法使用所以建议只对win10以下版本使用 $shellApp New-Object -ComObject Shell.Application $files $shellApp.NameSpace($file_path).Items() # 删除已存在的解压后的文件 $files|where{Remove-Item ($file_dir/{0}* -f $_.name ) -Force} $shellApp.NameSpace($file_dir).CopyHere($files) } else { Expand-Archive -Path $file_path -DestinationPath $file_dir -Force if (! $?) {throw Unzip $file_path failed} } }文件和目录# 文本查看 Get-Content -Path .\Desktop\aaa.txt # 持续查看类似tail -f Get-Content -Path .\Desktop\aaa.txt -ReadCount 0 -Tail 2 -Wait # 目录创建 New-Item -Type Directory -Path .\aa # 目录删除 Remove-Item -Recurse -Force -Path .\aa服务和进程# 查看服务状态 Get-Service $service | Select-Object -Property Name, StartType, Status # 结束任务进程 Get-Process -ProcessName DingTalk |foreach { cmd /c taskkill.exe /pid $_.Id /f}脚本和函数传参# 1 $args传参 foreach ($i in $args) { Write-Host $i -NoNewline } # 2 命令行参数绑定 param ( [string]$Name$(throw Parameter missing: -name Name), [int]$Age$(throw Parameter missing: -age x as number), [switch]$Man ) # 3 设置命令行参数集 使用ParameterSetName解析命令行参数可有效的设置参数集 Param ( [CmdletBinding(DefaultParameterSetNameSetUp)] [Parameter(ParameterSetNameSetUp, Mandatory$False)] [String]$ClientName, [Parameter(ParameterSetNameRun, Mandatory$True)] [String]$SK, [Parameter(ParameterSetNameRun, Mandatory$True)] [String]$AK, [Parameter(ParameterSetNameRun, Mandatory$True)] [Parameter(ParameterSetNameSetUp, Mandatory$True)] [String]$Params$(Throw Parameter Missing: -Params xxx As String.) ) 通过$PSCmdlet.ParameterSetName查看脚本运行的哪个参数集 $SexWoman if ( $Man ) {$SexMan} write-host (name: {0} age: {1} sex: {2} -f($Name, $Age, $Sex)) ################################# # 1 位置传参 function my-test($Name, $Age24){ Write-Host (name:{0}, age: {1} -f($Name, $Age)) } my-test lixiang 26 # 2 参数绑定 function my-test { param ( [string]$Name lx, [int]$Age 24 ) Write-Host (name:{0}, age: {1} -f $Name, $Age) } my-test -Name ddd -Age 23 my-test二进制传参$a (list disk, list vol) $p C:\Windows\System32\diskpart.exe $a | $p数组和字典# array 数组 $array New-Object -TypeName System.Collections.ArrayList $array.Insert(0, qaz) foreach ($i in 1..3) {$array.Add($i)} Write-Host $array # hash表 字典 $dict New-Object -TypeName System.Collections.Hashtable $a (lixiang, xiaoming) $b (26, 25) for($i0; $i -lt $($a.Count); $i) { $dict[$a[$i]]$b[$i] } foreach ($i in $dict.Keys) { Write-Host ({0}: {1} -f($i, $dict[$i])) } # 结果输出 xiaoming: 25 lixiang: 26计算文件的hash值# MDS值 Get-FileHash -Path .\aa.ps1 -Algorithm MD5 # SHA256值 Get-FileHash -Path .\aa.ps1 -Algorithm SHA256其他# windows server 支持 sconfig # 查看系统配置 msconfig # 查看当前用户 whoami # 查看系统信息 systeminfo