PS: Startup Script

1 minute read

Description:

One of my main goals this year is to replace all of my batch scripts with Powershell scripts. These are some examples.

To Resolve:

  1. When calling external programs, there is really one a few rules:
  • Use “&” to call the app.
#Shut down VMs

Try
{
    Log "Gracefully shutting down VMs so we can reboot"
    # Website
    & "C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" controlvm "XXXXXXX-383a-00000-bb2f-6da0ea34f94c" acpipowerbutton
    # Plex
    & "C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" controlvm "XXXXXXX-2816-00000-bd90-7af3cf0b39cb" acpipowerbutton
}
Catch
{
    Write-ErrorLog -Message $($_.Exception.Message) -ExitGracefully
}

And my startup script:

#Startup

Log "Running default startup script"
            
# Start-Process "Z:\google\myexe.exe"
# Start-Process "Z:\google\myexe.exe"
# Placed in "shell:startup" instead...
			
Set-Location "C:\Program Files\Oracle\VirtualBox"
& vdesk create:3

$Command = 'C:\Program Files (x86)\VDesk\vdesk.exe'
$Arguments = @("on:3", "run:$Arguments2")
$Arguments2 = '"C:\Program Files\Oracle\VirtualBox\VirtualBox.exe" --comment "gw-www" --startvm "XXXXXXX-383a-4121-bb2f-6da0ea34f94c"'
& $Command $Arguments

$Command = 'C:\Program Files (x86)\VDesk\vdesk.exe'
$Arguments = @("on:3", "run:$Arguments2")
$Arguments2 = '"C:\Program Files\Oracle\VirtualBox\VirtualBox.exe" --comment "gw-plex" --startvm "XXXXXXX-2816-45fa-bd90-7af3cf0b39cb"'
& $Command $Arguments

Log "Setting BGInfo Background"
$Command = "C:\Sysinternals Suite\bginfo\bginfo.exe"
$Arguments = @("C:\bginfo\mysettings.bgi", "/timer:0")
& $Command $Arguments
  • Strategically place apostrophes if the command has multiple arguments – see lines 31 – 322. As mentioned in a previous post, use the “Don’t parse” symbol when converting longer commands. For example my backup scripts are now:
$Command = "$env:Windir\system32\robocopy.exe"
$Arguments = @("C:\scripts", "C:\scripts2", 
"/mir", "/r:1", "/w:1", "/np", "/nfl","/ndl")
& $Command $Arguments