Skip to content

Instantly share code, notes, and snippets.

@composite
Created July 27, 2018 06:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save composite/494e0f506d579b92bf598b965ce21532 to your computer and use it in GitHub Desktop.
Save composite/494e0f506d579b92bf598b965ce21532 to your computer and use it in GitHub Desktop.
A Powershell script that backup or restore Sticky Notes for Windows 10 Version 1607 (Anniversary Update) or later.
Add-Type -AssemblyName System.Windows.Forms
try {
Get-Variable PSScriptRoot -Scope Global -ErrorAction 'Stop' | Out-Null
} catch [System.Management.Automation.ItemNotFoundException] {
$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
}
Function Get-FileName([System.String]$title)
{
$openFileDialog = New-Object Windows.Forms.OpenFileDialog
$openFileDialog.initialDirectory = $PSScriptRoot
$openFileDialog.title = if([System.String]::IsNullOrEmpty($title) -eq $true) {"Open file"} else {$title}
$openFileDialog.filter = "All files (*.*)| *.*"
$openFileDialog.filter = "All files|*.*"
$openFileDialog.ShowHelp = $False
$result = $openFileDialog.ShowDialog()
if($result -eq "OK") {$OpenFileDialog.filename} else {[System.String]::Empty}
} #end function Get-FileName
Function Set-FileName([System.String]$title, [System.String]$file)
{
$saveFileDialog = New-Object Windows.Forms.SaveFileDialog
$saveFileDialog.initialDirectory = $PSScriptRoot
$saveFileDialog.title = if([System.String]::IsNullOrEmpty($title) -eq $true) {"Save file"} else {$title}
$saveFileDialog.filter = "SQLite file (*.sqlite)| *.sqlite"
$saveFileDialog.filter = "SQLite file|*.sqlite"
$saveFileDialog.ShowHelp = $False
$saveFileDialog.FileName = $file
$result = $saveFileDialog.ShowDialog()
if($result -eq "OK") {$saveFileDialog.filename} else {[System.String]::Empty}
} #end function Get-FileName
function Using-Object
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[AllowEmptyCollection()]
[AllowNull()]
[Object]
$InputObject,
[Parameter(Mandatory = $true)]
[scriptblock]
$ScriptBlock
)
try
{
. $ScriptBlock
}
finally
{
if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])
{
$InputObject.Dispose()
}
}
}
Function Check-Sqlite([System.String]$file)
{
[byte[]]$bytes = New-Object -TypeName byte[] -ArgumentList 17
Using-Object ([System.IO.FileStream]$fs = New-Object System.IO.FileStream($file, 'Open', 'Read')) {
$fs.Read($bytes, 0, 16) | Out-Null
}
[System.String]$chkStr = [System.Text.ASCIIEncoding]::ASCII.GetString($bytes)
if($chkStr.StartsWith("SQLite format")){$True}else{$False}
}
$ver = [System.Environment]::OSVersion.Version
if($ver.Major -lt 10 -or $ver.Build -lt 14393){
Write-Host $ver
[System.Windows.Forms.MessageBox]::Show("Your windows version not supported yet.`nYou should run this script on Windows 10, Version 1607 (Anniversary Update) or later.",'Unsupported information',"Ok","Error") | Out-Null
Exit
}
$OpenFile = ""
$SaveFile = ""
$_TITLE = "Sticky Note Backup/Restore"
$_FILE = [System.String]::Concat($Env:LocalAppData, "\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\plum.sqlite")
$msgBoxInput = [System.Windows.Forms.MessageBox]::Show("What do you want to do for Sticky backup? `nYES`t: Backup`nNO`t: Restore`nCancel`t: Exit script",$_TITLE,"YesNoCancel","Question")
switch ($msgBoxInput) {
'Yes' {
:SaveRetry while([System.String]::IsNullOrEmpty($SaveFile) -eq $True){
$SaveFile = Set-FileName "Save backup file to:" $([System.String]::Concat("plum.", [System.DateTime]::Now.ToString("yyyyMMddHHmmss"), ".sqlite"))
Write-Host $SaveFile
if([System.String]::IsNullOrEmpty($SaveFile) -eq $True -and [System.Windows.Forms.MessageBox]::Show('Would you like to exit?', $_TITLE, 'YesNo', 'Question') -eq 'Yes'){ break }
}
if([System.String]::IsNullOrEmpty($SaveFile) -eq $False) {
try{
Copy-Item $_FILE $SaveFile -Force
[System.Windows.Forms.MessageBox]::Show('Backup completed.', $_TITLE, 'Ok', 'Information')
}catch{
Write-Error $_.Exception
if([System.Windows.Forms.MessageBox]::Show('Backup failed.`nit can cause by restricted access to this directory, or some file write problem.`nwould you like to retry another directory?', $_TITLE, 'YesNo', 'Error') -eq 'Yes'){
continue :SaveRetry
}
}
}
}
'No' {
:OpenRetry while([System.String]::IsNullOrEmpty($OpenFile) -eq $True){
$OpenFile = Get-FileName "Restore from this file:"
Write-Host $OpenFile
if([System.String]::IsNullOrEmpty($OpenFile) -eq $True -and [System.Windows.Forms.MessageBox]::Show('Would you like to exit?', $_TITLE, 'YesNo', 'Question') -eq 'Yes'){ break }
if($(Check-Sqlite $OpenFile) -eq $False){
[System.Windows.Forms.MessageBox]::Show('This file is not valid.', $_TITLE, 'Ok', 'Warning') | Out-Null
$OpenFile = ""
}
}
if([System.String]::IsNullOrEmpty($OpenFile) -eq $False) {
try{
Copy-Item $OpenFile $_FILE -Force
[System.Windows.Forms.MessageBox]::Show('Restore completed. restart Sticky Note to take effect.', $_TITLE, 'Ok', 'Information')
}catch{
Write-Error $_.Exception
if([System.Windows.Forms.MessageBox]::Show('Restore failed. would you like to retry another file?', $_TITLE, 'YesNo', 'Error') -eq 'Yes'){
continue :OpenRetry
}
}
}
}
}
Add-Type -AssemblyName System.Windows.Forms
try {
Get-Variable PSScriptRoot -Scope Global -ErrorAction 'Stop' | Out-Null
} catch [System.Management.Automation.ItemNotFoundException] {
$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
}
Function Get-FileName([System.String]$title)
{
$openFileDialog = New-Object Windows.Forms.OpenFileDialog
$openFileDialog.initialDirectory = $PSScriptRoot
$openFileDialog.title = if([System.String]::IsNullOrEmpty($title) -eq $true) {"파일 선택"} else {$title}
$openFileDialog.filter = "모든 파일 (*.*)| *.*"
$openFileDialog.filter = "모든 파일|*.*"
$openFileDialog.ShowHelp = $False
$result = $openFileDialog.ShowDialog()
if($result -eq "OK") {$OpenFileDialog.filename} else {[System.String]::Empty}
} #end function Get-FileName
Function Set-FileName([System.String]$title, [System.String]$file)
{
$saveFileDialog = New-Object Windows.Forms.SaveFileDialog
$saveFileDialog.initialDirectory = $PSScriptRoot
$saveFileDialog.title = if([System.String]::IsNullOrEmpty($title) -eq $true) {"파일 저장"} else {$title}
$saveFileDialog.filter = "SQLite 파일 (*.sqlite)| *.sqlite"
$saveFileDialog.filter = "SQLite 파일|*.sqlite"
$saveFileDialog.ShowHelp = $False
$saveFileDialog.FileName = $file
$result = $saveFileDialog.ShowDialog()
if($result -eq "OK") {$saveFileDialog.filename} else {[System.String]::Empty}
} #end function Get-FileName
function Using-Object
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[AllowEmptyCollection()]
[AllowNull()]
[Object]
$InputObject,
[Parameter(Mandatory = $true)]
[scriptblock]
$ScriptBlock
)
try
{
. $ScriptBlock
}
finally
{
if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])
{
$InputObject.Dispose()
}
}
}
Function Check-Sqlite([System.String]$file)
{
[byte[]]$bytes = New-Object -TypeName byte[] -ArgumentList 17
Using-Object ([System.IO.FileStream]$fs = New-Object System.IO.FileStream($file, 'Open', 'Read')) {
$fs.Read($bytes, 0, 16) | Out-Null
}
[System.String]$chkStr = [System.Text.ASCIIEncoding]::ASCII.GetString($bytes)
if($chkStr.StartsWith("SQLite format")){$True}else{$False}
}
$ver = [System.Environment]::OSVersion.Version
if($ver.Major -lt 10 -or $ver.Build -lt 14393){
Write-Host $ver
[System.Windows.Forms.MessageBox]::Show("아직 지원하지 않는 운영체제입니다.`n윈도우 10 1주년 업데이트 이상 스티커 메모 앱에서 실행 가능합니다.",'미지원 운영체제',"Ok","Error") | Out-Null
Exit
}
$OpenFile = ""
$SaveFile = ""
$_TITLE = "스티커 메모 백업/복구 프로그램"
$_FILE = [System.String]::Concat($Env:LocalAppData, "\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\plum.sqlite")
$msgBoxInput = [System.Windows.Forms.MessageBox]::Show("스티커 메모 작업 방법을 선택하세요. `n예`t: 백업`n아니오`t: 복구`n취소`t: 종료",$_TITLE,"YesNoCancel","Question")
switch ($msgBoxInput) {
'Yes' {
:SaveRetry while([System.String]::IsNullOrEmpty($SaveFile) -eq $True){
$SaveFile = Set-FileName "백업 파일 저장" $([System.String]::Concat("plum.", [System.DateTime]::Now.ToString("yyyyMMddHHmmss"), ".sqlite"))
Write-Host $SaveFile
if([System.String]::IsNullOrEmpty($SaveFile) -eq $True -and [System.Windows.Forms.MessageBox]::Show('취소하고 종료하시겠습니까?', $_TITLE, 'YesNo', 'Question') -eq 'Yes'){ break }
}
if([System.String]::IsNullOrEmpty($SaveFile) -eq $False) {
try{
Copy-Item $_FILE $SaveFile -Force
[System.Windows.Forms.MessageBox]::Show('백업이 완료되었습니다.', $_TITLE, 'Ok', 'Information')
}catch{
Write-Error $_.Exception
if([System.Windows.Forms.MessageBox]::Show('백업이 실패되었습니다. 권한 문제 및 잘못된 경로일 수 있습니다. 다른 이름으로 저장하시겠습니까?', $_TITLE, 'YesNo', 'Error') -eq 'Yes'){
continue :SaveRetry
}
}
}
}
'No' {
:OpenRetry while([System.String]::IsNullOrEmpty($OpenFile) -eq $True){
$OpenFile = Get-FileName "복구할 파일 선택"
Write-Host $OpenFile
if([System.String]::IsNullOrEmpty($OpenFile) -eq $True -and [System.Windows.Forms.MessageBox]::Show('취소하고 종료하시겠습니까?', $_TITLE, 'YesNo', 'Question') -eq 'Yes'){ break }
if($(Check-Sqlite $OpenFile) -eq $False){
[System.Windows.Forms.MessageBox]::Show('올바른 스티커 메모 파일이 아닙니다.', $_TITLE, 'Ok', 'Warning') | Out-Null
$OpenFile = ""
}
}
if([System.String]::IsNullOrEmpty($OpenFile) -eq $False) {
try{
Copy-Item $OpenFile $_FILE -Force
[System.Windows.Forms.MessageBox]::Show('복구가 완료되었습니다. 스티커 메모를 다시 실행하면 적용됩니다.', $_TITLE, 'Ok', 'Information')
}catch{
Write-Error $_.Exception
if([System.Windows.Forms.MessageBox]::Show('복구가 실패되었습니다. 다른 파일로 복구하시겠습니까?', $_TITLE, 'YesNo', 'Error') -eq 'Yes'){
continue :OpenRetry
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment