Skip to content

Instantly share code, notes, and snippets.

View bielawb's full-sized avatar

Bartek Bielawski bielawb

View GitHub Profile
@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) {
$caseInsensitive = @'
//node[
translate(@Name, "FIRST", "first") = 'first'
]
'@
Select-Xml -XPath $caseInsensitive -Path .\Test.xml |
ForEach-Object { $_.Node.Name }
$almostRegex = @'
$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
@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.
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
<#
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) {}
#>
@bielawb
bielawb / Excel.psm1
Created March 13, 2015 04:31
Module to read Excel as if it was just XML. Needs work (e.g. some cleanup after data is read).
<#
List taken from SO answer:
http://stackoverflow.com/questions/4730152/what-indicates-an-office-open-xml-cell-contains-a-date-time-value
#>
$ExCellStyles = @{
0 = 'General'
1 = '0'
2 = '0.00'
3 = '#,##0'
@bielawb
bielawb / Nested-cfs.ps1
Created September 18, 2014 22:08
ConvertFrom-String - nested properties (improved).
$data = @'
=
Adam Nowacki
Domowy:123 456 789
Praca:999 999 999
[prywatny/adam@nowacki.pl]
[sluzbowy/AdamNowacki@firma.pl]
=
Anna Kowalska
Domowy:123 456 789
@bielawb
bielawb / Japanese.ps1
Last active August 29, 2015 14:06
Just a test code for ConvertFrom-String used with Japanese (well.. or so I think) ;)
$Template = @'
{Name*:霧嶋 董香}
{City:Redmond}, {State:WA}
{Name*:Łukasz Ćwikalski}
{City:ワルシャワ}, {State:PL}
'@
$Data = @'
霧嶋 董香