-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerShellBeginner.txt
More file actions
113 lines (78 loc) · 3.86 KB
/
Copy pathPowerShellBeginner.txt
File metadata and controls
113 lines (78 loc) · 3.86 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
PowerShell
md - create a folder
Get-Service
Get Commands get information for you.
Start-Service
Stop-Service
New-LocalUser
Set-LocalUser
Parameters belong to a CMDLet
Get-Service -Name 'spooler'
CMDLet Parameter Value
Active Directory
Get-ADUser -Identity carles(ad user)
Set-ADUser -Identity carles -Surname Gonzalez
Set-ADUser -Identity carles -Surname "Gonzalez Leon"
Installing and Configuring a Domain and DC
First we install the Active Directory Services Role to install a new Forest
Install-WindowsFeature -Name AD-Domain-Services -IncludeManagementTools
Create the new forest
Install-ADDSForest -DomainName DomänenName -SafeModeAdministratorPassword (Convertto-SecureString -AsPlainText "Passwort" -Force)
Help
Get-Help -Name Get-Service -Full
Update-Help
Modules
They are files in your computer which contain CMDLets.
Modules are collections of cmdlets that are stored in the path %WINDIR%\System32\WindowsPowerShell\.
Get-Command retrieves all CMDLets and their associated modules
You can use this find out what modules are installed in your system
Get-Command -Module Hyper-V -> gets all the commands for Hyper-V
Get-Command -Module Hyper-V -Verb Get -> Get all the commands with GET-
This example finds a module in the repository and installs the module.
Find-Module -Name PowerShellGet | Install-Module
Install a module by name
Install-Module -Name PowerShellGet
PowerShell Pipeline
Get-Service -Name 'spooler' | Stop-Service
Get-LocalUser -name carles | Set-LocalUser -Description 'Admin of the Computer'
Get-Service | Out-File -FilePath c:\PowerShellTest\Services.txt
Format Data
Get-Service | Format-List -Property name
Get-Service | Format-List -Property name, status | Out-File -FilePath c:\PSTest\FormattedList.txt
Get-Service | Format-Table -Property...
Properties are attributes
Methods are like actions (Start, stop, turnonlights,etc)
Blueprints are like classes, and from classes, objects are made.
A car has a method: Start()
2 Objects are different because the value of their properties and implementation of their methods, but both are still objects.
Properties and methods of an object
Get-Service | Get-Member
Get-LocalUser | Get-Member
How to get results we need on a property of an object?
Get-Service | Where-Object {$_.status -eq 'running'} | Out-File -FilePath 'C:\PSTest\report.txt'
This command gets all the services with status Running and put them into a file.
Get-LocalUser | Where-Object {$_.enabled -eq 'True'} | Set-LocalUser -Description 'Admin'
This Command will change the description of all the enabled users to Admin.
How do we select specific information?
Get-Service | Select-Object -First 5
Get-Service | Select-Object -Last 5
Get-Service | Select-Object -Property name, status -First 5 |Out-File -FilePath 'C:\PSTest\Services.txt
Sort in alphabetical order
Get-Service | Sort-Object -Property 'Status'
Get-EventLog -LogName system -newest 5 | Sort-Object instanceid
Get-Service | Where-Object {$_.status -eq 'stopped'} | Sort-Object -Property displayname | Select-Object -First 5 | Format-List
Create HTML Report
Get-EventLog -LogName 'System' -Newest 5 | Select-Object -Property InstaceID, Message | ConvertTo-HTML -Head "<style>BODY{backgroun-color:yellow;}TABLE {border-width: 1px;bo..... | Out-File -FilePath 'C:\PSTest\Report.html'
Using Variables to add the HTML Code:
$Head = "<style>BODY{backgroun-color:yellow;}TABLE {border-width: 1px;bo.....
Get-EventLog -LogName 'System' -Newest 5 | Select-Object -Property InstaceID, Message | ConvertTo-HTML -Head $Head | Out-File -FilePath 'C:\PSTest\Report.html'
Scripting
Continues in each script example in VSCode.
Aliases
Functions
NSG - Do it at the NIC or Subnet Level.
Enable ps remoting on a Windows VM (method #1)
Enable-AzVMPSRemoting -Name web1 -ResourceGroupName WebServers -Protocol https -OsType Windows
$PSVersionTable - Gets the PS Version
Get-History - shows the last 10 commands
help about*