[TOC]
3.运行脚本
描述: 脚本和批处理都属于伪可执行文件
,它们只是包含了若干命令行解释器能够解释和执行
的命令行代码。
1 | # 1.执行批处理文件:批处理是扩展名为”.bat”的文本文件,它可以包含任何cmd控制台能够处理的命令 |
注意事项:
- 初次执行PS1脚本时可能会碰到一个异常,由于默认安全设置禁用了执行脚本,要启用这个功能需要拥有管理员的权限。
$C = Get-Culture | Select-Object -Property *
@{TestKey = (‘x’ * 200)} | Out-String -Width 250
PS C:\Users\WeiyiGeek> Get-Command -Name *Printer
CommandType Name Version Source
Function Add-Printer 1.1 PrintManagement
Function Get-Printer 1.1 PrintManagement
Function Remove-Printer 1.1 PrintManagement
Function Rename-Printer 1.1 PrintManagement
Function Set-Printer 1.1 PrintManagement
Cmdlet Out-Printer 3.1.0.0 Microsoft.PowerShell.Utility
选择对象的属性
包含在每一个对象中的属性可能有很多,但是并不是所有的属性你都感兴趣,这时可以使用Select-Object 限制对象的属性。接下来的例子演示如果获取机器上匿名帐号的完整信息。
PS C:Usersv-bali.FAREAST> Get-WmiObject Win32_UserAccount -filter “LocalAccount=True AND Name=’guest’”
AccountType : 512
Caption : myhomeguest
Domain : myhome
SID : S-1-5-21-3064017030-3269374297-2491181182-501
FullName :
Name : guest
如果你只对用户名、描述,启用感兴趣。
PS C:Powershell> Get-WmiObject Win32_UserAccount -filter “LocalAccount=True AND
Name=’guest’” | Select-Object Name,Description,Disabled
Name Description Disabled
guest Built-in account for gu… True
#使用比较运算符”like”过滤当前应用的数组, 这里可以用比较运算符结合控制台命令匹配出所有条件。
@(ipconfig /all) -like “IPV4“
pageid: 327
IPv4 地址 . . . . . . . . . . . . : 192.168.1.88(首选)
形用户界面
Cmdlet
Out-GridView
Show-Command
Show-ControlPanelItem
Show-EventLog
参数
PS编程之命令行参数传递与绑定:
方法1:$args
参数位置传值法
它实际是一个对象数组,注意输入参数的位置是固定的并且$args[0]
表示命令中输入的第一个参数
并非脚本名称(与Bash Shell有区别
)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# args.ps1 文件内容
Write-Host $args[0]
Write-Host $args[1]
Write-Host $args[2]
Write-Host "命令行:$args" #注意在""之中 $args[0] 加入没有任何意义,它只会解析$args 数组;
Write-Host -noNewLine "命令行:" $args[0] $args[1] $args[2]
#执行
$args.ps1 1 "参数2" param3
#执行结果
1
参数2
param3
命令行:1 参数2 param3
命令行:1 参数2 param3方法2:CmdletBinding
脚本内部变量名
,适合于传递多个参数值可以指定参数名称,并且参数值的位置随机。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19# DeployBuildEnv.ps1 文件内容
[CmdletBinding()]
Param (
[string] $Name = "",
[Int32] $Age = 0,
[string] $Gender = "未知"
)
$arg = $Name + " - " + $age + " - " + $Gender;
Write-Host $arg
Write-Host "命令行绑定: $arg"
#执行
.\DeployBuildEnv.ps1 -Name WeiyiGeek -Age 20 -Gender 男
#执行结果
WeiyiGeek - 20 - 男
命令行绑定: WeiyiGeek - 20 - 男
注意事项:
- #必须放在脚本最上面否则报错CmdletBinding
- PS命令行支持:单引号,双引号,中文的单引号,中文的双引号 ,以及中文变量名,中文参数名(
linux版的powershell也完全支持
)。
模块查找与安装:
Tips:没有该find-module模块的点击,注意需要以管理员权限运行;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# 模块查找
find-module *ssh*
# Version Name Repository Description
# ------- ---- ---------- -----------
# 2.2 Posh-SSH PSGallery Provide SSH and SCP functionality for executing commands against remote hosts.
# 0.0.3 dockeraccesshelper PSGallery Allow a user account to access the docker engine without elevated access rights
# 安装Posh-SSH的模块
Install-Module Posh-SSH
# 你正在从不受信任的存储库安装模块。如果你信任该存储库,请通过运行 Set-PSRepository cmdlet 更改其 InstallationPolicy
# 值。是否确实要从“PSGallery”安装模块?
[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为“N”): y
# 删除模块
# 删除Posh-SSH的目录:C:\Program Files\WindowsPowerShell\Modules\Posh-SSH
remove-module -name posh-ssh -Force -Verbose -Debug
#查看模块命令
get-command -Module posh-ssh
在PowerShell中如何检测模块是否存在
我们可以使用Get-Module的另一个参数-ListAvailable来列出是否含有潜在的模块。
Get-Module -ListAvailable -Name Azure
Powershell检测网络中存活地址1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26# 方式1
for($i=1; $i -le 255; $i++){
ping -n 3 10.0.0.$i >> Paddr.txt
}
# 方式2
$Ping = New-Object system.net.networkinformation.ping
1..253 | % { $Ping.send( "192.168.12.$_" ) | select address,status} | Out-File -FilePath IPaddr.txt -Encoding utf8
$IP = Get-Content "IPaddr.txt" | Where-Object { $_ -match "Success" }
-split $IP | Where-Object { $_ -like "192*" } | Out-File -FilePath IP-Successful.txt -Encoding utf8
$hostname = foreach ( $i in $IPS ) { [System.Net.DNS]::GetHostByAddress($i).HostName;$i}
$hostname
[System.Net.DNS]::GetHostName()
[System.Net.DNS]::GetHostAddresses("192.168.12.188")
188..199 | % { [System.Net.DNS]::GetHostEntry("192.168.12.194").HostName; }
188..199 | % { [System.Net.DNS]::GetHostEntry("192.168.12.$_").HostName; }
HostName Aliases AddressList
-------- ------- -----------
USER-MVFI1L2V69 {} {fe80::5936:33a7:29bf:7d6e%5, 192.168.12.188}
https://docs.microsoft.com/zh-cn/dotnet/api/system.net.dns?view=netcore-3.1
下面再附带两个循环读取一个文件夹下的所有子文件夹下的、所有的文件个数的代码:
Get-ChildItem -Path $env:windir -Force -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.PSIsContainer -eq $false } |
Measure-Object |
Select-Object -ExpandProperty Count
[System.IO.Directory]::GetFiles(“F:\gzkz-2020\new”, “*”, “AllDirectories”).Count
1 | param |
1 | PS C:/Documents and Settings/Administrator> ./Convert-Image.ps1 -inFile 01.jpg |
1 | $gifs=dir -Path "C:\Users\WeiyiGeek\Documents\Tencent Files\291238737\FileRecv\GIF" -Filter '*.gif'; |
类库探源——System.Drawing.Bitmap
https://www.cnblogs.com/Aphasia/p/4158827.html
http://www.colorconsole.de/PS_Windows/de/Get-Counter.htm
Get-Counter
该函数下可以把现有的电脑监控统计数据 直接提取出来 ,