Powershell example 1

简介: Restart-Computers.ps1 # Define input parameters param ( [string] filename=(throw "Filename is required!") ) ...

Restart-Computers.ps1

Image from book
# Define input parameters
param (
  [string] $filename = $(throw "Filename is required!")
)

Write-Host "Reading computer names from $filename"

# Read file
$computers = get-content $filename
foreach ($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.ps1
Image from book
write-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"
}

foo

write-host "5a (var): $var"
write-host "5b (global): $global:var"
write-host "5c (script): $script:var"
Image from book
 
NegativeMatchingTest.ps1
Image from book
$var="The-quick-brown-fox-jumped-over-the-lazy-dog."
$var2="The quick brown fox jumped over the lazy dog."
$regex=[regex]"/s{1}"
$var
if (($regex.IsMatch($var)) -eq "False")
{
write-host "Expression has spaces"
}
else
{
write-host "Expression has no spaces" }

$var2
if (($regex.IsMatch($var2)) -eq "False")
{
write-host "Expression has spaces"
}
else
{
write-host "Expression has no spaces" }
Image from book

 

ForEachFruit.ps1

Image from book
#ForEachFruit.ps1
$var=("apple","banana","pineapple","orange")
foreach ($fruit in $var) {
$i++ #this is a counter that is incremented by one each time through
write-host "Adding" $fruit
}
write-host "Added" $i "pieces of fruit"

 

ForEachFile.ps1

Image from book
#ForEachFile.ps1
set-location "C:/"
$sum=0
foreach ($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 
Image from book
#ForEachSvc.ps1

get-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"
  }
}
Image from book
IfTest.ps1 
Image from book
#IfTest.ps1
$i=11
if ($i-le 10) {
"Less than 10"
}
else
{
"Greater than 10"
}
Image from book

 

IfElseIfTest.ps1
Image from book
#IfElseIfTest.ps1

$i=45
if ($i -le 10) {
write-host "Less than 25"
}
elseif ($i -le 50)
{
write-host "Less than 50"
}
else
{
write-host "Greater than 50"
}
Image from book
 
IfElseIfTest.ps1
Image from book
#IfElseIfTest.ps1

$i=45
if ($i -le 10) {
write-host "Less than 25"
}
elseif ($i -le 50)
{
write-host "Less than 50"
}
else
{
write-host "Greater than 50"
}
Image from book
目录
打赏
0
0
0
0
20
分享
相关文章
Powershell example 2
ProcessCPU.ps1 #ProcessCPU.ps1 process=getprocesslow=0 #counter for low cpu processes $med=0 #counter for medi...
832 0
Powershell example 3
Blocktest.ps1 sb = {   foreach (process in input) {process.
714 0
Powershell example 4
BulkCopy.ps1 #BulkCopy.ps1 Set-Location "C:/Logs" files=Get-ChildItem |where {_.
761 0
Powershell example 5
SetPermswithCACLS.ps1 #SetPermsWithCACLS.ps1 # CACLS rights are usually # F = FullControl # C = Change # R = Reado...
903 0
Powershell example 6
GetSecurityRSS.ps1 #GetSecurityRSS.ps1 #Query Microsoft's Basic Security Feed for latest bulletins #Critical bullet...
887 0
|
6月前
|
Powershell 重新排列去重 Windows环境变量
【9月更文挑战第13天】本文介绍如何使用PowerShell对Windows环境变量进行重新排列和去重。首先通过`$env:`访问环境变量,接着使用`-split`命令分割路径,再利用`Select-Object -Unique`去除重复项。之后可根据需要对路径进行排序,最后将处理后的路径组合并更新环境变量。注意修改环境变量前应备份重要数据并了解潜在影响。
198 10
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
210 0
|
10月前
|
windows可以安装Ubuntu,ubuntu上也可以安装Powershell
powerhsell除了可以在windows上使用外,还可以在Ubuntu上部署开发环境。下面介绍Ubuntu上安装powershell的方法。
271 0
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的
150 0

相关课程

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等