Skip to content

Instantly share code, notes, and snippets.

"`nWalking the XML tree`n"
$WeirdThingy = [xml](Get-Content "test.audit")
$RemoveNodes = @()
# Walk the nodes, match text and add nodes to $RemoveNodes array
$WeirdThingy.check_type.ChildNodes | ForEach-Object {
if ($_.InnerText -match '8.1.1.1') {
$RemoveNodes += $_
<!DOCTYPE html>
<html>
<!-- NOTE: This code started with a copy of index.html from https://bl.ocks.org/mbostock/4063570 which is: -->
<!-- LICENSE: GPL version 3 https://opensource.org/licenses/GPL-3.0 -->
<head>
<style>
<meta charset="utf-8">
.node circle {
fill: #fff;
// Selected hand-copied text from https://www.youtube.com/watch?v=FjhRkfAuU7I#t=2035
// Article at https://databricks.gitbooks.io/databricks-spark-reference-applications/content/twitter_classifier/index.html
// references this video, but the provided code is different than video.
// The video does a bunch of stuff to obtain and process the tweets, but
// what "val texts = " becomes is simply an array of strings, so I just
// made a text file with one log entry per line (I'm trying to classify logs)
// and did something like:
val texts = sc.textFile("input.log")
# In reply to comment https://www.reddit.com/r/PowerShell/comments/47wc3x/ive_been_asked_to_come_up_with_35_questions_to/d0g8bgd
$Hash = @{
"a" = 5
"b" = 4
"c" = 3
"d" = 2
"e" = 1
}
# In reply to https://www.reddit.com/r/PowerShell/comments/46bwtd/help_with_counting_in_csv_files/
# Tested against test csvs generated by the other script in this gist, and it worked without modification!
param (
$Path = ".",
$OutFile = "C:\temp\countlines.csv"
)
Get-ChildItem -Filter *.csv -Path $Path -Recurse |
# Refactored from https://www.reddit.com/r/PowerShell/comments/44zkff/simple_script_for_looking_through_text_files_for/
# Lots of comments explaining changes removed. Look at the revision history in Gist or the commit messages in git for explanations.
param(
[string] $LogDirectory = "C:\Projects\Patterns\Reports\Run01\logs",
[string] $ResultFileDate = ("Results_{0}.csv" -f (MyDate)),
[string] $ExtractedFilePath = "$($LogDirectory.TrimEnd('\'))\$($ResultFileDate)",
[Array] $patterns= @(
',vsftp ,Connection closed.'
1..5 | ForEach-Object {
$out2 = New-Object psobject
$out2 | add-member noteproperty Name "Hi"
$out2 | add-member noteproperty Username "There $_"
$out2 | Format-Table -AutoSize
}
# This just puts each array in a vertical column of a table.
# The rows are not related data unless the source data is keyed on array index
$Arr1 = @(1, 2, 3, 4, 5)
$Arr2 = @(1, 2, 3)
$Arr3 = @(1, 2, 3, 4, 5, 6)
$Arr4 = @(1, 2, 3, 4, 5, 6, 7)
$Arr5 = @(1, 2)
$Arr6 = @(1, 2, 3, 4)
# An illustration of what does and doesn't slow down Powershell and why
function Get-LotsOfData {
param (
$Rows = 10000
)
# $OneK is a 1024-character string that is just the alphabet repeating
$OneK = ( 0..1023 | ForEach-Object { [char](65 + $_ % 26) } ) -join ""
1..$Rows | ForEach-Object {
# Emit the equivalent of one row of a CSV with 5 columns
$data = @{}
Import-Csv namecity.csv |
ForEach-Object {
if (-not $data[$_.name]) { $data[$_.name] = @{} }
$data[$_.name]["city"] = $_.city
}
Import-Csv namestate.csv |
ForEach-Object {