#requires -version 2.0 <# ----------------------------------------------------------------------------- Script: 13ScriptBlocks-v2.ps1 Version: 0.9 Author: Jeffery Hicks http://jdhitsolutions.com/blog http://twitter.com/JeffHicks http://www.ScriptingGeek.com Date: 4/13/2012 Keywords: Comments: Each script block is followed by a commented sample command "Those who forget to script are doomed to repeat their work." **************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** ----------------------------------------------------------------------------- #> #1 Get top problem source from the last 500 event log entries $topprob={Param($log="System") Get-EventLog -LogName $log -newest 500 -entrytype Error | Group Source -NoElement | Sort Count | Select -last 1} #&$topprob #2 Get folder usage by owner in MB $usage={Param($path=".") dir $path -recurse | Where {-Not $_.PSIsContainer} | Select Fullname,Length,@{N='Owner';E={($_ | Get-ACL).Owner}} | Group Owner | Sort Count -descending| Select Count,Name,@{N='SizeMB';E={(($_.Group | Measure length -sum).sum)/1MB}} } #&$usage #3 get empty event logs $emptylog={get-eventlog -list | where {$_.entries.count -eq 0}} #&$emptylog #4 Get OS Install date $install={Param($Computername=$env:computername) Get-WmiObject win32_operatingsystem -comp $computername | select CSName,Caption, @{N="Install";E={$_.ConvertToDateTime($_.InstallDate)}}} #&$install #5 Test if running Windows 8 $test8={Param($Computername=$env:computername) (Get-WmiObject win32_operatingsystem -comp $computername).Caption -match "Windows 8" } #&$test8 MyWin8 #6 Test if running PowerShell v3 $testPS3={Param($Computername=$env:computername) (test-wsman -ComputerName $computername).Productversion -match "Stack: 3.0"} #&$testPS3 MyWin8 #7 Get code snippets from help examples $excode={Param($command="get-service") (get-help $command).examples.example | select code} #&$excode get-process #8 Count by 13 $countup={Param($count=5) $x=0; For ($i=0;$i -lt $Count; $i++) {$x+=13;$x}} #&$countup 13 #9 get a 13 character random password $randpass={ Param($length=13) $chars=[char[]](33..126) ; -join ($chars | get-random -count $length)} #&$randpass #10 Test if profile scripts exist $profileCheck={ $profile | Select @{N="Type";E={"AllUsersAllHosts"}},@{N="Path";E={$_.AllUsersAllHosts}},@{N="Exists";E={Test-Path $_.AllUsersAllHosts}} $profile | Select @{N="Type";E={"AllUsersCurrentHost"}},@{N="Path";E={$_.AllUsersCurrentHost}},@{N="Exists";E={Test-Path $_.AllUsersCurrentHost}} $profile | Select @{N="Type";E={"CurrentUsersAllHosts"}},@{N="Path";E={$_.CurrentUserAllHosts}},@{N="Exists";E={Test-Path $_.CurrentUserAllHosts}} $profile | Select @{N="Type";E={"CurrentUserCurrentHost"}},@{N="Path";E={$_.CurrentUserCurrentHost}},@{N="Exists";E={Test-Path $_.CurrentUserCurrentHost}} } #&$profileCheck | format-list #11 get default printer $defaultPrint={get-wmiobject win32_printer -filter "Default='True'"} #&$defaultPrint #12 get timezone $tz={Param($computername=$env:computername) Get-WmiObject win32_timezone -computername $computername | Select @{N="Computername";E={$_.__SERVER}},Description} #&$tz #13 find expired certificates $expired={dir cert:\ -recurse | where {$_.NotAfter -AND $_.NotAfter -lt (Get-date)}} #&$expired #Invoke-command $expired -comp server01