Skip to content

Instantly share code, notes, and snippets.

@macna
macna / openssl-powershell-certgen.ps1
Last active August 14, 2023 04:31
A PowerShell script for generating CSRs using OpenSSL
# Define where OpenSSL is installed
$openSSLDir = "C:\Program Files\OpenSSL\bin"
# Define the domain we're generating the CSR for
$commonName = Read-Host -Prompt "Provide the domain you're generating a certificate for"
# Define the default parameters on the certificate
$email = 'webteam@contoso.com'
$country = 'GB'
$state = 'Suffolk'
@macna
macna / apache-powershell-logrotate.ps1
Created January 14, 2019 13:40
A PowerShell script for rotating Apache HTTP Server logs
# Define where Apache writes the log files
$logFiles = "C:\path\to\logs"
# Define the location the log files should be archived to
$logArchive = "C:\path\to\archive"
# Define for how many days we should retain logs in the archive location
$logLimit = (Get-Date).AddDays(-90)
# Stop the Apache HTTP Server service, using a wildcard to find it by name
Get-ADGroupMember -Identity "Some Group" -Recursive | Get-ADUser -Properties * | Export-Csv -Path "C:\Temp\groupdump.csv" -NoTypeInformation
@macna
macna / PowerShell-Server-Reboot-Notification.ps1
Last active February 19, 2024 23:30
This PowerShell script sends an email notification when a server reboots. The script simply sends an email message, and is to be used on a scheduled task that is triggered at system startup.
# Get the server name
$hostName = hostname
# Define the email address to send notifications to
$toAddress = "someone@contoso.com"
# Send the notification
Send-MailMessage -To $toAddress -From "$hostName@contoso.com" -SmtpServer smtp.contoso.com -Subject "$hostName - Server Reboot" -Body "The server $hostName has rebooted."
@macna
macna / PowerShell-CodeSigning.ps1
Created July 7, 2015 13:35
PowerShell script for signing a PowerShell script. Requires that you have a certificate in your personal store that is suitable for code signing
$cert = @(gci cert:\currentuser\my
-codesigning)[0]
Set-AuthenticodeSignature myscript.ps1 $cert
@macna
macna / PowerShell-Azure-Proxy.ps1
Created June 8, 2015 14:14
PowerShell script for connecting to Microsoft Azure when behind a forward-proxy. Requires the Microsoft Azure PowerShell commandlets.
$webclient=New-Object System.Net.WebClient
$creds=Get-Credential
$webclient.Proxy.Credentials=$creds
$cred = Get-Credential
Add-AzureAccount -Credential $cred
@macna
macna / PowerShell-Windows-Server-Backup-Failure.ps1
Created April 27, 2015 09:38
PowerShell scripts for sending Windows Server Backup success/failure notifications. Using these scripts, you can create Windows scheduled tasks that are triggered on the event IDs of Windows Server Backup. For success notifications the task should be triggered on event ID '4'. For failure notifications the task should be triggered on event IDs '…
$hostname = hostname
Send-MailMessage -To admin@contoso.com -From "$hostname@contoso.com" -Priority High -SmtpServer smtp.contoso.com -Subject "$hostname - Backup Failure" -Body "A backup on $hostname has failed."
@macna
macna / PHP-Azure-Websites.user.ini
Created April 18, 2015 21:23
A user.ini file for enabling PHP logging on a website hosted in Microsoft Azure Websites. Needs to be placed at the root of your application (D:\home\site\wwwroot\).
display_errors=On
log_errors=On
error_log = "D:\home\site\wwwroot\<your_dir>\php-errors.log"
@macna
macna / File-Cleanup.bat
Created April 18, 2015 21:22
A Microsoft Windows batch file for deleting files in a folder based on their file extension and age (.txt files older than 30 days in this example).
forfiles.exe -p C:\<your>\<folder> -m *.txt -d -30 -c "cmd.exe /C del @path""
@macna
macna / MySQL-Backup.bat
Created April 18, 2015 21:19
A script for dumping a MySQL database to flat file, including a time/date stamp in the file name.
mysqldump <dbname> -uroot --skip-triggers > "D:\<your>\<folder>\<dbname>%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.sql"