Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/9650ddf0b4bd6a4a223e to your computer and use it in GitHub Desktop.
Save guitarrapc/9650ddf0b4bd6a4a223e to your computer and use it in GitHub Desktop.
PowerShell Class Samples
Class CopyItemClass
{
[string]$Path
[string]$Destination
[string]$Item
[PSObject] SetItem([string]$Path, [string]$Destination)
{
$result = copy-Item -Path $Path -Destination $Destination -PassThru
Write-Warning ("Item Copied from {0} to {1}" -f $Path, $Destination)
return $result
}
[System.IO.FileInfo]GetItem()
{
Write-Warning ("Get item in {0}" -f $Item)
return Get-ChildItem -Path $Item -File
}
[System.IO.FileInfo]GetPath()
{
Write-Warning ("Get item in {0}" -f $Path)
return Get-ChildItem -Path $Path -File
}
[System.IO.FileInfo]GetDestination()
{
Write-Warning ("Get item in {0}" -f $Destination)
return Get-ChildItem -Path $Destination -File
}
[bool] TestPath()
{
Write-Warning ("Test item in {0}" -f $Path)
return (Get-ChildItem -Path $Path -File | %{Test-Path -Path $_})
}
[bool] TestDestination()
{
Write-Warning ("Test item in {0}" -f $Destination)
return (Get-ChildItem -Path $Destination -File | %{Test-Path -Path $_})
}
}
# New
$hoge = [CopyItemClass]::New()
$hoge | Get-Member
<#
TypeName: CopyItemClass
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetDestination Method System.IO.FileInfo GetDestination()
GetHashCode Method int GetHashCode()
GetItem Method System.IO.FileInfo GetItem()
GetPath Method System.IO.FileInfo GetPath()
GetType Method type GetType()
SetItem Method psobject SetItem(string Path, string Destination)
TestDestination Method bool TestDestination()
TestPath Method bool TestPath()
ToString Method string ToString()
Destination Property string Destination {get;set;}
Item Property string Item {get;set;}
Path Property string Path {get;set;}
#>
# Initializing
$hoge.Path = "d:\hoge.dll"
$hoge.Destination = "d:\hogehoge.dll"
$hoge.Item = "d:\hoge.log"
# Set
$hoge.SetItem($hoge.Path, $hoge.Destination)
# Get
$hoge.GetPath()
$hoge.GetDestination()
$hoge.GetItem()
# Test
$hoge.TestPath()
$hoge.TestDestination()
# Enum
Enum hoge
{
Absent
Present
}
Write-Warning "Enum is here."
[hoge]::Absent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment