PS C:\> $currentDir = (Get-Item .) PS C:\> $currentDir | Add-Member -MemberType ScriptProperty ` >> -Name FileCount -Value { (dir $this).Count } >> PS C:\> $currentDir.FileCount 43
PS C:\PowerShell\> $dirs = (Get-Item .),(Get-Item ..) PS C:\PowerShell\> $dirs | Add-Member -MemberType ScriptProperty ` >> -Name FileCount -Value {(dir $this).Count} >> PS C:\PowerShell\> $dirs | select Name,FileCount Name FileCount C:\PowerShell\ 5 C:\ 15
function Define-Class($constructor) { $class = New-Object PSObject $class | Add-Member NoteProperty Constructor $constructor $class | Add-Member ScriptMethod Create { $instance = New-Object PSObject $constructorBlock = $this.Constructor $this = $instance & $constructorBlock return $instance } return $class }
. .\Class-SupportLib.ps1 $Person = Define-Class{ Write-Host "Creating a Person Instance" } $p = $Person.Create()
PS C:\PowerShell> .\Person-Define.ps1 Creating a Person Instance PS C:\PowerShell>
function Add-Filed($Name,$Value) { $this | Add-Member NoteProperty $Name $Value }
. .\Class-SupportLib.ps1 $Person = Define-Class { Write-Host "Creating a Person instance" Add-Field "FirstName" "LiMing" } $p = $Person.Create() $p.FirstName
PS C:\PowerShell> .\Person-NoteProperty.ps1 Creating a Person instance LiMing
function Add-Property($Name, $Getter, $Setter) { $this | Add-Member ScriptProperty $Name $Getter $Setter } function Add-Method($Name, $Body) { $this | Add-Member ScriptMethod $Name $Body }
. .\Class-SupportLib.ps1 $Person = Define-Class { Write-Host "Creating a Person instance" Add-Field "FirstName" "John" Add-Field "LastName" "Smith" Add-Property "FullName" ` { "$($this.FirstName) $($this.LastName)" } ` { $value = $args[0] $words = $value.Split() $this.FirstName = $words[0] $this.LastName = $words[1] } Add-Method "Greet" { Write-Host "Hello there. I am $($this.FullName)." } } $p = $Person.Create() Write-Host "FullName:$($p.FullName)" Write-Host "Changing LastName to Wang" $p.LastName = "Wang" Write-Host "FullName:$($p.FullName)" Write-Host "Changing FullName to XiaoPang" $p.FullName = "XiaoPang" $p.Greet()
PS C:\PowerShell> .\Person-All.ps1 Creating a Person instance FullName:John Smith Changing LastName to Wang FullName:John Wang Changing FullName to XiaoPang Hello there. I am XiaoPang .
function Define-Class($constructor) { $class = New-Object PSObject $class | Add-Member NoteProperty Constructor $constructor $class | Add-Member ScriptMethod Create { $instance = New-Object PSObject $constructorBlock = $this.Constructor $this = $instance & $constructorBlock return $instance } return $class } function Add-Field($Name, $Value) { $this | Add-Member NoteProperty $Name $Value } function Add-Property($Name, $Getter, $Setter) { $this | Add-Member ScriptProperty $Name $Getter $Setter } function Add-Method($Name, $Body) { $this | Add-Member ScriptMethod $Name $Body }
<Type> <Name>System.Diagnostics.ProcessModule</Name> <Members> <ScriptProperty> <Name>Size</Name> <GetScriptBlock>$this.ModuleMemorySize / 1024</GetScriptBlock> </ScriptProperty> <ScriptProperty> <Name>Company</Name> <GetScriptBlock>$this.FileVersionInfo.CompanyName</GetScriptBlock> </ScriptProperty> <ScriptProperty> <Name>FileVersion</Name> <GetScriptBlock>$this.FileVersionInfo.FileVersion</GetScriptBlock> </ScriptProperty> <ScriptProperty> <Name>ProductVersion</Name> <GetScriptBlock>$this.FileVersionInfo.ProductVersion</GetScriptBlock> </ScriptProperty> <ScriptProperty> <Name>Description</Name> <GetScriptBlock>$this.FileVersionInfo.FileDescription</GetScriptBlock> </ScriptProperty> <ScriptProperty> <Name>Product</Name> <GetScriptBlock>$this.FileVersionInfo.ProductName</GetScriptBlock> </ScriptProperty> </Members> </Type>
PS C:\PowerShell> dir *.ps1 Directory: C:\PowerShell Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2009-4-20 21:21 622 Class-SupportLib.ps1 -a--- 2009-3-30 21:37 369 Data-Traps.ps1 -a--- 2009-4-12 21:31 131 DotNetProcesses.ps1 -a--- 2009-4-1 1:15 1674 Get-CertificateFiles.ps1 -a--- 2009-4-20 11:15 925 Help-SearchWeb.ps1 -a--- 2009-4-20 21:46 677 Person-All.ps1 -a--- 2009-4-20 19:53 121 Person-Define.ps1 -a--- 2009-4-20 21:12 165 Person-NoteProperty.ps1 -a--- 2009-4-20 2:09 245 Search-Examples.ps1
Directory: C:\PowerShell Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2009-4-20 21:21 622 Class-SupportLib.ps1 -a--- 2009-3-30 21:37 369 Data-Traps.ps1 -a--- 2009-4-12 21:31 131 DotNetProcesses.ps1 -a--- 2009-4-1 1:15 1674 Get-CertificateFiles.ps1 -a--- 2009-4-20 11:15 925 Help-SearchWeb.ps1 -a--- 2009-4-20 21:46 677 Person-All.ps1 -a--- 2009-4-20 19:53 121 Person-Define.ps1 -a--- 2009-4-20 21:12 165 Person-NoteProperty.ps1 -a--- 2009-4-20 2:09 245 Search-Examples.ps1
PS C:\PowerShell> dir *.ps1 | Format-Table Directory: C:\PowerShell Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2009-4-20 21:21 622 Class-SupportLib.ps1 -a--- 2009-3-30 21:37 369 Data-Traps.ps1 -a--- 2009-4-12 21:31 131 DotNetProcesses.ps1 -a--- 2009-4-1 1:15 1674 Get-CertificateFiles.ps1 -a--- 2009-4-20 11:15 925 Help-SearchWeb.ps1 -a--- 2009-4-20 21:46 677 Person-All.ps1 -a--- 2009-4-20 19:53 121 Person-Define.ps1 -a--- 2009-4-20 21:12 165 Person-NoteProperty.ps1 -a--- 2009-4-20 2:09 245 Search-Examples.ps1 PS C:\PowerShell> dir *.ps1 | Format-List Directory: C:\PowerShell Name : Class-SupportLib.ps1 Length : 622 CreationTime : 2009-4-20 19:34:57 LastWriteTime : 2009-4-20 21:21:33 LastAccessTime : 2009-4-20 21:45:11 …… Name : Data-Traps.ps1 Length : 369 …… Name : DotNetProcesses.ps1 Length : 131 CreationTime : 2009-4-12 21:30:28 LastWriteTime : 2009-4-12 21:31:00 LastAccessTime : 2009-4-12 21:32:56 …… Name : Help-SearchWeb.ps1 Length : 925 CreationTime : 2009-4-20 11:12:09 LastWriteTime : 2009-4-20 11:15:12 . Name : Person-Define.ps1 Length : 121 CreationTime : 2009-4-20 19:50:09 LastWriteTime : 2009-4-20 19:53:37 LastAccessTime : 2009-4-20 19:53:49 VersionInfo : File: C:\PowerShell\Person-Define.ps1 InternalName: OriginalFilename: FileVersion: FileDescription: Product: ProductVersion: Debug: False Patched: False PreRelease: False PrivateBuild: False SpecialBuild: False Language: ……
PS C:\PowerShell> dir *.ps1 | Format-Wide Directory: C:\PowerShell Class-SupportLib.ps1 Data-Traps.ps1 DotNetProcesses.ps1 Get-CertificateFiles.ps1 Help-SearchWeb.ps1 Person-All.ps1 Person-Define.ps1 Person-NoteProperty.ps1 Search-Examples.ps1
PS C:\PowerShell> dir .\Class-SupportLib.ps1 | Format-Custom class FileInfo { LastWriteTime = class DateTime { Date = class DateTime { Date = class DateTime { Date = class DateTime { Date = class DateTime { Date = 2009-4-20 0:00:00 ...... Length = 622 Name = Class-SupportLib.ps1 }
PS C:\PowerShell> dir $PSHOME\*.format.ps1xml Directory: C:\WINDOWS\system32\WindowsPowerShell\v1.0 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2008-12-15 5:00 22258 Certificate.Format.ps1xml -a--- 2008-12-15 5:00 23932 Diagnostics.format.ps1xml -a--- 2008-12-15 5:00 73218 DotNetTypes.Format.ps1xml -a--- 2008-12-15 5:00 19761 FileSystem.Format.ps1xml -a--- 2008-12-15 5:00 256244 Help.Format.ps1xml -a--- 2008-12-15 5:00 81821 PowerShellCore.format.ps1xml -a--- 2008-12-15 5:00 13516 PowerShellTrace.format.ps1xml -a--- 2008-12-15 5:00 15040 Registry.format.ps1xml -a--- 2008-12-15 5:00 19418 WSMan.format.ps1xml
<Configuration> <ViewDefinitions> <View> <Name>FileSecurity</Name> <ViewSelectedBy> <SelectionSetName>FileSystemTypes</SelectionSetName> </ViewSelectedBy> <GroupBy> <PropertyName>PSParentPath</PropertyName> <CustomControlName>FileSystemTypes-GroupingFormat </CustomControlName> </GroupBy> <TableControl> <TableHeaders> <TableColumnHeader> <Label>Name</Label> <Width>20</Width> <Alignment>left</Alignment> </TableColumnHeader> <TableColumnHeader> <Label>Owner</Label> <Width>12</Width> <Alignment>left</Alignment> </TableColumnHeader> <TableColumnHeader> <Label>AccessRules</Label> <Alignment>left</Alignment> </TableColumnHeader> </TableHeaders>
PS C:\PowerShell\Chapter14> Update-FormatData – PrependPath .\FileSecurity.format.ps1xml PS C:\PowerShell\Chapter14> dir person* | Format-Table -view FileSecurity Name Owner AccessRules ---- ----- ----------- Person-All.ps1 NULL-NOTEBOO BUILTIN\Administrators Allow FullControl K\Administra NT AUTHORITY\SYSTEM Allow FullControl tor FUHJ-NOTEBOOK\Administrator Allow FullControl BUILTIN\Users Allow ReadAndExecute, Synchronize Person-Define.ps1 NULL-NOTEBOO BUILTIN\Administrators Allow FullControl K\Administra NT AUTHORITY\SYSTEM Allow FullControl tor NULL-NOTEBOOK\Administrator Allow FullControl BUILTIN\Users Allow ReadAndExecute, Synchronize Person-NoteProperty. NULL-NOTEBOO BUILTIN\Administrators Allow FullControl ps1 K\Administra NT AUTHORITY\SYSTEM Allow FullControl tor NULL-NOTEBOOK\Administrator Allow FullControl BUILTIN\Users Allow ReadAndExecute, Synchronize
出处:http://fuhj02.blog.51cto.com
版权:本文版权归作者和51cto共有
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
个人网站: http://txj.shell.tor.hu/