Author: freakling

  • Quick and dirty way to find primes

    2..5000 | ForEach-Object{ $current = $_ $f = @() 1..$Current | ForEach-Object{ $result = $current/$_ If($result.GetType().Name -eq “Int32”){ $f += $result } } if($f.count -le 2){ $current } }

  • Fibonacci sequence

    $1 = 0 $2 = 1 1..100 | ForEach-Object{ if($_%2 -eq 1){ $1 = $1+$2 $1 } Else{ $2 = $1+$2 $2 } }

  • Alcohol calculator

    Calculate the amount of pure alchohol in litre you consume per year. All forms needs to be filled in. [void][reflection.assembly]::LoadWithPartialName(“System.Windows.Forms”) $Form = New-Object ‘System.Windows.Forms.Form’ $Calculate = { [double]$Result = 0 [double]$beer = $BeerTextBox.Text [double]$wwine = $WhiteWineTextBox.Text [double]$rwine = $RedWineTextBox.Text [double]$spirit = $SpiritTextBox.Text $result = ($beer*0.5*0.05)+($wwine*0.7*0.08)+($rwine*0.7*0.13)+($spirit*0.7*0.4) $ResultBox.Text = $Result } $Form.Size = “300,300” $BeerLabel = New-Object…

  • Get startpage

    Invent startpage set in IE for XP and Windows7 clients. Needs a list of computers and collects the startpage in a CSV file. [cmdletbinding()] Param( $Computers, $output, $Progress, [Switch]$Loop ) $ErrorActionPreference = “SilentlyContinue” If(Test-Path -Path $Computers -PathType Leaf){ [Array]$Script:computers = Get-Content $Computers } $ComputerCount = $Computers.Count $Script:Database = @{} Function Get-Startpage{ Param( $output, $Progress )…

  • Access CSC

    this is used to quickly gain access to a users client side cache without ruining sync. Used for backup issues and other stuff. $comp = ‘computer1’ # Replace with computer $FQDN = ‘your.domain.com’ # Replace with FQDN $UserID = ‘user1’ # Replace with the useraccount to gain access Function ConvertTo‐Base64($string) { $bytes = [System.Text.Encoding]::Unicode.GetBytes($string); $encoded…

  • Add users as local admin on select computers

    This is used to add users as local admin on a select group of clients. Needs a input file specified or default to input.txt in same folder. It converts Administrator SID to friendlyname to account for things such as foreign characters. It uses WMI to invoke a client side script to be run under admin…

  • Custom progress bar

    Not that anyone wants or needs it, but here is a custom progress bar! It has two modes. Loop and step. And is called like below. PS> 0..99 | ForEach-Object{“Processing, current step $_”;Start-Sleep -Milliseconds 200;.\epicship.ps1 -step $_};”Installation complete!” .\epicship.ps1 -loop The Loop has more functions, such as a sun shining and getting obscured by the…

  • FileDialogs

    Function SaveFileDialog{ Param( [Parameter(Mandatory=$True)] $Filetype ) [Void][Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) $SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog $SaveFileDialog.Filter = “$Filetype files (*.$Filetype)|*.$Filetype|All files (*.*)|*.*” $status = $SaveFileDialog.ShowDialog() If($status -eq “Cancel”){$Return = $status} Else{$Return = $SaveFileDialog.FileName} $SaveFileDialog.Dispose() Return $Return } Function SelectFileDialog{ Param( [Parameter(Mandatory=$True)] $Filetype ) [Void][Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.Filter = “$Filetype files (*.$Filetype)|*.$Filetype|All files (*.*)|*.*” $status = $OpenFileDialog.ShowDialog() If($status…

  • The one-liner maker!

    I give you all, probably the most unused function on the planet. The “Make one-liner”! It’s probably not even close to “Complete” but it is easy to add exceptions on where to format. Function Main{ $ScriptString = Get-Content [enter powershell script path here] Make-Oneliner $ScriptString } Function Make-Oneliner{ Param([Array]$array) [string]$OneLiner = @() Foreach($Line1 in $array){…

  • SQL queries in powershell

    I was trying to query the SCCM database and sort the results with Powershell to get a full report on all the connections to a collection. To do this i needed to inject a SELECT query and retrieve an object from the database which proved to be a bit difficult. Below is how i resolved…

  • Beta testing and error handling

    Here is a simple snippet that you can run to output all exceptions in a script. $ErrorActionPreference = “SilentlyContinue” IWillProduceAnError #—————————————————————- # Debug function, dumps all errors in $Error variable to file #—————————————————————- Function Dump-Errors(){ Param( [Parameter(Position=0, Mandatory = $True)] [String]$Logfile ) $Error | Add-Content $Logfile $Error.Clear() } Dump-Errors -logfile “c:\temp\script.log” If you look at…

  • Office 2007 and .ost files.

    There is a slight issue with .ost files and roaming profiles in Outlook 2007. The issue appears when the user gets a corrupt profile, gets another generated but still has some registry keys mapped to the old corrupt profile. If the corrupt profile gets removed, or if another local mail account is created on the…

  • Windows forms, tips and trix – Buttons

    Ever written a GUI to your application? Ever think there are alot of unnecessary lines? Here is a smart way to add basic buttons without too many lines. function CreateButton{ param( $name, $text, $size, $location, $onclick, $Form ) $name = New-Object System.Windows.Forms.Button $name.Text = “$text” $name.Location = “$location” $name.Size = “$size” $name.add_Click($onclick) $Form.Controls.Add($name) } This requires…

  • wget for windows

    ever wanted to use wget in Powershell? This short function allows you to wget to local directory. function wget($urlpath){ $directory = (get-location).path $filename = $urlpath.Split(“/”)[-1] $file = $directory+”\”+$filename $webclient = New-Object System.Net.WebClient $webclient.DownloadFile($urlpath,$file) } Simply enter the function into your current powershell session and try it out. wget http://upload.wikimedia.org/wikipedia/en/f/f4/The_Scream.jpg If you want to permanently have access…

  • Powershell auto updater

    I wrote this short script to keep local scripts up to date with server versions. Makes it easier to handle scripts that are added with initial version through task sequence or other onetime distributions. The requirement is for the scripts to have the variable $CurrentVersion = <int/double> Example script, located on the central/repository: $CurrentVersion =…