-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDemo2.ps1
62 lines (45 loc) · 1.16 KB
/
Demo2.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
#requires -version 5.0
#define some properties
#add a constructor or two
Class MyFileObject {
#region Properties
[ValidateNotNullorEmpty()]
[string]$Path
[string]$Name
[string]$Extension
[string]$Directory
[int]$Size
[datetime]$Created
[datetime]$Modified
#endregion
#region Constructors
MyFileObject([string]$FilePath) {
If (Test-Path -Path $Filepath) {
$item = Get-Item -Path $Filepath
$this.path = $item.fullname
$this.Name = $item.Name
$this.Extension = $item.Extension.Substring(1)
$this.size = $item.Length
$this.Created = $item.CreationTime
$this.Modified = $item.LastWriteTime
$this.Directory = $item.Directory
}
else {
Write-Warning "Failed to find $filepath"
#don't create the object
Break
}
}
#endregion
}
Return
#walkthrough demo
cls
[myfileobject]::new.overloadDefinitions
$f = New-Object MyFileObject -ArgumentList .\Demo1.ps1
$f
$f | get-member
#test bad path
[MyFileObject]::New("x:\foo.txt")
#creating several objects
dir .\ -File | foreach { New-Object myfileobject $_.FullName}