powershell札记2_脚本学习
2019-06-14 本文已影响1人
皮皮大
添加windows防火墙入站规则
PowerShell
命令New-NetFirewallRule
可以很方便的快速添加入站规则到本地的防火墙里,学习网址如下:
- 允许80端口的入站规则
Write-Host -ForegroundColor Cyan "Creating new inbound rule Http"
New-NetFirewallRule -DisplayName "Http" -Direction Inbound -LocalPort 80 -RemotePort Any -Protocol TCP -Enabled True
- 允许80端口的出站规则
Write-Host -ForegroundColor Cyan "Creating new outbound rule Http"
New-NetFirewallRule -DisplayName "Http" -Direction Outbound -LocalPort 80 -RemotePort Any -Protocol TCP -Enabled True
- 允许ICMPv4协议入站规则
Write-Host -ForegroundColor Cyan "Creating new rule Allow Ping"
New-NetFirewallRule -DisplayName "Allow Ping" -Description "Allow ping" -Protocol ICMPv4 -IcmpType 8 -Enabled True -Profile Any -Action Allow
- 利用powershell自动查找最占内存的10个进程
# create new excel instance
$objExcel = New-Object -comobject Excel.Application # 创建一个Excel对象
$objExcel.Visible = $True
$objWorkbook = $objExcel.Workbooks.Add()
$objWorksheet = $objWorkbook.Worksheets.Item(1)
# write information to the excel file
$i = 0
$first10 = (ps | sort ws -Descending | select -first 10) # 显示占用内存最大的10个进程。
$first10 | foreach -Process {$i++; $objWorksheet.Cells.Item($i,1) = $_.name; $objWorksheet.Cells.Item($i,2) = $_.ws}
$otherMem = (ps | measure ws -s).Sum - ($first10 | measure ws -s).Sum
$objWorksheet.Cells.Item(11,1) = "Others"; $objWorksheet.Cells.Item(11,2) = $otherMem
# draw the pie chart
$objCharts = $objWorksheet.ChartObjects()
$objChart = $objCharts.Add(0, 0, 500, 300)
$objChart.Chart.SetSourceData($objWorksheet.range("A1:B11"), 2)
$objChart.Chart.ChartType = 70
$objChart.Chart.ApplyDataLabels(5)
# 运行结果如下
image.png
- .NET Framework中包含了一个异常强大的库,而微软为了保证二进制层面上跨语言的兼容性,很多库都是用COM封装的。PowerShell的一大特色就是可以直接调用这些库。