PowerShell: GoodSync Installer/Updater, Service Disabler and Cleanup

I use GoodSync for file synchronization a lot. While a awesome program, there are two major issues I find with it – the lack of a auto-update mechanism and the GoodSync Server service which is auto-enabled each time you update and I personally don’t use. So I created a PowerShell script to take care of all that. Downloading the msi, initiating the install/update, stopping and disabling the GoodSync Server services and cleaning up the temporary files and desktop icons. Enjoy!

GitHub

# GoodSync All
# This script is designed to install or update Goodsync, stop and disable services (GoodSync Server) and clean up desktop icons.
# Designed for GoodSync 10.x

# Checks for Administrator privileges and opens an elevated prompt is user has Administrator rights
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{   
    $arguments = "& '" + $myinvocation.mycommand.definition + "'"
    Start-Process powershell -Verb runAs -ArgumentList $arguments
    Break
}

# SSL Certificate Handling
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# All the variables
$sstat = Get-Service -Name GsServer
$sstart = Get-WmiObject -Query "Select StartMode From Win32_Service Where Name='GsServer'"
$dl = "https://www.goodsync.com/download/GoodSync-v10-Setup.msi"
$msi = "GoodSync-v10-Setup.msi"

# Downloads GoodSync .msi to system TEMP folder, installs and removes .msi
Invoke-WebRequest -uri $dl -OutFile $env:TEMP\$msi
Start-Process msiexec.exe -Wait -ArgumentList "/I $env:TEMP\$msi /quiet"
Remove-Item $env:TEMP\$msi

# Checks for Service Status
# Stops GoodSync Service if it's running, otherwise continues
if ($sstat.status -eq "running"){
    write-output "Stopping GoodSync Server"
    stop-service gsserver
    "Service is stopped"
    "Continuing....."
    ""
    }
elseif ($sstat.status -eq "stopped"){
    write-output "GoodSync Server is already Stopped"
    "Continuing....."
    ""
    }
if ($sstart.startmode -ne "disabled"){
    write-output "Setting GoodSync Server service to Disabled"
    set-service gsserver -startuptype disabled
    "Done"
    }
elseif ($sstart.startmode -eq "disabled"){
    write-output "GoodSync Server service is already Disabled"
    "Done"
    }

# Removes GoodSync desktop shortcuts
Remove-Item $env:public\Desktop\GoodSync*.lnk

 

PowerShell : Windows 10 Modern Application Removal Script

If you have Windows 10 you’ve not doubt seen the new modern apps and either love them, hate them or just don’t want to deal with them. Windows 10 does not provide an easy way to remove any of these applications or to keep them from running which can be a major issue for low RAM systems. The good news is they can quickly be removed via a few PowerShell commands or a script. The script below is one I’ve been using to manage them on my systems. You can also find a link to GitHub where I maintain this and several other PowerShell scripts I routinely use.

GitHub PowerShell Scripts | GitHub W10RemoveCoreApps Script
Read more of this post

Weekend Project: Process Explorer Auto Install

I was bored this weekend and decided to try my hand at making a PowerShell script to automate the install of Sysinternals Process Explorer. It’s pretty rough product of about 3 hours of work. I’ll make improvements in the future as I get time. In any case, it’s available below.

#### Downloads Process explorer from download.sysinternals.com,
#### unzips it into Program Files and then cleans up.
####
#### Sources:
####	s1: http://nyquist212.wordpress.com/2013/09/23/powershell-webclient-example/
####	s2: http://sharepoint.smayes.com/2012/07/extracting-zip-files-using-powershell/

#Checks for Administrator privileges and opens an elevated prompt is user has Administrator rights
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{   
    $arguments = "& '" + $myinvocation.mycommand.definition + "'"
    Start-Process powershell -Verb runAs -ArgumentList $arguments
    Break
}

# s1 
Function Get-Webclient ($urla, $out) {
    $proxy = [System.Net.WebRequest]::GetSystemWebProxy()
    $proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    $request = New-Object System.Net.WebClient
    $request.UseDefaultCredentials = $true ## Proxy credentials only
    $request.Proxy.Credentials = $request.Credentials
    $request.DownloadFile($urla, $out)
}

# s2 

# Expands the entire contents of a zip file to a folder
# MSDN References
# - Shell Object:   http://msdn.microsoft.com/en-us/library/windows/desktop/bb774094(v=vs.85).aspx
# - SHFILEOPSTRUCT: http://msdn.microsoft.com/en-us/library/windows/desktop/bb759795(v=vs.85).aspx
function Expand-Zip (
    [ValidateNotNullOrEmpty()][string]$ZipFilePath,
    [ValidateNotNullOrEmpty()][string]$DestinationFolderPath,
    [switch]$HideProgressDialog,
    [switch]$OverwriteExistingFiles
    ) {
    # Ensure that the zip file exists, the destination path is a folder, and the destination folder
    # exists. The code to expand the zip file will *only* execute if the three conditions above are
    # true.
    if ((Test-Path $ZipFilePath) -and (Test-Path $DestinationFolderPath) -and ((Get-Item $DestinationFolderPath).PSIsContainer)) {
        try {
            # Configure the flags for the copy operation based on the switches passed to this
            # function. The flags for the CopyHere method are based on the SHFILEOPSTRUCT
            # structure's fFlags field. Two of the flags are leveraged by this function.
            # 0x04 --- Do not display a progress dialog box.
            # 0x10 --- Click "Yes to All" in any dialog box displayed. Functionally overwrites any
            #          existing files.
            $copyFlags = 0x00
            if ($HideProgressDialog) {
                $copyFlags += 0x04
            }
            if ($OverwriteExistingFiles) {
                $copyFlags += 0x10
            }
            
            # Create the Shell COM object
            $shell = New-Object -ComObject Shell.Application
            
            # Get references to the zip file and the destination folder as Shell Folder COM objects
            $zipFile = $shell.NameSpace($ZipFilePath)
            $destinationFolder = $shell.NameSpace($DestinationFolderPath)
            
            # Execute a file copy from the zip file to the destination folder; which effectively
            # extracts the zip file's contents to the destination folder
            $destinationFolder.CopyHere($zipFile.Items(), $copyFlags)
        } finally {
            # Release the COM objects
            if ($zipFile -ne $null) {
                [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($zipFile)
            }
            if ($destinationFolder -ne $null) {
                [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($destinationFolder)
            }
            if ($shell -ne $null) {                
                [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($shell)
            }
        }
    }
}

function mkdirs {
    mkdir $sDir\temp\ -force > $null
    mkdir $sDir\ProcessExplorer\ -force > $null
    mkdir "$start\Process Explorer" -force > $null
}

function shortcuts ($target, $link) {
    # Create a Shortcut with Windows PowerShell
    $TargetFile = $target
    $ShortcutFile = $link
    $WScriptShell = New-Object -ComObject WScript.Shell
    $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
    $Shortcut.TargetPath = $TargetFile
    $Shortcut.Save()
    }

# Variables
$sDir = $env:programfiles
#$uDir = $env:allusersprofile
$start = [Environment]::GetFolderPath('CommonStartMenu') + "\Programs"
$url = "http://download.sysinternals.com/files/ProcessExplorer.zip"
$file = $sDir + "\temp\ProcessExplorer.zip"

# Makes directories:
# ProcessExplorer directory in Program Files according to Environment variable\
# temp directory in Program Files for download
mkdirs
Get-Webclient $url $file
Start-Sleep -s 2
# Closes Process Explorer if running
Get-Process procexp* | stop-process –force
Expand-Zip $file "$sDir\ProcessExplorer\" -HideProgressDialog -OverwriteExistingFiles
Remove-Item "$sDir\temp\" -recurse
# Creates Start Menu shorcuts
shortcuts "$sDir\ProcessExplorer\Eula.txt" "$start\Process Explorer\EULA.lnk"
shortcuts "$sDir\ProcessExplorer\procexp.chm" "$start\Process Explorer\Process Explorer Help.lnk"
shortcuts "$sDir\ProcessExplorer\procexp.exe" "$start\Process Explorer\Process Explorer.lnk"
# Accepts EULA and starts minimized
start-process $sDir\ProcessExplorer\procexp.exe -ArgumentList "/AcceptEula /t"

 

Download ProcessExplorerInstaller.ps1 from Github
GitHub | PowerShell-Scripts / ProcessExplorerInstaller.ps1

PowerShell: Empty Recycle Bin

Here is a quick PowerShell script I found recently to clear the Windows Recycle Bin. This can be really useful if you want to automatically empty the Recycle Bin through something like the Task Scheduler. This code comes from the TechNet Script Center, courtesy of Windows Engineer and PowerShell Blogger Rich Prescott.

$Shell = New-Object -ComObject Shell.Application
$RecBin = $Shell.Namespace(0xA) 
$RecBin.Items() | %{Remove-Item $_.Path -Recurse -Confirm:$false}

This script allows you to view the contents of the recycle bin in your profile. The first line creates a ComObject and then the second line grabs the Recycling Bin special folder. It then enumerates the items contained in that special folder and removes each of them. The Remove-Item cmdlet includes a switch to turn off confirmation for the removal of the files. It can be removed if you would like to be prompted for each file.

Works on:

Windows Server 2012 and Up Yes Windows 10 and Up Yes
Windows Server 2008 R2 Yes Windows 8 Yes
Windows Server 2008 Yes Windows 7 Yes
Windows Server 2003 No Windows Vista Yes
Windows XP Yes
Windows 2000 No

Source

PowerShell: Check System Execution Policy

Here is another quick script to check your system’s Execution Policy.

batch file: pspolicycheck.bat

@echo off
powershell.exe -noexit Get-ExecutionPolicy -list

This will give you a quick list of Execution Policies on your system and give you a PowerShell prompt. Of course you can always simple run “Get-ExecutionPolicy -list” from the Command Line or PowerShell prompt. You can download bar from the above link or copy the 2 lines above to a text file and rename it .bat.

pspolicycheck-bat

PowerShell : Uptime scripts

I wanted a quick way to determine uptime for my servers and other windows computers and so started searching for solutions. After some time I finally found some good information but it wasn’t entirely user friendly. After another 2 hours of researching and trying to get it right, I came up with these scripts for displaying your computer’s uptime. The PowerShell scripts aren’t mine (you’ll find links to the originals below) but the batch files are. Pretty simple but a nice time saver.
Read more of this post

Verified by MonsterInsights