ProcessCPU.ps1
#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"
SwitchRegex.ps1
#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"$_} }
SwitchNoContinue.ps1
#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"$_ } }
|
SwitchContinue.ps1
#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"$_ ; } }
ServiceDemo.ps1
#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"]" } }
Service2HTML.ps1 #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." HelloForm.ps1 #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() HelloForm.ps1 #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() ServicesGrid.ps1 #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() |
时间: 2024-11-18 21:12:56