Powershell example 2

简介: ProcessCPU.ps1 #ProcessCPU.ps1 $process=get-process $low=0 #counter for low cpu processes $med=0 #counter for medi...
ProcessCPU.ps1
Image from book
#ProcessCPU.ps1
$process=get-process
$low=0 #counter for low cpu processes
$med=0 #counter for medium cpu processes
$high=0 #counter for high cpu processes
foreach ($p in $process) {
[int]$cpuseconds="{0:F0}" -f $p.cpu

 if ($cpuseconds -le 300) {
  write-host $p.name $cpuseconds "seconds" -foregroundcolor "Green"
  $low++
 elseif (($cpuseconds -ge 301) -AND ($cpuseconds -le 1000)) {
  write-host $p.name $cpuseconds "seconds"
  $med++
  }
 else
  {
  write-host $p.name $cpuseconds "seconds" -foregroundcolor "Red"
  $high++
  }
}
#display a summary message
write-host `n"Process Summary"
write-host "-->" $low "low CPU processes"
write-host "-->" $med "medium CPU processes"
write-host "-->" $high "high CPU processes"
Image from book
 
SwitchRegex.ps1
Image from book
#SwitchRegex.ps1
$var="PowerShell123","PowerShell","123","PowerShell 123"
Switch -regex ($var) {
"^/w+[a-zA-Z]$" {write-host $_" is a word"}
"^/d+$" {write-host $_" is a number"}
"/s" {write-host $_" has a space"}
Default {write-host "No match found for"$_}
}
Image from book
 

SwitchNoContinue.ps1

Image from book
#SwitchNoContinue.ps1
$var="PowerShell123","PowerShell","123","PowerShell 123"
Switch -regex ($var) {
"/w" {write-host $_" matches /w"}
"/d" {write-host $_" matches /d"}
"/s" {write-host $_" matches /s"}
Default {write-host "No match found for"$_ }
}

Image from book

SwitchContinue.ps1

Image from book
#SwitchContinue.ps1
$var="PowerShell123","PowerShell","123","PowerShell 123"
Switch -regex ($var) {
"/w" {write-host $_" matches /w" ;
      continue}
"/d" {write-host $_" matches /d" ;
      continue}
"/s" {write-host $_" matches /s" ;
      continue}
Default {write-host "No match found for"$_ ;
      }
}
Image from book
ServiceDemo.ps1
Image from book
#ServiceDemo.ps1

$services=Get-wmiobject -class "Win32_service"
foreach ($svc in $services) {
 if (($svc.startmode -eq "Auto") -AND ($svc.state -ne "Running")) {
  write-host $svc.displayname $svc.state $svc.startmode `
  -backgroundcolor "White" -foregroundcolor "Red"
  }
 else
  {
  write-host $svc.displayname $svc.state $svc.startmode
  }
 }$services=Get-wmiobject -class "Win32_service"
foreach ($svc in $services) {
 if (($svc.startmode -eq "Auto") -AND ($svc.state -ne "Running")) {
  write-host $svc.displayname "["$svc.state"]" "["$svc.startmode"]" `
  -backgroundcolor "White" -foregroundcolor "Red"
  }
 else
  {
  write-host $svc.displayname "["$svc.state"]" "["$svc.startmode"]"
  }
 }
Image from book
Service2HTML.ps1
Image from book
#Service2HTML.ps1
# a style sheet, style.css, should be in the same directory
# as the saved html file.

$server=hostname
$body="Services Report for "+$server.ToUpper()+"<HR>"
$file="c:/"+$server+"-services.html"

write-host "Generating Services Report for "$server.ToUpper()

get-service |sort -property status -descending | ConvertTo-HTML `
Name,DisplayName,Status -Title "Service Report" `
-Head "<link rel=stylesheet type=text/css href=style.css>" `
-Body $body | out-file $file

write-host "Report Generation Complete! Open" $file "for results."
Image from book
 
HelloForm.ps1
Image from book
#HelloForm.ps1
[void][Reflection.Assembly]::LoadWithPartialName(`
"System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
#default form size is 300x300 pixels
$Form.width=250
$form.height=200
$Label=new-object System.Windows.Forms.Label
$Label.Text="Hello World"
$Label.visible=$true
$Form.Text = "PowerShell TFM"
$Button = New-Object System.Windows.Forms.Button
$Button.Text = "OK"
#set button vertical button position
$Button.Top=$Form.Height*.50

#default button width is 75
#Center button horizontally
$Button.left=($Form.Width*.50)-75/2

$Button.Add_Click({$Form.Close()})
$Form.Controls.Add($Button)
$Form.Controls.Add($Label)
$Form.ShowDialog()
Image from book
 
HelloForm.ps1
Image from book
#HelloForm.ps1
[void][Reflection.Assembly]::LoadWithPartialName(`
"System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
#default form size is 300x300 pixels
$Form.width=250
$form.height=200
$Label=new-object System.Windows.Forms.Label
$Label.Text="Hello World"
$Label.visible=$true
$Form.Text = "PowerShell TFM"
$Button = New-Object System.Windows.Forms.Button
$Button.Text = "OK"
#set button vertical button position
$Button.Top=$Form.Height*.50

#default button width is 75
#Center button horizontally
$Button.left=($Form.Width*.50)-75/2

$Button.Add_Click({$Form.Close()})
$Form.Controls.Add($Button)
$Form.Controls.Add($Label)
$Form.ShowDialog()
Image from book
 
ServicesGrid.ps1
Image from book
#ServicesGrid.ps1

[void][reflection.assembly]::LoadWithPartialName(`
"System.Windows.Forms")
[void][reflection.assembly]::LoadWithPartialName("System.Drawing")

$form = new-object System.Windows.Forms.Form
$form.Size = new-object System.Drawing.Size 400,500
$Form.Text = "PowerShell TFM"

$DataGridView = new-object System.windows.forms.DataGridView

$array= new-object System.Collections.ArrayList

$data=@(get-service | write-output)
$array.AddRange($data)
$DataGridView.DataSource = $array
$DataGridView.Dock = [System.Windows.Forms.DockStyle]::Fill
$DataGridView.AllowUsertoResizeColumns=$True

$form.Controls.Add($DataGridView)
$form.topmost = $True
$form.showdialog()
Image from book
 

 

 

目录
相关文章
Powershell example 1
Restart-Computers.ps1 # Define input parameters param ( [string] $filename = $(throw "Filename is required!") ) ...
727 0
Powershell example 3
Blocktest.ps1 $sb = { foreach ($process in $input) { $process.
805 0
Powershell example 4
BulkCopy.ps1 #BulkCopy.ps1 Set-Location "C:/Logs" $files=Get-ChildItem |where {$_.
830 0
|
安全
Powershell example 5
SetPermswithCACLS.ps1 #SetPermsWithCACLS.ps1 # CACLS rights are usually # F = FullControl # C = Change # R = Reado...
989 0
|
XML 安全 Go
Powershell example 6
GetSecurityRSS.ps1 #GetSecurityRSS.ps1 #Query Microsoft's Basic Security Feed for latest bulletins #Critical bullet...
976 0
|
Windows
Powershell 重新排列去重 Windows环境变量
【9月更文挑战第13天】本文介绍如何使用PowerShell对Windows环境变量进行重新排列和去重。首先通过`$env:`访问环境变量,接着使用`-split`命令分割路径,再利用`Select-Object -Unique`去除重复项。之后可根据需要对路径进行排序,最后将处理后的路径组合并更新环境变量。注意修改环境变量前应备份重要数据并了解潜在影响。
606 10
|
监控 关系型数据库 MySQL
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
786 0
|
XML 监控 数据格式
利用powershell进行windows日志分析
0x00 前言   Windows 中提供了 2 个分析事件日志的 PowerShell cmdlet:一个是Get-WinEvent,超级强大,但使用起来比较麻烦;另一个是Get-EventLog,使得起来相当简单,可以实时筛选,接下来,我们利用PowerShell 来自动筛选 Windows 事件日志。
2980 0
|
存储 Ubuntu Linux
windows可以安装Ubuntu,ubuntu上也可以安装Powershell
powerhsell除了可以在windows上使用外,还可以在Ubuntu上部署开发环境。下面介绍Ubuntu上安装powershell的方法。
852 0
|
Shell Linux 开发工具
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的

热门文章

最新文章