Skip to content

Instantly share code, notes, and snippets.

View bielawb's full-sized avatar

Bartek Bielawski bielawb

View GitHub Profile
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 / 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.
$Common = @{
Path = 'SomeTables.xhtml'
Namespace = @{
d = 'http://www.w3.org/1999/xhtml'
}
}
Select-Xml -XPath '//d:table[d:tr[@class]]' @Common |
ForEach-Object { $_.Node } | Format-Table -AutoSize
$caseInsensitive = @'
//node[
translate(@Name, "FIRST", "first") = 'first'
]
'@
Select-Xml -XPath $caseInsensitive -Path .\Test.xml |
ForEach-Object { $_.Node.Name }
$almostRegex = @'
@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 / New-ModuleClassInstance.ps1
Created December 30, 2016 07:27
Work-around for the fact that `using module` is not very flexible.
param (
[string]$Path,
[string]$ClassName
)
$scriptBody = @'
using module {0}
'@ -f $Path
$script = [scriptblock]::Create($scriptBody)
@bielawb
bielawb / Get-ModuleDefinedClass.ps1
Last active December 30, 2016 08:33
Just an attempt to get info about classes defined by a given module.
[CmdletBinding()]
[OutputType([hashtable])]
param (
[string]$Path,
[string]$ClassName = '*'
)
$moduleBase = Split-Path -Path $Path -Parent
Write-Verbose "Module name - $fileName"