Powershell example 1
来源:互联网 发布:million years ago知乎 编辑:程序博客网 时间:2024/06/16 08:23
Restart-Computers.ps1
![Start example Image from book](images/images/_.gif)
# Define input parametersparam ( [string] $filename = $(throw "Filename is required!"))Write-Host "Reading computer names from $filename"# Read file$computers = get-content $filenameforeach ($computer in $computers) { # Connect to WMI $wmi = get-wmiobject -class "Win32_OperatingSystem" ` -namespace "root/cimv2" -computer $computer # Restart computer foreach ($item in $wmi) { $wmi.reboot() write-host "Restarted " + $computer }}
ScopeTest.ps1write-host "1 (var): $var"$var = "SCRIPT"$global:var = "GLOBAL"write-host "2a (var): $var"write-host "2b (global): $global:var"function foo { write-host "3a (var): $var" write-host "3b (global): $global:var" $var = "LOCAL" $script:var = "SCRIPT!" write-host "4a (var): $var" write-host "4b (global): $global:var" write-host "4c (script): $script:var"}foowrite-host "5a (var): $var"write-host "5b (global): $global:var"write-host "5c (script): $script:var"
NegativeMatchingTest.ps1$var="The-quick-brown-fox-jumped-over-the-lazy-dog."$var2="The quick brown fox jumped over the lazy dog."$regex=[regex]"/s{1}"$varif (($regex.IsMatch($var)) -eq "False"){write-host "Expression has spaces"}else{write-host "Expression has no spaces" }$var2if (($regex.IsMatch($var2)) -eq "False"){write-host "Expression has spaces"}else{write-host "Expression has no spaces" }
ForEachFruit.ps1
#ForEachFruit.ps1$var=("apple","banana","pineapple","orange")foreach ($fruit in $var) {$i++ #this is a counter that is incremented by one each time throughwrite-host "Adding" $fruit}write-host "Added" $i "pieces of fruit"
ForEachFile.ps1
#ForEachFile.ps1set-location "C:/"$sum=0foreach ($file in get-childitem) {#$file.GetType() if (($file.GetType()).Name -eq "FileInfo") { write-host $file.fullname `t $file.length "bytes" $sum=$sum+$file.length $i++ }}write-host "Counted" $i "file for a total of" $sum "bytes."ForEachSvc.ps1#ForEachSvc.ps1get-service | foreach { if ($_.status -eq "Running") { write-host $_.displayname "("$_.status")" -foregroundcolor "Green" write-host `t "Service Type="$_.servicetype -foregroundcolor "Green" } else { write-host $_.displayname "("$_.status")" -foregroundcolor "Red" }}IfTest.ps1#IfTest.ps1$i=11if ($i-le 10) {"Less than 10"}else{"Greater than 10"}
IfElseIfTest.ps1IfElseIfTest.ps1#IfElseIfTest.ps1$i=45if ($i -le 10) {write-host "Less than 25"}elseif ($i -le 50){write-host "Less than 50"}else{write-host "Greater than 50"}
#IfElseIfTest.ps1$i=45if ($i -le 10) {write-host "Less than 25"}elseif ($i -le 50){write-host "Less than 50"}else{write-host "Greater than 50"}
- Powershell example 1
- Powershell example 2
- Powershell example 3
- Powershell example 4
- Powershell example 5
- Powershell example 6
- PowerShell实战1:Ping_Test
- PowerShell 学习入门-1
- Powershell&TFS_Part 1
- PowerShell 工作流实战1
- Powershell 工作流实战(1)
- Powershell问题解决1
- PowerShell
- PowerShell
- PowerShell
- PowerShell
- PowerShell
- PowerShell
- 获取指定页面的HTML源码
- 网络程序开发库备忘
- 关于程序员好坏差异的一个鲜为人知的秘密
- 严重:Error listenerStart
- Java web-w1:java web、jsp、JavaScript之间的区别
- Powershell example 1
- 辩证的看问题
- Redhat Enterprise Linux5安装samba服务器教程
- Powershell example 2
- 与老外打交道必知英文缩写400个
- TASK
- 今天我写了第一篇博客
- 对话框使用VIEW (转)
- 检测.net环境的C++程序