Skip to content

Instantly share code, notes, and snippets.

View Agazoth's full-sized avatar

Axel B. Andersen Agazoth

View GitHub Profile
@Agazoth
Agazoth / Get-LocalMonitorInfo.ps1
Last active January 14, 2018 14:20
Iron Scripter 2018 Puzzle 1
$Monitors = Get-CimInstance -Namespace root\wmi -ClassName WmiMonitorID
$Computer = Get-CimInstance -Class Win32_ComputerSystem
$Bios = Get-CimInstance -ClassName Win32_Bios
foreach ($Monitor in $Monitors){
$PSObject = [PSCustomObject]@{
ComputerName = $Computer.Name
ComputerType = $Computer.model
ComputerSerial = $Bios.SerialNumber
MonitorSerial = [string]::join('',$monitor.SerialNumberID.Where{$_ -ne 0})
MonitorType = [string]::join('',$monitor.UserFriendlyName.Where{$_ -ne 0})
@Agazoth
Agazoth / Get-WickedOSandDiskObject.ps1
Last active January 21, 2018 19:38
Gets information about Operating System and Disks - A solution to Puzzle 2 in Iron Scripter 2018 Perquel 2
function Get-WickedOSandDiskObject {
param ($ComputerName=$env:COMPUTERNAME)
$CimSession = New-CimSession -ComputerName $ComputerName
$ComputerObject = Get-CimInstance Win32_OperatingSystem -CimSession $CimSession | Select-Object *,@{n='Disks';e={Get-CimInstance Win32_LogicalDisk -CimSession $CimSession | Select-Object *,@{n='PercentageUsed';e={"{0:P}" –f $(($_.size-$_.Freespace)/$_.size)}}}}
$ComputerObject.psobject.TypeNames.Insert(0, "WickedOSandDiskObject")
$ComputerObject
}
@Agazoth
Agazoth / Get-Feed.ps1
Last active January 28, 2018 07:27
Gets a newsfeed and lets the user select a specific feed to display in either browser or console
function Get-Feed {
[CmdletBinding()]
param ($Feeduri = 'https://powershell.org/feed/')
$c = 0
$Feeds = Invoke-RestMethod -uri $Feeduri
$FeedObjects = foreach ($Feed in $Feeds){
[PSCustomObject]@{
'No.' = ++$c
Title = $Feed.title
"Publication date" = $Feed.pubDate
@Agazoth
Agazoth / LegacyCommandObject.psm1
Created February 4, 2018 08:42
Gets PSCustomObjects from legacy commands. netstat and arp is allowed so far. Runs on Powershell 5.1 and 6.0
function Get-LegacyCommandObject {
[CmdletBinding()]
param ([ValidatePattern("^(netstat|arp)")]
[string]$CommandLine)
$Result = iex $CommandLine.split(';',2)[0]
$Headers = $Result -match '^\w+(`n`r)*'
foreach ($Line in $Result) {
if ($Line -match '^$'){$Collect = $False;$Properties=$Null;continue}
if ($Headers -contains $Line){$ObjHash=@{}; if ($Line -match ':'){$Parts = $Line.split(':',2).trim();$ObjHash.add($PArts[0],$Parts[1])};$Collect = $true;$CollectParametersHeader=$true;continue}
@Agazoth
Agazoth / Get-CounterReport.ps1
Created February 11, 2018 10:49
Gets counters from specified computer and displays them as PSCustomObject, HTML page or exproted to clixml. Verbose displays steps - Processor time takes a bit longer :-)
function Get-CounterReport {
[CmdletBinding()]
param ($ComputerName=$env:COMPUTERNAME,
[system.io.fileinfo]$ExportCliXML,
[switch]$OutHTML,
[switch]$Quiet)
$OutputObject = [ordered]@{CollectionDate = $(Get-Date);ComputerName = $ComputerName}
$Parameters = @{ComputerName = $ComputerName}
Write-Verbose "Fetching Processor Time on $ComputerName"
$Parameters["Counter"] = "\\{0}\Processor(_Total)\% Processor Time" -f $ComputerName
@Agazoth
Agazoth / Get-UpTime.ps1
Last active February 18, 2018 15:36
Gets the uptime from Windows and Linux machines. A possible solution for Iron Scripter Prequel 2018 Puzzle 6
function Get-UpTime {
[CmdletBinding()]
param ([parameter(ValueFromPipeline)][string]$ComputerName,[PSCredential]$Credential,$Authentication)
$Parameters = @{
ScriptBlock = [scriptblock]::Create('if($env:OS -match "Windows"){$strBoot = $((systeminfo | find "System Boot Time") -replace "^.+:\s+|,")} else {$strBoot = (who -b) -replace "^\D+"}; Get-Date $strBoot')
}
if (!$ComputerName){$ComputerName = hostname}
if ($Credential){$Parameters.Add('Credential',$Credential);$Parameters.Add('ComputerName',$ComputerName)}
if ($Authentication){$Parameters.Add('Authentication',$Authentication)}
$BootDate = icm @Parameters
class ComputerInfo {
[string]$ComputerName
[string]$BIOSManufacturer
[string]$BIOSVersion
[string]$Domain
[int]$Processors
[int]$Cores
[int]$TotalPhysicalMemoryGB
[string]$OSName
[string]$OSArchitecture
$File = New-Item -ItemType File -Path c:\SpecialFolder\SpecialFile.txt -Force
$Bill = New-LocalUser "Bill Bennsson" -Password (ConvertTo-SecureString -String BillAdmin -AsPlainText -Force)
$Andy = New-LocalUser "Andy Pandien" -Password (ConvertTo-SecureString -String AndyUser -AsPlainText -Force)
$Access = [System.Security.AccessControl.FileSystemAccessRule]::new($Bill.SID,"Modify","Allow"),[System.Security.AccessControl.FileSystemAccessRule]::new($Andy.SID,"Read","Allow")
$NewACL=[System.Security.AccessControl.DirectorySecurity]::new()
$NewACL.SetSecurityDescriptorSddlForm('D:')
$NewACL.SetAccessRuleProtection($True, $True)
$Access | ForEach-Object {$NewACL.AddAccessRule($_)}
Set-Acl $File.FullName $NewACL
@Agazoth
Agazoth / CleanupTempFolder.ps1
Last active March 11, 2018 20:10
A possible solution to Iron Scripter 2018 prequel. Create a scheduled job that cleans up the $env:temp folder
$ScheduledJobName = "CleanupTempFolder"
$ScriptBlock = [ScriptBlock]::Create(@'
[System.IO.FileInfo]$LogFile = "C:\Logs\CleanTempLog.log"
if (!$(Test-Path $LogFile.DirectoryName)){New-Item -ItemType Directory -Path $LogFile.DirectoryName}
function Get-TempFileObject {
param ($Timing)
$TempContent = Get-ChildItem $env:temp -Recurse -Force
[PSCustomObject]@{
Filecount = ($TempContent | Where-Object {!$_.psiscontainer}).Count
Foldercount=($TempContent | Where-Object {$_.psiscontainer}).Count
@Agazoth
Agazoth / EventLogFix.ps1
Last active March 18, 2018 07:52
Iron Scripter Prequel 2018 Puzzle 10
if(([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator')){(Get-WinEvent -ListLog * -ea 0 -Force).where{$_.RecordCount -gt 0}.foreach{if ($_.filesize/$_.MaximumSizeInBytes -gt 0.8){if ($_.LogMode -ne "Circular"){ $_.LogMode="Circular"}; $_.MaximumSizeInBytes=([math]::ceiling($_.MaximumSizeInBytes*1.1/64)*64);$_.SaveChanges(); 'EventLog {0} increased to {1} bytes. Logmode: {2}' -f $_.LogName,([math]::ceiling($_.MaximumSizeInBytes*1.1/64)*64),$_.LogMode | out-file $PSScriptRoot\EventlogFix.log -Append};$_} | select LogName ,RecordCount | sort RecordCount -Descending} else {Write-Warning "You must be an admin to run this script"}