Difference between revisions of "PowerShell"
(→call operator &) |
|||
(10 intermediate revisions by the same user not shown) | |||
Line 22: | Line 22: | ||
<source lang="Powershell"> | <source lang="Powershell"> | ||
Send-Greeting -name Claude | Send-Greeting -name Claude | ||
+ | |||
+ | |||
Line 37: | Line 39: | ||
} | } | ||
} | } | ||
+ | </source> | ||
+ | |||
+ | == if == | ||
+ | <source lang="powershell"> | ||
+ | $currentDate = Get-Date | ||
+ | $messageOfTheDay = "" | ||
+ | |||
+ | if($currentDate.Day % 2 -ne 0) | ||
+ | { | ||
+ | $messageOfTheDay = "Today is an odd Day, " | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | $messageOfTheDay = "Today all should be even, " | ||
+ | } | ||
+ | |||
+ | switch (($currentDate.DayOfWeek)) | ||
+ | { | ||
+ | "Monday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." } | ||
+ | "Tuesday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." } | ||
+ | "Wednesday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." } | ||
+ | "Thursday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." } | ||
+ | "Friday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." } | ||
+ | "Saturday" { $messageOfTheDay = $messageOfTheDay + "yay it's the Weekend!!!" } | ||
+ | "Sunday" { $messageOfTheDay = $messageOfTheDay + "yay it's the Weekend!!!" } | ||
+ | default { $messageOfTheDay = $messageOfTheDay + "This script only knows the 7 weekdays...." } | ||
+ | } | ||
+ | |||
+ | echo $messageOfTheDay | ||
+ | </source> | ||
+ | |||
+ | == for, while, doWhile == | ||
+ | <source lang="powershell"> | ||
+ | for ($i = 0; $i -lt 10; $i++) | ||
+ | { | ||
+ | Write-Host $i | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | <source lang="powershell"> | ||
+ | $i = 0 | ||
+ | while ($i -lt 10) | ||
+ | { | ||
+ | Write-Host $i | ||
+ | $I++; | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | <source lang="powershell"> | ||
+ | $i = 0 | ||
+ | Do | ||
+ | { | ||
+ | Write-Host $i | ||
+ | $I++; | ||
+ | } while($i -lt 10) | ||
+ | </source> | ||
+ | |||
+ | == array == | ||
+ | <source lang="powershell"> | ||
+ | $myarray = 'a', 'b', 'c' | ||
+ | $myarray | ||
+ | </source> | ||
+ | |||
+ | == WHERE == | ||
+ | <source lang="powershell"> | ||
+ | Clear-Host #Clears the display | ||
+ | $GuyDir = "C:\Temp" | ||
+ | $FilesExe = gci $GuyDir -recurse #GCI: Get Child Items | ||
+ | $List = $FilesExe | ? {$_.extension -eq ".pem"} | ||
+ | $List | Sort-Object -unique | Format-Table name | ||
+ | </source> | ||
+ | |||
+ | <source lang="powershell"> | ||
+ | Clear-Host #Clears the display | ||
+ | $GuyDir = "C:\Temp" | ||
+ | $FilesExe = gci $GuyDir -recurse #GCI: Get Child Items | ||
+ | $List = $FilesExe | WHERE {$_.extension -eq ".pem"} | ||
+ | $List | Sort-Object -unique | Format-Table name | ||
+ | </source> | ||
+ | |||
+ | <source lang="powershell"> | ||
+ | Clear-Host #Clears the display | ||
+ | $GuyDir = "C:\Temp" | ||
+ | $FilesExe = gci $GuyDir -recurse #GCI: Get Child Items | ||
+ | $List = $FilesExe | Where-Object {$_.extension -eq ".pem"} | ||
+ | $List | Sort-Object -unique | Format-Table name | ||
+ | </source> | ||
+ | |||
+ | == call operator & == | ||
+ | <source lang="powershell"> | ||
+ | & $ENV:windir\System32\HostName.exe | ||
+ | </source> | ||
+ | |||
+ | <source lang="powershell"> | ||
+ | |||
</source> | </source> |
Revision as of 09:49, 4 June 2022
Pipes
$_ Variable
Get-Service | WHERE {$_.status -eq "Running"} | SELECT displayname
# “$_.” defines current element in the pipe
Get-Service | Sort-Object -property Status
$mydatge = Get-Date
$mydatge | Get-Member
Fucntion
Send-Greeting -name Claude
function Send-Greeting
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string] $Name
)
Process
{
Write-Host ("Hello " + $Name + "!")
}
}
if
$currentDate = Get-Date
$messageOfTheDay = ""
if($currentDate.Day % 2 -ne 0)
{
$messageOfTheDay = "Today is an odd Day, "
}
else
{
$messageOfTheDay = "Today all should be even, "
}
switch (($currentDate.DayOfWeek))
{
"Monday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." }
"Tuesday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." }
"Wednesday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." }
"Thursday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." }
"Friday" { $messageOfTheDay = $messageOfTheDay + "that being said you should be working..." }
"Saturday" { $messageOfTheDay = $messageOfTheDay + "yay it's the Weekend!!!" }
"Sunday" { $messageOfTheDay = $messageOfTheDay + "yay it's the Weekend!!!" }
default { $messageOfTheDay = $messageOfTheDay + "This script only knows the 7 weekdays...." }
}
echo $messageOfTheDay
for, while, doWhile
for ($i = 0; $i -lt 10; $i++)
{
Write-Host $i
}
$i = 0
while ($i -lt 10)
{
Write-Host $i
$I++;
}
$i = 0
Do
{
Write-Host $i
$I++;
} while($i -lt 10)
array
$myarray = 'a', 'b', 'c'
$myarray
WHERE
Clear-Host #Clears the display
$GuyDir = "C:\Temp"
$FilesExe = gci $GuyDir -recurse #GCI: Get Child Items
$List = $FilesExe | ? {$_.extension -eq ".pem"}
$List | Sort-Object -unique | Format-Table name
Clear-Host #Clears the display
$GuyDir = "C:\Temp"
$FilesExe = gci $GuyDir -recurse #GCI: Get Child Items
$List = $FilesExe | WHERE {$_.extension -eq ".pem"}
$List | Sort-Object -unique | Format-Table name
Clear-Host #Clears the display
$GuyDir = "C:\Temp"
$FilesExe = gci $GuyDir -recurse #GCI: Get Child Items
$List = $FilesExe | Where-Object {$_.extension -eq ".pem"}
$List | Sort-Object -unique | Format-Table name
call operator &
& $ENV:windir\System32\HostName.exe