Skip to content

Instantly share code, notes, and snippets.

@AspenForester
Created May 16, 2014 17:39
Show Gist options
  • Save AspenForester/d6245451f96b5538e9b7 to your computer and use it in GitHub Desktop.
Save AspenForester/d6245451f96b5538e9b7 to your computer and use it in GitHub Desktop.
2013 PowerShell Scripting Games
{$_.Creationtime -lt (Get-Date).AddDays(-90)} |
ForEach-Object {
Move-Item -Force -Recurse -Path $_.FullName -Destination $($_.FullName.Replace("C:\Application\Log","\\NASServer\Archives"))
}
# This one is a bit embarrassing. I really got the mechanics of an advanced function wrong,
# and did way more than I really needed to. This was a learning experience!
<#
.SYNOPSIS
Return system information for remote systems
.DESCRIPTION
Based on the input of a file containing a list of IP addresses, one address per line,
return the name of the computer, OS version, installed RAM, & Processor info (cores, sockets, or both)
Requires PowerShell 3.0
Expects a file called IPList.txt to exist at c:\ (as specified by the event Description) which
contains one IP address per line.
.EXAMPLE
Solve-Event2
.OUTPUT
System.Management.Automation.PSCustomObject
#>
Function Solve-Event2 {
BEGIN {
$InFile = "C:\IPList.txt"
if (!(test-path $InFile)){$InFile = split-path -Leaf $InFile}
}
PROCESS {
$Servers = Get-Content $InFile|
ForEach-Object {
if (($_.length) -gt 0) {
$CompSys = get-wmiobject win32_computersystem -computername $_
$OpSys = get-wmiobject win32_OperatingSystem -computername $_
$hash = [ordered]@{
ServerName=$CompSys.Caption
Version=$Opsys.Caption
InstalledRAMGB=($CompSys.TotalPhysicalMemory/1gb -as [int])
Sockets= $CompSys.NumberofProcessors
}
[pscustomobject]$hash
}
}
}
END {
$Servers
}
}
Solve-Event2
# Please don't kill puppies! The back ticks are avoidable!
Get-WmiObject -Class win32_logicalDisk -Filter "DriveType=3" -ComputerName . |
select @{Name="Drive";Expression={$_.DeviceId}}, `
@{Name="Size(GB)";Expression={"{0:N2}" -f $($_.Size / [math]::pow(1024,3))}}, `
@{Name="FreeSpace(MB)";Expression={"{0:N2}" -f $($_.FreeSpace / [math]::pow(1024,2))}} |
ConvertTo-Html -As Table `
-PostContent "<hr>$(Get-Date -Format G)" `
-PreContent "<h2>Local Fixed Disk Report</h2>" `
-Title "Disk Free Space Report" > c:\support\diskReport.html
# I was still learning, and hadn't figured out the "#Requires -version 3.0" yet
# Returns 20 random users from AD, displaying SamAccountName 'username', Dpeartment, Title, LastLogonDate (Date), PasswordLastSet (Date), Enabled (Bool), LockedOut (Bool)
# Requires PowerShell 3.0 to automatically import ActiveDirectory Module
Get-ADUser -filter * -Properties Department, Title, LastLogonDate, PasswordLastSet, Enabled, LockedOut |
Get-Random -Count 20 |
select @{Name="UserName";Expression = {$_.samaccountname}}, Department, Title, LastLogonDate, PasswordLastSet, Enabled, LockedOut |
ConvertTo-Html -as Table -Title "User Account Audit" -PreContent "<H2>User Account Audit</H2>" -PostContent "<HR> Retrieved: $(Get-Date -Format G)" |
Out-File -FilePath c:\support\audit.html
# Resource:
# http://stackoverflow.com/questions/115989/using-powershell-to-access-iis-logs
Get-ChildItem C:\Reporting\LogFiles -Recurse -Include "*.log" |
Foreach-Object {
Import-Csv -Delimiter " " -Path $_.FullName -Header "date","time","s-ip","cs-method","cs-uri-stem","cs-uri-query","s-port","cs-username","c-ip","cs(User-Agent)","sc-status","sc-substatus","sc-win32-status","time-taken" |
Where-Object { !($_.Date.StartsWith('#')) }
}|
Select-Object -Unique c-ip
# I give! With travelling to TechEd, I never had an opportunity to test this... at all.
# If I got it all it wrong, so be it. I had fun, and learned a lot from my first scripting games.
# Thank you all for your comments on each of the events!
#requires -version 3.0
#requires -module dhcpserver
$DomainCred = New-Object System.Management.Automation.PSCredential ("admin", $(ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force))
$LocalCred = New-Object System.Management.Automation.PSCredential ("administrator", $(ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force))
$i = 1
Get-Content C:\Mac.txt |
Foreach-Object {
$Lease = Get-DHCPServerv4Lease -Computername DHCP1 -ScopeID 10.0.0.0 -ClientID $_
# With the Ver 3.0 functionality, the add-computer can do the rename and join in one shot...
# Or so says the help...
If (test-connection $lease.ipaddress) {
#no point trying to rename the computer if you can’t reach it.
add-computer -DomainName “Company.local” `
-computername “$lease.ipaddress” `
-localCredential $LocalCred `
-newName “Server$i” `
-Credential $DomainCred `
-restart -force `
-confirm:$false
}
$i ++
}
@AspenForester
Copy link
Author

AspenForester commented May 16, 2014

I learned a ton over the course of the 2013 PowerShell Scripting Games!

The last event, I typed up at the last minute and submitted, without ever actually trying it out. For event 6, my and The Scripting Wife's entries were nearly identical! I was lucky enough to have received a Third place award for that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment