Skip to content

Instantly share code, notes, and snippets.

@AfroThundr3007730
Last active March 2, 2024 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AfroThundr3007730/8d149e7066cd29196e0c7b965cfb912b to your computer and use it in GitHub Desktop.
Save AfroThundr3007730/8d149e7066cd29196e0c7b965cfb912b to your computer and use it in GitHub Desktop.
Collection of useful powershell snippets
#Usage filesplit.ps1 <filename> <# of lines>
Param(
[parameter(mandatory=$true,position=0)][string]$filename,
[parameter(position=1)][string]$linesperFile = 100000
)
$sw = new-object System.Diagnostics.Stopwatch
$sw.Start()
$rootName = (join-path ([io.fileinfo]$filename).directoryname ([io.fileinfo]$filename).basename) + '_'
$ext = ([io.fileinfo]$filename).extension
$header = get-content $filename -first 1
$filecount = 0
$reader = $null
try {
$reader = [io.file]::OpenText($filename)
try {
$filecount++
$linecount = 0
"Creating file number $filecount"
$writer = [io.file]::CreateText("{0}{1}{2}" -f ($rootName,$filecount.ToString("000"),$ext))
while($reader.EndOfStream -ne $true) {
"Reading $linesperFile lines"
if($filecount -gt 1){$writer.WriteLine($header)}
while( ($linecount -lt $linesperFile) -and ($reader.EndOfStream -ne $true)) {
$writer.WriteLine($reader.ReadLine());
$linecount++
}
if($reader.EndOfStream -ne $true) {
"Closing file"
$writer.Dispose();
$filecount++
$linecount = 0
"Creating file number $filecount"
$writer = [io.file]::CreateText("{0}{1}{2}" -f ($rootName,$filecount.ToString("000"),$ext))
}
}
} finally {
$writer.Dispose();
}
} finally {
$reader.Dispose();
}
$sw.Stop()
Write-Host "Split complete in " $sw.Elapsed.TotalSeconds "seconds"
# Sends PrintScreen every 60 seconds
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
While ($true) {
[System.Windows.Forms.SendKeys]::SendWait("{PRTSC}")
Start-Sleep -s 30
}
# Used to publish new user photo to GAL
param (
[string]$sid = $(throw "-sid is required"),
[string]$photo = $(throw "-photo is required")
)
$ErrorActionPreference = "stop"
$root = [adsi]''
$searcher = New-Object System.DirectoryServices.DirectorySearcher($root)
$searcher.Filter = "(mail=$($sid)*)"
$users = $searcher.FindAll()
if($users.Count -eq 0){
throw "No matching user found"
} elseif($users.Count -eq 1){
$user = $users[0].GetDirectoryEntry()
} else{
$i = 1
foreach($user in $users){
Write-Host "$i : $($user.Path)"
$i += 1
}
$pick = Read-Host "Which user?"
$user = $users[$pick - 1].GetDirectoryEntry()
}
$file = [byte[]](Get-Content $photo -Encoding Byte)
$user.Properties["thumbnailPhoto"].Clear()
$user.Properties["thumbnailPhoto"].Add($file)
$user.commitChanges()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment