ssss






For reasons, all my VMWare services are set to manual startup. I like it this way.
I wanted an easy way to start all the services so naturally, a PowerShell script was required. However the services can’t be started by PowerShell without elevated privileges, and I usually work in a non-elevated ISE. So this version of the script self-elevates, saving me precious seconds.
# Start All VMware services V2 # WARNING - THIS SCRIPT WILL ATTEMPT TO ELEVATE ITSELF # Check if the current user role is in the local computer administrator role # get-service -displayname 'VMware*' | %{Stop-Service -name $_.Name} If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(` [Security.Principal.WindowsBuiltInRole] "Administrator")){ # Not running as administrator # Create a new process object that starts PowerShell $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell"; # Specify the current script path and name as a parameter $script:MyInvocation.MyCommand.Path $newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'" # Indicate that the process should be elevated $newProcess.Verb = "runas"; # Start the new process [System.Diagnostics.Process]::Start($newProcess); } Else { # Elevated Code - will only run if IS Administrator # Set some colors so its clear what is going on $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)" $Host.UI.RawUI.BackgroundColor = "DarkRed" clear-host # start all the VMware services $s = get-service -displayname 'VMware*' # i should really list the services in the right startup order write-host $s foreach ($svc in $s){ write-host $svc.name 'is' $svc.Status if($svc.Status -ne "Running") { write-host 'Starting: ' $svc.Name #Stop-Service $svc.name #set-service $svc.Name -StartupType Manual Start-Service $svc.Name } Else{ # Do something else if you want } } #start "C:\Program Files (x86)\VMware\VMware Workstation\vmware.exe" } # Re-present the services again get-service -displayname 'VMware*' Write-Host "Sleeping 60" start-sleep -seconds 60
This web scraper is a little odd because it had to run within a very constrained environment.
Essentially it attempts to load URLs consisting of a prefix and sequential numerical suffix in the format;
https://url/browse/JIRA-number
then prints the webpage to PDF via the default printer, and downloads a zip file from the URL found in each page’s ‘aszip’ element.
# JIRA Archive v3.0 # For archiving JIRA entries and zip attachments # Its a quick and dirty web scraper # Not the most ideal way I would usually do this but this one needed # to work with several corporate SOE and security restrictions # SYNOPSIS ------------------------------------------------- # Needed a way to download and archive all JIRA items and attachments. # The JIRA setup does not allow bulk extraction or archiving. # The solution has to operate with only the same access as a non-admin client user. # CHALLENGES ----------------------------------------------- # Corporate SOE prevents IE from auto downloading without user prompting, Chrome however allows it. # Invoke-WebRequest doesnt work (powershell is only v2, possibly other causes of this) # Resulting archive needs to be seen to maintain integrity of source without tamper (as much as possible) # (Hence print to PDF rather than save as HTML) # Urgency. # Set the target JIRA number range $from_JIRA = 1 $to_JIRA = 300 # Throtle the time between downloads $throtle = 5 # in seconds # -------------- Functions # TestFileLock function TestFileLock { param ( [parameter(Mandatory=$true)][string]$Path ) $oFile = New-Object System.IO.FileInfo $Path if ((Test-Path -Path $Path) -eq $false) { return $false } try { $oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None) if ($oStream) { $oStream.Close() } $false } catch { # file is locked by a process. return $true } } # ------------------------------------------------- #Main Loop for ($current_JIRA=$from_JIRA; $current_JIRA -lt $to_JIRA; $current_JIRA ++){ Write-Host ('Processing JIRA-' + $current_JIRA) $website = ("https://url/browse/JIRA-" + $current_JIRA) $website #using ie com object because invoke-webrequest doesnt work $ie=new-object -com internetexplorer.application $ie.Visible=$true $ie.navigate2($website) #Give the website a chance to load (note: might replace this with a page load return code check) start-sleep -seconds 10 #Print to default printer which is set to PDFCreator $ie.execWB(6,2) #Scrape the 'download all attachments' link url from the element id $aszipurl = $ie.Document.getElementById('aszip').href write-host "attachments zip url: " $aszipurl if ($aszipurl -ne $null){ #if zip file exists pass it to chrome because cant get IE to download without prompting Start-Process "chrome.exe" $aszipurl} # wait for pdf to exist and then rename it $testpath = 'C:\Users\user\Downloads\current.pdf' while ((Test-Path $testpath) -ne $true){ write-host "Waiting for pdf to exist..." Start-Sleep -seconds 5 } $newpath = 'C:\Users\user\Downloads\Named\JIRA_' + $current_JIRA + '.pdf' #make sure the pdf has finished writing first while ((TestFileLock($testpath)) -eq $true){ write-host "Waiting for pdf to be complete ..." Start-Sleep -seconds 5 } #now move the file write-host "Moving PDF" Move-Item -path $testpath -Destination $newpath #Close the ie instance $ie.quit() write-host "Sleeping " $throtle start-sleep -Seconds $throtle }
A very good friend said to me once, and i’m paraphrasing for brevity,
“…ideas are worthless, they are everywhere, they are free. It’s the ability to get them done that has any value.”
I’ve met plenty of people with good ideas. People who can drive a good idea home to delivery are, however, of much rarer occurrence.
Some simple PowerShell snippets I find useful.
Not worthy of their own pages, but useful nevertheless.
# Get-Process | Select-Object id, starttime, name | Sort-Object starttime #
# $searchFiles = Get-ChildItem F:\files\ -Filter *.ext -Recurse $searchFiles | Select-String -Pattern "find this string" #
This script does a few things. I will break it apart into separate posts that deal with each bit, but for now here is the whole lot.
The purpose of this script is to;
There are some functions in this one not specifically related to the objectives, but for reasons I won’t go into here, I generally try to keep all scripts as self-contained as possible. I will try to break this out when I get time.
#region Includes # ====================== Add some required bits and import some DLLs # System drawing and windows forms [Reflection.Assembly]::LoadWithPartialName("System.Drawing") [Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) # Some mousey things $signature=@' [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo); '@ $SendMouseClick = Add-Type -memberDefinition $signature -name "Win32MouseEventNew" -namespace Win32Functions -passThru # Stopwatch for timing the whole process $SWScriptTime = [Diagnostics.Stopwatch]::StartNew() #endregion Includes #region Variables # ====================== Define Some Screen Variables # Screen size related variables $baseScreenSizeX = 1920 #pixles $baseScreenSizeY = 1080 #pixles $sleepPeriod = 20 #seconds $baseimagepath = "D:\RoboClicky\images\" # Log File Variables $appVersion = "RoboClicky Version 2" $logMonth = (Get-Date -Format "yyyy-MM-") $logDay = (Get-Date -Format "dd-") $basePath = "" $logFile = "" $logDestinationDetail = $basepath + "D:\RoboClicky\"+$logmonth+$logday+"RoboClicky_Detail_Log.txt" # Temporary session log $global:currentLog = "" #endregion Variables #region FUNCTIONS # ====================== FUNCTIONS #region GenericFunctions # I use these in many of my scripts # ====================== Format Date Function function GetDateFormatted(){ Return (Get-Date -Format "yyyy-MM-dd HH:mm:ss") } function GetDateFormattedForPaths(){ Return (Get-Date -Format "yyyy-MM-dd HHmmss") } # ====================== Logging Function function LogThis($logthisstring){ $logEntry = (GetDateFormatted) + "# " + $logthisstring $global:currentLog = $global:currentLog + "`n" + $logentry $logEntry | Out-File $logDestinationDetail -Append $logEntry | Out-host } # ====================== Path Testing Function function CheckFileExists($checkPath){ $filexists = (test-path ($checkPath)) logthis("Check Path: " + $checkPath + " | file exists = " + $filexists) return $filexists } # ====================== Exit Block $ExitNow = { # find me a line Morpheus $SWScriptTime.Stop() logthis("EXIT NOW! Script complete in " + $SWScriptTime.Elapsed + " ... welcome to the real world Neo ...") $global:currentLog | Out-File $currentLogPath -Force exit } #endregion GenericFunctions #region RoboClickyFunctions # ====================== Return mouse to 0,0 coordinates function mooseHome(){ logthis ("F: moving mouse now to coordinates: 0,0") [Windows.Forms.Cursor]::Position = "0,0" return } # ====================== Move mouse to specified coordinates and sleep for sleep period function mooseMove($screenNumber, $xCoords, $yCoords, $label, [boolean]$isrelative, $loadWaitPeriod){ # NOTE::: This code only handles displays that have left to right numbering. Future upgrade will include full relative screen handling. Because it would be awesome! #Log some things logthis ("F: mooseMove function called with paramaters;") logthis (" screenNumber = "+$screenNumber) logthis (" xCoords = "+$xCoords) logthis (" yCoords = " +$yCoords) logthis (" label = " + $label) logthis (" isrelative = " + $isrelative) logthis ("F: calculating absolute coordinates") #Set Absolute X coordinates of the screen (which are relative to the entire screen canvas) $absolutexCoords = $xCoords + ($baseScreenSizeX * $screenNumber) $absoluteyCoords = $yCoords $coords = "$($absolutexCoords),$($absoluteyCoords)" logthis (" ... "+$coords) logthis ("F: moving mouse now to screen " + $screenNumber + ", relative coordinates: x=" + $xCoords + ", y=" + $yCoords + ", absolute coordinates: "+$coords) # move the mouse [Windows.Forms.Cursor]::Position = $coords # perform left mouse click using mooseClick function mooseClick logthis ("F: sleeping for: " + $sleepPeriod +" seconds to allow for screen rendering") sleep $loadWaitPeriod mooseHome return } # ====================== Perform left mouse click function mooseClick { logthis ("F: left mouse clicking now") $SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0); #Left Mouse Down $SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0); #Left Mouse Up return } # ====================== Grab ScreenShot of screen X function screenshot($displaynumber,$screenshotname) { logthis ("F: Performing screen capture of display "+$displaynumber) #$bounds = [Drawing.Rectangle]::FromLTRB($screen.Left, $screen.Top, $screen.right, $screen.bottom) #$bounds = [Drawing.Rectangle]::FromLTRB(-236, -1080, 1684, 0) #calculating cooridinates for screen capture logthis("#calculating cooridinates for screen capture") $captureLeft = $baseScreenSizeX * $displaynumber $captureTop = 0 # fixed value in this version $captureRight = ($baseScreenSizeX * $displaynumber) + 1920 $captureBottom = 1080 # fixed value in this version $bounds = [Drawing.Rectangle]::FromLTRB($captureLeft, $captureTop, $captureRight, $captureBottom) $bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height $graphics = [Drawing.Graphics]::FromImage($bmp) logthis("F: Screen capturing from absolute range:") logthis(" Left: "+$captureLeft) logthis(" Top: "+$captureTop) logthis(" Right: "+$captureRight) logthis(" Bottom: "+$captureBottom) # create image $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size) $path=$baseimagepath + $screenshotname + ".png" logthis ("F: Saving screen capture as "+$path) $bmp.Save($path) $graphics.Dispose() $bmp.Dispose() } function refreshIE($refreshStringRef) { # Function to refresh IE window with a specific name logthis("F: Refreshing IE Where name of window contains '" + $refreshStringRef +"'") $shell = new-object -ComObject shell.application $shell.windows() | Where-Object { $_.document.url -like $('*' + $refreshStringRef + '*')} | ForEach-Object { $_.refresh() } } #endregion RoboClickyFunctions #endregion FUNCTIONS #region Mainline # Step 1 - Refresh IE window containing application visualisation refreshie -refreshStringRef "ie-application-window-name" logthis ("waiting for ie-application-window-name") sleep 30 # Step 2 - Move mouse around the visualisation using the mooseMove function, then snap an image of it using the screenshot function. # Zone A mooseMove -screenNumber 6 -xCoords 685 -yCoords 21 -label "Zone A" -isrelative $true -loadWaitPeriod 25 screenshot -displaynumber 6 -screenshotname "Dashboard_Zone_A" # Zone B mooseMove -screenNumber 6 -xCoords 797 -yCoords 21 -label "Zone B" -isrelative $true -loadWaitPeriod 25 screenshot -displaynumber 6 -screenshotname "Dashboard_Zone_B" # Zone C mooseMove -screenNumber 6 -xCoords 908 -yCoords 21 -label "Zone C" -isrelative $true -loadWaitPeriod 25 screenshot -displaynumber 6 -screenshotname "Dashboard_Zone_C" # Zone D mooseMove -screenNumber 6 -xCoords 1020 -yCoords 21 -label "Zone D" -isrelative $true -loadWaitPeriod 25 screenshot -displaynumber 6 -screenshotname "Dashboard_Zone_D" # Zone E mooseMove -screenNumber 6 -xCoords 1131 -yCoords 21 -label "Zone E" -isrelative $true -loadWaitPeriod 25 screenshot -displaynumber 6 -screenshotname "Dashboard_Zone_E" # Zone F mooseMove -screenNumber 6 -xCoords 1242 -yCoords 21 -label "Zone F" -isrelative $true -loadWaitPeriod 25 screenshot -displaynumber 6 -screenshotname "Dashboard_Zone_F" # Zone G mooseMove -screenNumber 6 -xCoords 1354 -yCoords 21 -label "Zone G" -isrelative $true -loadWaitPeriod 25 screenshot -displaynumber 6 -screenshotname "Dashboard_Zone_G" # Zone H mooseMove -screenNumber 6 -xCoords 1465 -yCoords 21 -label "Zone H" -isrelative $true -loadWaitPeriod 25 screenshot -displaynumber 6 -screenshotname "Dashboard_Zone_A" # Zone etc... mooseMove -screenNumber 6 -xCoords 1576 -yCoords 21 -label "Zone etc... and so on" -isrelative $true -loadWaitPeriod 25 screenshot -displaynumber 6 -screenshotname "Dashboard_Zone_Summary" &$exitnow #endregion Mainline
I have a bunch of DVD’s that I want to archive to hard-disk so I can dispose of the DVDs.
Automate the process as much as possible.
Note: This is not a DVD copy protection crack, the DVD’s have to be copyable by the OS.
This script will wait for the DVD volume name to be NOT NULL
When a DVD is inserted, the script automatically creates a new folder in the destination root path, using the DVD name as the destination folder name.
After completing the copy it ejects the DVD drive and loops back into the waiting state.
I’m wondering if using [System.IO.StreamWriter would be faster, but I think the bottleneck would be in the DVD Read, not the disk write. Speculation only. Next time I use the script I’ll make that modification and see how it goes.
My scripts are open source, free to use, come with neither warranty as to optimal performance nor suggestion that they are necessarily the best way to achieve anything.
<# DVD Copy Script V 2.0 This version speaks so I can hear it from the next room when it needs a new disk SYNOPSIS ---------------------------------------------------------------------- I have a bunch of DVD's that I want to archive to Hard Disk so I can dispose of the DVDs I want to automate it as much as possible. This script will wait for the DVD volumename to be not null When a DVD is inserted it automatically copies the DVD to a destination folder root using the DVD name as a new destination sub-folder After completing it pops open the dvd drive and loops back into waiting for disk mode This version uses speach so I can hear it from another room! FEATURES --------------------------------------------------------------------- * Speach * Destination folder name duplication protection * Simple copy integrity check using length #> # INCLUDES ---------------------------------------------------------------------- # add speach object Add-Type -AssemblyName System.speech $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer # FUNCTIONS --------------------------------------------------------------------- function popdisk(){ (new-object -COM Shell.Application).NameSpace(17).ParseName($dvdpath).InvokeVerb('Eject') } function testpath($tpath){ if ((test-path $tpath) -ne $true) { $speak.Speak('path does not exist ... all is done here"') return $tpath } else{ $speak.Speak('path exists, adding suffix and retesting') $tpath += "_1" $speak.Speak("new foldername is "+ $tpath) testpath "$tpath" } } function CopyDVD{ # Drives and Paths (omit \) $dvdpath = 'I:' #no slash $destpath = 'E:\DVDCOPY' #no slash While($true){ #Clear variables $volumename = $null $foldername = "" $drive ="" $speak.Speak('Waiting for disk') " Waiting for disk ... " | Out-Host #loop while disk volumename is null DO{ $drive = Get-WmiObject -Class Win32_CDROMDrive $volumename = $drive.volumename }while ($volumename -eq $null) #disk detected get source details $speak.Speak('Disk detected, obtaining details.') $foldername = $volumename.ToString() #Check for duplicate folder and suffix number if folder exists $speak.Speak('Disk name is ' + $volumename.ToString()) $source = Get-ChildItem "$dvdpath\VIDEO_TS\" -recurse | Measure-Object -Property length -Sum $sourceLength = $source.Sum start-sleep -seconds 10 $speak.Speak('Copying Disk.') " Copying disk " + $foldername | Out-Host " src len " + $sourceLength | Out-Host $speak.Speak('checking if destination path is already used ' + $volumename.ToString()) $validpath=(testpath "$destpath\$foldername") copy-item "$dvdpath\VIDEO_TS\" "$validpath\VIDEO_TS\" -Recurse -Force #get destination length $destChilds = Get-ChildItem "$validpath\VIDEO_TS\" | Measure-Object -Property length -Sum $speak.Speak('Checking destination integrity') $destLength = $destChilds.Sum " dst len " + $destLength | Out-Host if ($sourceLength -ne $destLength){ " Error! " | Out-Host $speak.Speak('ERROR ERROR ERROR ERROR ERROR') exit } Else{ "ok" | out-host } start-sleep -Seconds 10 $speak.Speak('Hello the D--V-D has finished!') $speak.Speak('Hello the D-V-D has finished!') " Finished " + $foldername | Out-Host # Eject CD popdisk start-sleep -Seconds 10 #end of while } #end of function } # MAIN ---------------------------------------------------------------------- "--------------- starting DVD Auto Copy" | Out-Host popdisk while ($true){ copyDVD }
Version 0.1
<# SYNOPSIS: In the spirit of sharing that enabled me to learn how to code, I am now open sourcing all of my code, that it may be of some use to others. Enjoy! TO DO: I'd like to get around to describing all of the problems each solution solved for, but for now its just a minimalist dump. #>
I first learned to code in ZX Spectrum BASIC at around the age of 10. It was 1986. More accurately, I learned to type and eventually understand the code from various ‘program your own games’ books and ‘Sinclair User’ magazine articles.
It was a little later that I actually achieved an understanding of the primitive building blocks of the code.
Quite possibly my young brain was prepped for the logic and structure of programming through my obsession with the essentially ‘if-then-else’ based Fighting Fantasy series of ‘choose your own adventure’ game books.
‘The Warlock of Firetop Mountain’ was the first I remember owning, and I believe the first in the series. They were awesome. My very early unguided ventures into coding my own programs were literally codifications of these simple narratives.
I, like many people, have learned a lot of what I know about coding from commercially driven ‘pay for knowledge’ sources such as at university, training courses, and books. However, I am just old enough to claim some memory of the relatively early years of open source code sharing.
So, like everybody, I have also learned a stack from the open source information made available by the generosity, or naivety, nevertheless sheer enjoyment of sharing through magazines, blogs, websites, friends and various other mediums.
So, I have decided to dig up and throw into the pot of shared online code, a bunch of my own work; better or poorer to be debated, but certainly useful.
Note: I don’t claim that any of this code still works, nor either that that which does, is optimal for the task. Much of it is old, some of it is new, the context and constraints of the environment it was written for are here absent, but it all served a purpose.
Six times nine equals forty two. This is a cultural and mathematical certainty.
But it doesn’t … does it? Yes it does!
Understanding how this apparent aberration of mathematics works can unlock the secrets of life, work, relationships, business, and, you know, everything.
So don’t panic! Grab a cup of tea, find your towel, and prepare to disrupt your universe.
Forty two is a very important number.
In fact, it’s not just a number. It is the answer. The ultimate answer. The answer to life the universe and everything.