Skip to content

Instantly share code, notes, and snippets.

View bielawb's full-sized avatar

Bartek Bielawski bielawb

View GitHub Profile
@bielawb
bielawb / ADFunctions.psm1
Created January 31, 2016 21:07
Simple function to search Active Directory
function Search-AD {
<#
.Synopsis
Function to search Active Directory using specified filter.
.Description
Function uses selected LDAP filter to search Active Directory.
It doesn't have any external dependencies and is using ADSISearcher class.
User can specify attributes that should be retrieved and SearchRoot.
<#
Assumptions:
-- uses v3 module manifest (RootModule rather than ModuleToProcess)
-- RootModule contains name of the file only
-- Certain pattern can be used to check only functions that follow naming convention (here: *-Cim*)
Also:
-- Actual tests include more actions related to manifest contents, thus "context" block is used
-- Actual test is testing multiple modules/ manifests, thus $manifest in the real scenario is part of foreach ($manifest in ls) {}
#>
netsh dhcp server scope 192.168.7.0 show clients 1 | % {
if ($_ -match '(?n)(?<ip>\S+).*?(?<mac>([0-9a-f]{2}-){5}[0-9a-f]{2}).*?(?<name>\b\w+\.monad\.net)') {
$Matches.Remove(0)
$Matches.mac = $Matches.mac -replace '-', ':'
[PSCustomOBject]$Matches
}
}
@bielawb
bielawb / XmlDemo.ps1
Created April 23, 2016 17:38
Demo code from PSConfEU presentation "XML is your friend"
#region Basics...
# implicit foreach on Adapted Type System object.
$xml = [xml]@'
<node attribute="attributeValue">
<CaseSensitive>First</CaseSensitive>
<caseSensitive>second</caseSensitive>
</node>
'@
$PSDefaultParameterValues.'Select-Xml:Xml' = $xml
@bielawb
bielawb / ComputerName.Completer.ps1
Created September 28, 2016 18:31
Using TabExpansionPlusPlus to tab-complete CName -> HostName
Register-ArgumentCompleter -ParameterName ComputerName -ScriptBlock {
param(
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameter
)
$record = [System.Net.Dns]::Resolve($wordToComplete)
if ($record) {
@bielawb
bielawb / ShowVM.psm1
Created October 31, 2016 22:48
Module that uses WMF5 completion for vmconnect wrapper.
class DopelnijVMke : System.Management.Automation.IArgumentCompleter {
[System.Collections.Generic.IEnumerable[System.Management.Automation.CompletionResult]] CompleteArgument(
[string]$commandName,
[string]$parameterName,
[string]$wordToComplete,
[System.Management.Automation.Language.CommandAst]$commandAst,
[System.Collections.IDictionary]$fakeBoundParameters
) {
return [System.Management.Automation.CompletionResult[]]@(
foreach ($vm in Get-VM -Name "$wordToComplete*") {
@bielawb
bielawb / JIRA.psm1
Created October 31, 2016 23:09
Module that uses WMF5 completion for skeleton New-JiraIssue
function Get-LoginCompletion {
param(
[string]$commandName,
[string]$parameterName,
[string]$wordToComplete,
[System.Management.Automation.Language.CommandAst]$commandAst,
[System.Collections.IDictionary]$fakeBoundParameters
)
([ADSISearcher]"(&(objectClass=user)(sAMAccountName=$wordToComplete*))").FindAll() |
@bielawb
bielawb / FindADObject.psm1
Created October 31, 2016 23:21
Module that shows how hard to read can ArgumentCompleter become when query is complex.
function Find-ADObject {
<#
.Synopsis
Function to search Active Directory using specified LDAP filter.
.Description
Function uses selected LDAP filter to search Active Directory.
It doesn't have any external dependencies and is using ADSISearcher class.
@bielawb
bielawb / ValidateSet.Tests.ps1
Created November 1, 2016 19:10
Pester test for ValidateSet
function foo {
param (
[ValidateSet(
'this',
'that'
)]
[String]$bar
)
}
@bielawb
bielawb / CompleteShould.ps1
Created November 29, 2016 18:44
TabExpansion for Should
Register-ArgumentCompleter -Native -CommandName Should -ScriptBlock {
param ($WordToComplete, $CommandAst)
$options = @(
'Be'
'BeExactly'
'BeLike'
'BeGreaterThan'
'BeLessThan'
'Contain'
'Match'