Last active
September 13, 2018 13:35
-
-
Save luksi1/fd8e3a59a3399aa9326453e18e5d1fd5 to your computer and use it in GitHub Desktop.
PowerShell helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Custom validator for one element | |
class ValidateElementAttribute : System.Management.Automation.ValidateArgumentsAttribute | |
{ | |
[void] Validate([object]$arguments, [System.Management.Automation.EngineIntrinsics]$engineIntrinsics) | |
{ | |
# Validation happens here | |
if ($arguments -ne "foo") { | |
throw "you should have chosen foo" | |
} | |
} | |
} | |
# Custom validator for multiple elements | |
class ValidateElementsAttribute : System.Management.Automation.ValidateEnumeratedArgumentsAttribute | |
{ | |
[void] ValidateElement([object]$arguments) | |
{ | |
# Check each element here | |
if ($arguments -notmatch "^f") { | |
throw "nope... not f*. maybe try 'foo' or 'flute' or 'fsck'" | |
} | |
} | |
} | |
function Do-Something | |
{ | |
[cmdletbinding()] | |
param( | |
[ValidateElement()] | |
$A, | |
[ValidateElements()] | |
$B | |
) | |
if ($A) { | |
write-host $A | |
} | |
if ($B) { | |
write-host $B | |
} | |
} | |
# Throws an exception | |
Do-Something -A poo | |
# Does not throw exception | |
Do-Something -A foo | |
# Throws exception | |
Do-Something -B @("foo","food","pooo") | |
# Does not throw exception | |
Do-Something -B @("foo","food","flute") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cast active directory names to computername | |
get-adcomputer -filter * | | |
select-object -Property @{N='ComputerName'; E={$_.Name}} | | |
% { get-eventlog -LogName system -EntryType error, warning -after $(get-date).AddDays(-2) -ComputerName $_.computername | | |
sort source | | |
select-object InstanceId, MachineName, source } | | |
Export-Clixml (get-date -format filedate)-system.xml | |
# set active directory names to an array | |
$computers = $(get-adcomputer -filter *).Name | |
get-eventlog -LogName system -EntryType error, warning -after $(get-date).AddDays(-2) -ComputerName $computers | | |
sort source | | |
select-object InstanceId, MachineName, source | |
# invoke command | |
$computers = $(get-adcomputer -filter *).Name | |
invoke-command {get-eventlog -LogName system -EntryType error, warning -after $(get-date).AddDays(-2)} | |
-ComputerName $computers | | |
sort source | | |
select-object InstanceId, MachineName, source |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$json = '{ | |
"keystores": { | |
"/usr/local/cert/foo/cert1.jks:signing1": { | |
"days_remaining": 629, | |
"issuer": "C=SE O=Inera AB CN=SITHS Type 3 CA v1 PP", | |
"owner": "C=SE L=Vanersborg O=Vastra Gotalands Lans Landsting CN=footest.vgregion.se serialNumber=12345" | |
}, | |
"/usr/local/cert/foo/cert1.jks:signing2": { | |
"days_remaining": 629, | |
"issuer": "C=SE O=Inera AB CN=SITHS Type 3 CA v1 PP", | |
"owner": "C=SE L=Vanersborg O=Vastra Gotalands Lans Landsting CN=footest.vgregion.se serialNumber=12345" | |
}, | |
"/usr/local/cert/foo/cert1.jks:encryption1": { | |
"days_remaining": 594, | |
"issuer": "C=SE L=Stockholm O=Foo AB OU=Foo CN=Foo Bar Root", | |
"owner": "serialNumber=12345 C=SE O=Foo AB OU=Foo CN=encryption" | |
}, | |
"/usr/local/cert/foo.domain.org_siths_type3_ca_v1_pp_auth.p12:1": { | |
"days_remaining": 531, | |
"issuer": "C=SE O=Inera AB CN=SITHS Type 3 CA v1 PP", | |
"owner": "C=SE L=Vanersborg O=Vastra Gotalands Lans Landsting CN=footest.vgregion.sjunet.org serialNumber=1234" | |
}, | |
"/usr/local/cert/footest.domain.com_siths_type3_ca_v1_pp_auth.p12:1": { | |
"days_remaining": 537, | |
"issuer": "C=SE O=Inera AB CN=SITHS Type 3 CA v1 PP", | |
"owner": "C=SE L=Vanersborg O=Vastra Gotalands Lans Landsting CN=footest.vgregion.se serialNumber=1234" | |
} | |
} | |
}' | |
$o = $json | ConvertFrom-Json | |
foreach ($info in $o.keystores.PSObject.Properties) { | |
[pscustomobject]@{ | |
"Name"=$info.Name; | |
"Days_Remaining"=$info.Value.days_remaining; | |
"Issuer"=$info.Value.issuer; | |
"Owner"=$info.Value.owner | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get-wmi with computers from active directory | |
invoke-command {gwmi -Class win32_logicaldisk} -ComputerName $(get-adcomputer -filter *).name | |
# same with get-ciminstance | |
invoke-command { Get-CimClass -ClassName win32_logicaldisk } -ComputerName $(Get-ADComputer -filter *).Name | |
# using cim sessions | |
$s = New-CimSession -ComputerName $(Get-ADComputer -filter *).name | |
Get-CimSession | gcim Win32_LogicalDisk | |
# using cim sessions one-liner | |
$s = New-CimSession -computername $(get-adcomputer -filter *).name; | |
Get-CimSession | | |
gcim Win32_LogicalDisk; | |
Get-CimSession | Remove-CimSession |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# using cim sessions | |
$s = New-CimSession -ComputerName $(Get-ADComputer -filter *).name | |
Get-CimSession | gcim Win32_LogicalDisk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum category { | |
suspense | |
detective | |
horror | |
romance | |
comedy | |
biography | |
} | |
class Book { | |
[string] $title | |
[string] $author | |
[datetime] $published | |
[float] $rating | |
[string] $owner | |
[category] $category | |
Book ($title, $author, $published, $owner, $category) { | |
$this.title = $title | |
$this.author = $author | |
$this.published = $published | |
$this.owner = $owner | |
$this.category = $category | |
} | |
sell($owner) { | |
$this.owner = $owner | |
} | |
} | |
class GoodreadsBook { | |
[string] $title | |
[string] $owner | |
[category] $category | |
[book] getInformation ($title, $category, $owner, $goodreadsKey) { | |
[string] $encodedTitle = [System.Web.HttpUtility]::UrlEncode($title) | |
[string] $url = "https://www.goodreads.com/search.xml?key=${goodreadsKey}&q=${encodedTitle}" | |
$request = Invoke-RestMethod $url | |
[int]$year = $request.GoodreadsResponse.search.results.work[0].original_publication_year."#text" | |
[string]$month = "{0:D2}" -f [int]$request.GoodreadsResponse.search.results.work[0].original_publication_month."#text" | |
[string]$day = "{0:D2}" -f [int]$request.GoodreadsResponse.search.results.work[0].original_publication_day."#text" | |
$author = $request.GoodreadsResponse.search.results.work[0].best_book.author.name | |
$goodreadsTitle = $request.GoodreadsResponse.search.results.work[0].best_book.title | |
#$rating = $request.GoodreadsResponse.search.results.work[0].average_rating | |
#$this.publishedDate = [DateTime]::ParseExact("${year}-${month}-${day} 00:00:00,000", "yyyy-MM-dd HH:mm:ss,fff", [System.Globalization.CultureInfo]::InvariantCulture) | |
$published = [DateTime]::ParseExact("${year}-${month}-${day}", "yyyy-MM-dd",[System.Globalization.CultureInfo]::InvariantCulture) | |
return [Book]::new($goodreadsTitle, $author, $published, $owner, $category) | |
} | |
} | |
function sellBook () { | |
[cmdletbinding()] | |
param ( | |
[book] $book, | |
[string] $owner | |
) | |
begin {} | |
process { | |
$book.sell($owner) | |
$book.getBook() | |
} | |
} | |
function getBook() { | |
[cmdletbinding()] | |
param ( | |
[string[]] $title, | |
[string[]] $category, | |
[string[]] $owner, | |
[string[]] $goodreadsKey | |
) | |
process { | |
$goodReadsbook = [GoodreadsBook]::new() | |
$book = $goodReadsbook.getInformation($title, $category, $owner, $goodreadsKey) | |
$book | |
} | |
} | |
$mybook = getBook -title "To Kill a Mockingbird" -category "Suspense" -owner "Luke" | |
$mybook | |
$mybook.sell("Fred") | |
$mybook | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test { | |
[cmdletbinding()] | |
param( | |
[parameter(mandatory)] | |
[validatescript({test-computer $_})] | |
[string] $computername | |
) | |
write-host $computername | |
} | |
function test-computer { | |
[cmdletbinding()] | |
param( | |
[parameter(mandatory,valuefrompipeline,position=0)] | |
[string] $computername | |
) | |
$computers = get-adcomputer -filter * | Where-object name -eq $computername | |
$computers.name -contains $computername | |
} | |
test -computername "WIN10" | |
test -computername "FIN10" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment