Skip to content

Instantly share code, notes, and snippets.

View spy86's full-sized avatar
🎯
Focusing

Maciej Michalski spy86

🎯
Focusing
View GitHub Profile
@spy86
spy86 / GetEmailAddrrFromAD.ps1
Created January 21, 2019 20:53
Get Email addresses from members of an AD Group
Get-ADGroup "Group Name" | Get-ADGroupMember -Recursive | Get-ADUser -Properties * | Select SamAccountName,GivenName,sn,Mail
@spy86
spy86 / FTP.ps1
Created January 21, 2019 20:54
Script to FTP
#search latest files
$dir = "C:\Users\Admin\Documents"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
#we specify the directory where all files that we want to upload
$Dir="C:/Users/Admin/Documents/"+ $latest.name
#ftp server
$ftp = "ftp://ftp.someaddress.com/dir/"
$user = "login"
@spy86
spy86 / GetADUserLastLogonTime.psm1
Last active January 21, 2019 21:00
Get Active Directory user account last logged on time
#Check if the account exists.
If($Results.Count -eq 0)
{
Write-Warning "The SamAccountName '$UserName' cannot find. Please make sure that it exists."
}
Else
{
Foreach($Result in $Results)
{
$DistinguishedName = $Result.Properties.Item("DistinguishedName")
@spy86
spy86 / SlackWebhook.ps1
Created January 21, 2019 21:10
PowerShell push message to Slack incoming webhook.
Set-StrictMode -Version Latest
$payload = @{
"channel" = "#my-channel"
"icon_emoji" = ":bomb:"
"text" = "This is my message. Hello there!"
"username" = "Mr. Robot"
}
Invoke-WebRequest `
@spy86
spy86 / MultiDBRestore.ps1
Created January 21, 2019 21:13
Automate multiple database SQL Server restores to refresh environments
#requires -Version 3.0
#Assume sql server SMO is installed, https://msdn.microsoft.com/en-us/library/ms162189(v=sql.110).aspx
add-type -AssemblyName "Microsoft.SQLServer.Smo, Version=11.0.0.0, Culture=Neutral, PublicKeyToken=89845dcd8080cc91";
add-type -AssemblyName "Microsoft.SQLServer.SmoExtended, Version=11.0.0.0, Culture=Neutral, PublicKeyToken=89845dcd8080cc91";
[boolean] $debug =$true; # $true = print out the t-sql; $false = execute the restore
[string] $bkup_folder = 'c:\Backup\*' #the folder where the backup files are located. Can be a network share
[string] $sql_instance = 'TP_W520'; #this is the destination sql instance where the restore will occur. Change it to your own.
@spy86
spy86 / encode.ps1
Created January 21, 2019 21:14
Encode powershell script
$command = 'send-mailmessage -from -to -subject "$env:computername has been restarted" -body "The server $env:computername has recently restarted. If this was unexpected please log into the server and check the cause as soon as possible." -smtpserver '
$bytes = [system.text.encoding]::Unicode.GetBytes($command)
$encodedCommand = [convert]::tobase64string($bytes)
echo $encodedCommand > encodedtext.txt
notepad encodedtext.txt
@spy86
spy86 / SetupNIC.ps1
Created January 24, 2019 13:44
Setting up the NIC, Renaming the Computer, and Rebooting
# Define the Computer Name
$computerName = "dc1"
# Define the IPv4 Addressing
$IPv4Address = "10.10.100.25"
$IPv4Prefix = "24"
$IPv4GW = "10.10.100.1"
$IPv4DNS = "8.8.8.8"
# Get the Network Adapter's Prefix
@spy86
spy86 / InstallADDS.ps1
Created January 24, 2019 13:45
Install the ADDS Bits and Promote
$domainName = "contoso.com"
$netBIOSname = "CONTOSO"
$mode = "Win2012R2"
Install-WindowsFeature AD-Domain-Services -IncludeAllSubFeature -IncludeManagementTools
Import-Module ADDSDeployment
$forestProperties = @{
@spy86
spy86 / SetupDNSAndServices.ps1
Created January 24, 2019 13:45
DNS, Sites & Services, and Time Keeping
# Define DNS and Sites & Services Settings
$IPv4netID = "10.10.100.0/24"
$siteName = "LAB"
$location = "New Lab City"
# Define Authoritative Internet Time Servers
$timePeerList = "0.us.pool.ntp.org 1.us.pool.ntp.org"
# Add DNS Reverse Lookup Zones
Add-DNSServerPrimaryZone -NetworkID $IPv4netID -ReplicationScope 'Forest' -DynamicUpdate 'Secure'
$baseDN = "DC=contoso,DC=com"
$resourcesDN = "OU=Resources," + $baseDN
New-ADOrganizationalUnit "Resources" -path $baseDN
New-ADOrganizationalUnit "Admin Users" -path $resourcesDN
New-ADOrganizationalUnit "Groups Security" -path $resourcesDN
New-ADOrganizationalUnit "Service Accounts" -path $resourcesDN
New-ADOrganizationalUnit "Workstations" -path $resourcesDN
New-ADOrganizationalUnit "Servers" -path $resourcesDN
New-ADOrganizationalUnit "Users" -path $resourcesDN