-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00_objects.ps1
More file actions
74 lines (51 loc) · 1.95 KB
/
00_objects.ps1
File metadata and controls
74 lines (51 loc) · 1.95 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
# In PowerShell - everything is an object
# an object is an instance of a class
# the class determins methods, properties, actions.
$car = "Toyota"
# look at properties of an object:
Select-Object -InputObject $car -Property *
$car | Select-Object *
$car | select *
$car.Length
# to discover all properties and methods that exist on a object - use get-member
Get-Member -InputObject $car
$car | Get-Member
$car | gm
# calling methods
get-member -InputObject $car -Name remove
$car.Remove(0,1)
$car.Remove(1,1)
$car.Remove(0,3)
$car.ToLower()
$car.ToUpper()
'this is rocket science'.Replace('rocket', 'rock')
$w = " 4 white space before and 4 after "
$w.trim()
# other object examples:
$f = Get-Item .\README.md
$f | select *
# --------------------------------------------------------------------------------------------------
# Service example
Get-Service -Name w32time
# First line: typename - type of object
Get-Service -Name w32time | Get-Member
# To find commands that accept that type of object as input:
Get-Command -ParameterType ServiceController
# Select all or specific properties
Get-Service -Name w32time | Select-Object -Property *
Get-Service -Name w32time | Select-Object -Property Status, Name, DisplayName, ServiceType
Get-Service -Name w32time | Select-Object -Property Status, DisplayName, Can*
# get methods for get-service
Get-Service -Name w32time | Get-Member -MemberType Method
# example to use a method
(Get-Service -Name w32time).Stop()
# better use cmdlets if one exists:
Get-Service -Name w32time | Start-Service -PassThru
# --------------------------------------------------------------------------------------------------
# process example
Get-Process -Name chrome | Get-member
Get-Command -ParameterType Process
Get-Process -Name chrome | Select-Object -Property s*
Get-Process -Name chrome | ConvertTo-Html | Out-File chrome-process.html
Get-Process | Sort-Object -Property ProcessName
Get-Process | Sort ProcessName -desc