-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-intro-powershell.ps1
67 lines (51 loc) · 2.18 KB
/
01-intro-powershell.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Windows + R -> run type: powershell
# type "ise" in PowerShell -> integrated scripting engine
# Enable Windows + i from system settings taskbar to run PowerShell
# Use vscode + PoewrShell extensions https://code.visualstudio.com/
# List all commands available in PowerShell
Get-Command #(gcm)
# Use Placeholder / Wildcard like "*""
gcm Get-Serv*
# Use properties of objects to filter with a dash "-"
Get-Command -Module Hyper-V
# Use alias and tab complete
Get-Alias gcm
# Get help for a specific command
Get-Help #(help)
# Get help and an example for specific command
Help get-service -examples #(help gcm –examples)
# Use pipe to put an output of a command as an input for the next
# Example of Get- | Do- | Out- methodology
Get-Service | Where-Object Status –eq “Stopped” | Set-Service $_ -Status Running | Out-File C:\scripts\services.txt
# Example of web
# Getting help
help Invoke-RestMethod
help Invoke-RestMethod examples
# Make use of variables
# Store URL in variable
$uri = 'https://jsonplaceholder.typicode.com'
# Invoke
Invoke-RestMethod -uri $uri -Method Get
Invoke-RestMethod -uri $uri/posts/1 -Method Get
Invoke-RestMethod -uri $uri/posts/1 -Method Get -out JSON
cat ./JSON
# Use windwos API
# Computersystem
Get-CimInstance Win32_Computersystem
# Get property name from an Win32_Computersytem object
$name = $(Get-CimInstance Win32_Computersystem).name
#Use remoting
# Test if computer can be invoked
Test-WsMan $name
# Invoke a command
Invoke-Command -ComputerName $name -ScriptBlock { ipconfig } -credential mawarnek
# Format output
Get-Service | Format-list # (fl)
Get-Service | Fromat-table # (ft)
Get-Command | More
Get-Command | Out-Gridview
# list commands used
Get-History
help history
# to Output
history | Out-File 'C:\Scripts\out.txt'