Skip to content

Instantly share code, notes, and snippets.

View Dan1el42's full-sized avatar

Daniel Krebs Dan1el42

View GitHub Profile
$path = "C:\Testing\File.xml"
[Xml]$servicefactoryconfig = Get-Content -Path $path -Raw
$old = $servicefactoryconfig.SelectSingleNode("/factory/map/add[@key='Audit']")
$parent = $old.ParentNode
[void] $parent.RemoveChild($old)
$newNode = [Xml] @'
$csvFileName = 'C:\My\Input.csv'
# Example 3 - Using the -match operator with a more complex regular expression to extract the IP address
Import-Csv -Path $csvFileName |
ForEach-Object {
if ($PSItem.URL -match '(?<IPAddress>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})') {
$Matches.IPAddress
}
}
@Dan1el42
Dan1el42 / Translate-DISM-Feature-Names.ps1
Created March 30, 2017 10:26
Translate DISM feature names into PowerShell compatible ones.
# Translate DISM feature names to PowerShell
$translationTable = @'
NetFx4ServerFeatures
NetFx4
NetFx4Extended-ASPNET45
IIS-WebServerRole
IIS-WebServer
IIS-CommonHttpFeatures
IIS-Security
var script = System.IO.File.ReadAllText(textBoxScriptToRun.Text);
var match = Regex.Match(script, "function (?<FunctionName>[a-z]+-[a-z0-9]+)",
RegexOptions.IgnoreCase);
var functionName = match.Groups["FunctionName"];
Hashtable functionParams = new Hashtable();
foreach (var line in textBoxParameters.Lines)
{
var tokens = line.Split("=".ToCharArray(), 2);
$logFileContent = Get-Content -Path C:\My\Forum\mail.log -Raw
$regExMatches = [regex]::Matches($logFileContent, '(?<timestamp>[0-9/: ]+)\tUser (?<email>[a-z0-9@.]+) logged on')
$items = $regExMatches | ForEach-Object {
[PSCustomObject]@{
timestamp = [DateTime]$_.Groups['timestamp'].value
email = $_.Groups['email'].Value
}
}
$items | Group-Object -Property email | Select-Object -Property Name, Count | Sort-Object -Property Count -Descending
$osInfo = Get-WmiObject -Class Win32_OperatingSystem -Property Name
# Using the PowerShell -split operator to split the string into an array
# and output the first array item containing the operating system name
($osInfo.Name -split '\|')[0]
# Using the Split method of the .NET String class to split the string into an array
# and output the first array item containing the operating system name
$osInfo.Name.Split('|')[0]
Clear-Host
$file = Get-Content -Path 'X' # List of Computers
$fileName = 'X' + (Get-Date -Format dd.MM.yyyy) + '.HTML'
foreach ($node in $file) {
$selectObjectProperties = @(
'SystemName'
'DeviceID'
$text = '[email protected]'
# Example 1: Unnamed capturing group
if ($text -match '@(.+)$') {
# Output the complete match
$Matches[0] # @example.com
# Output only the value of the capturing group
$Matches[1] # example.com
}
@Dan1el42
Dan1el42 / gist:e0bc84883902e0fb4fbf8a5bf8dfcdd7
Created September 1, 2016 07:37
PowerShell: Disable SSL certificate validation
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$crlf = [System.Environment]::NewLine
[string] $TNS = 'LINKED_SERVER'
$sqlfiles = "c:\temp\script1.sql", "c:\temp\script2.sql"
$connection=New-Object DATA.OracleClient.OracleConnection("Data Source=$TNS;User Id=TEST;Password=XXXXXXXX")
foreach ($sqlfile in $sqlfiles){
$FileLines = Get-Content $sqlfile
$query = [string]::Join($crlf,$FileLines)
#log start time