Skip to content

Commit d6a1df3

Browse files
committed
Working with PowerShell Objects
1 parent 5e2493a commit d6a1df3

1 file changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
Working with PowerShell Objects
2+
====
3+
When cmdlets are executed in PowerShell, the output is an Object, as opposed to only returning text.
4+
This provides the ability to store information as properties.
5+
As a result, handling large amounts of data and getting only specific properties is a trivial task.
6+
7+
As a simple example, the following function retrieves information about storage Devices on a Linux or MacOS operating system platform.
8+
This is accomplished by parsing the output of an existing command, *parted -l* in administrative context, and creating an object from the raw text by using the *New-Object* cmdlet.
9+
10+
```PowerShell
11+
Function Get-DiskInfo {
12+
$disks = sudo parted -l | Select-String "Disk /dev/sd*" -context 1,0
13+
$diskinfo = @()
14+
foreach ($disk in $disks) {
15+
$diskline1 = $disk.ToString().Split("`n")[0].ToString().Replace(' Model: ','')
16+
$diskline2 = $disk.ToString().Split("`n")[1].ToString().Replace('> Disk ','')
17+
$i = New-Object psobject -Property @{'Friendly Name' = $diskline1; Device=$diskline2.Split(': ')[0]; 'Total Size'=$diskline2.Split(':')[1]}
18+
$diskinfo += $i
19+
}
20+
$diskinfo
21+
}
22+
```
23+
24+
Execute the function and store the results as a variable.
25+
Now retrieve the value of the variable.
26+
The results are formatted as a table with the default view.
27+
28+
*Note: in this example, the disks are virtual disks in a Microsoft Azure virtual machine.*
29+
30+
```PowerShell
31+
PS /home/psuser> $d = Get-DiskInfo
32+
[sudo] password for psuser:
33+
PS /home/psuser> $d
34+
35+
Friendly Name Total Size Device
36+
------------- ---------- ------
37+
Msft Virtual Disk (scsi) 31.5GB /dev/sda
38+
Msft Virtual Disk (scsi) 145GB /dev/sdb
39+
40+
```
41+
42+
Passing the variable down the pipeline to *Get-Member* reveals available methods and properties.
43+
This is because the value of *d$* is not just text output.
44+
The value is actually an array of .Net objects with methods and properties.
45+
The properties include Device, Friendly Name, and Total Size.
46+
47+
```PowerShell
48+
PS /home/psuser> $d | Get-Member
49+
50+
51+
TypeName: System.Management.Automation.PSCustomObject
52+
53+
Name MemberType Definition
54+
---- ---------- ----------
55+
Equals Method bool Equals(System.Object obj)
56+
GetHashCode Method int GetHashCode()
57+
GetType Method type GetType()
58+
ToString Method string ToString()
59+
Device NoteProperty string Device=/dev/sda
60+
Friendly Name NoteProperty string Friendly Name=Msft Virtual Disk (scsi)
61+
Total Size NoteProperty string Total Size= 31.5GB
62+
```
63+
64+
To confirm, we can call the GetType() method interactively from the console.
65+
66+
```PowerShell
67+
PS /home/psuser> $d.GetType()
68+
69+
IsPublic IsSerial Name BaseType
70+
-------- -------- ---- --------
71+
True True Object[] System.Array
72+
```
73+
74+
To index in to the array and return only specific objects, use the square brackets.
75+
76+
```PowerShell
77+
PS /home/psuser> $d[0]
78+
79+
Friendly Name Total Size Device
80+
------------- ---------- ------
81+
Msft Virtual Disk (scsi) 31.5GB /dev/sda
82+
83+
PS /home/psuser> $d[0].GetType()
84+
85+
IsPublic IsSerial Name BaseType
86+
-------- -------- ---- --------
87+
True False PSCustomObject System.Object
88+
```
89+
90+
To return a specific property, the property name can be called interactively from the console.
91+
92+
```PowerShell
93+
PS /home/psuser> $d.Device
94+
/dev/sda
95+
/dev/sdb
96+
```
97+
98+
To output a view of the information other than default, such as a view with only specific properties selected, pass the value to the *Select-Object* cmdlet.
99+
100+
```PowerShell
101+
PS /home/psuser> $d | Select-Object Device, 'Total Size'
102+
103+
Device Total Size
104+
------ ----------
105+
/dev/sda 31.5GB
106+
/dev/sdb 145GB
107+
```
108+
109+
Finally, the example below demonstrates use of the *ForEach-Object* cmdlet to iterate through the array and manipulate the value of a specific property of each object.
110+
In this case the Total Size property, which was given in Gigabytes, is changed to Megabytes.
111+
Alternatively, index in to a position in the array as shown below in the third example.
112+
113+
```PowerShell
114+
PS /home/psuser> $d | ForEach-Object 'Total Size'
115+
31.5GB
116+
145GB
117+
118+
PS /home/psuser> $d | ForEach-Object {$_.'Total Size' / 1MB}
119+
32256
120+
148480
121+
122+
PS /home/psuser> $d[1].'Total Size' / 1MB
123+
148480
124+
```

0 commit comments

Comments
 (0)