Friday, July 13, 2018

C# TrimStart and TrimEnd with integer length parameter string extensions

This C# code overloads TrimStart and TrimEnd methods, adding an integer length parameter which  provides a more intuitive method call, in my humble opinion.

TrimStart(int length) and TrimEnd(int length) methods signatures added using boilerplate extension methods.

string numbers = "12345"; 
numbers.TrimStart(1);  --> "2345" 
numbers.TrimStart(50); --> "" 
numbers.TrimEnd(1);    --> "1234" 
numbers.TrimEnd(50);   --> ""


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    public static class StringExtensions
    {
        //Note: Intuitive method names, IMHO metadataconsult@gmail.com 
        //      Prefered method signature with uint, but introduces a range issue UNIT (4294967295) > INT (+/-2147483647) and therefore misleading 
        //      Be great if c# had a UINT which was same size as INT (2147483647)
   
        /// <summary>
        /// Gets string removing a length from start of string 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="length">remove length from start of input, returning remaining string to end</param>
        /// <returns>Bonus: if index is greater that length, returns empty string</returns>
        public static string TrimStart(this string input, int length)
        {
            if (string.IsNullOrEmpty(input) || length < 0) return input;
            
            if (length > input.Length) return ""; //bonus

            return input.Substring(length); 
        }
        /// <summary>
        /// Gets string less length from end
        /// </summary>
        /// <param name="input"></param>
        /// <param name="length">remove length from end of input, returning remaining string from start</param> 
        /// <returns>Bonus: if index is greater that length, returns empty string</returns>
        public static string TrimEnd(this string input, int length)
        {
            if (string.IsNullOrEmpty(input) || length < 1) return input;

            if (length > input.Length) return  ""; //bonus

            return input.Substring(0, input.Length - length);
        }
    }

Thursday, July 12, 2018

Cleanup Powershell Tracing Directories script

Removes the Poweshell Script Tracing directories, by strictly matching directories in Documents directory that match YYYYMMDD only.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Script: CleanupPowershellTracingDirectories.ps1 
#
# Created by metadataconsult@gmail.com, metadataconsulting.blogspot.ca, 2018
#
# Function: 
#
# Removes the Poweshell Script Tracing directories, by strictly matching directories in documents directory that match YYYYMMDD only.
#
# Warranty: 
#
# The  scripts are provided AS IS without warranty of any kind. 
# Metadata Consulting further disclaims all implied warranties including,  
# without limitation, any implied warranties of merchantability or of fitness for 
# a particular purpose. The entire risk arising out of the use or performance of  
# the  scripts and documentation remains with you. 
# Metadata Consulting, its authors, or anyone else involved in the creation, production, or 
# delivery of the scripts be liable for any damages whatsoever (including, 
# without limitation, damages for loss of business profits, business interruption, 
# loss of business information, or other pecuniary loss) arising out of the use 
# of or inability to use the  scripts or documentation, even if  
# has been advised of the possibility of such damages. 
#------------------------------------------------------------------------------------------------------------------------------------------------------------------

$docfolder = "${Env:USERPROFILE}\Documents"

Write-Host "Listing All Poweshell Script Tracing Directories in folder $docfolder" -ForegroundColor Cyan
# https://stackoverflow.com/questions/4766845/yyyymmdd-date-format-regular-expression-to-validate-a-date-in-c-sharp-net - mactches leap years as well
Get-ChildItem $docfolder | Where {$_.Name -Match "^(?:(?:(?:(?:(?:[13579][26]|[2468][048])00)|(?:[0-9]{2}(?:(?:[13579][26])|(?:[2468][048]|0[48]))))(?:(?:(?:09|04|06|11)(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02(?:0[1-9]|1[0-9]|2[0-9]))))|(?:[0-9]{4}(?:(?:(?:09|04|06|11)(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02(?:[01][0-9]|2[0-8])))))$"}

$title = "Delete Poweshell Script Tracing Directories?"
$message = "Choose default Exit,`nor DELETE Selected Powershell Script Tracing Directories,`nor DELETE SILENT All Powershell Script Tracing Directories.`nThis is permanent delete, there is no recycle-bin recovery."
$msg1 = "Exit" 
$msg2 = "DELETE Selected Script Tracing Directories forever, no recycle bin!"
$msg3 = "DELETE All Silently Script Tracing Directories forever, no recycle bin!"
$one = New-Object System.Management.Automation.Host.ChoiceDescription "&Exit", $msg1 
$two = New-Object System.Management.Automation.Host.ChoiceDescription "&Delete Selected", $msg2
$three = New-Object System.Management.Automation.Host.ChoiceDescription "&Delete Silent", $msg3
$options = [System.Management.Automation.Host.ChoiceDescription[]]($one,$two, $three)
$result = $host.ui.PromptForChoice($title, $message, $options, 0) 

Write-Host ""
switch ($result)
    {
        0 {Write-Host "You selected `n1. $msg1" -ForegroundColor Cyan;}
        1 {Write-Host "You selected `n2. $msg2" -ForegroundColor Cyan;}
        2 {Write-Host "You selected `n3. $msg3" -ForegroundColor Cyan;}
        default {Write-Host "You choose something else. Exiting"; exit}
    }

if ($result -eq 1) {

Get-ChildItem $docfolder | Where {$_.Name -Match "^(?:(?:(?:(?:(?:[13579][26]|[2468][048])00)|(?:[0-9]{2}(?:(?:[13579][26])|(?:[2468][048]|0[48]))))(?:(?:(?:09|04|06|11)(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02(?:0[1-9]|1[0-9]|2[0-9]))))|(?:[0-9]{4}(?:(?:(?:09|04|06|11)(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02(?:[01][0-9]|2[0-8])))))$"} | Remove-Item -ErrorAction Stop
Write-Host "Deleted selected Poweshell Script Tracing Directories in folder $docfolder" -ForegroundColor Yellow
Write-Host "Done" -ForegroundColor Cyan

}
elseif ($result -eq 2) {

Get-ChildItem $docfolder | Where {$_.Name -Match "^(?:(?:(?:(?:(?:[13579][26]|[2468][048])00)|(?:[0-9]{2}(?:(?:[13579][26])|(?:[2468][048]|0[48]))))(?:(?:(?:09|04|06|11)(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02(?:0[1-9]|1[0-9]|2[0-9]))))|(?:[0-9]{4}(?:(?:(?:09|04|06|11)(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02(?:[01][0-9]|2[0-8])))))$"} | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
Write-Host "Deleted all Poweshell Script Tracing Directories in folder $docfolder, errors silently ignored." -ForegroundColor Yellow
Write-Host "Done" -ForegroundColor Cyan

}

Tuesday, July 10, 2018

Windows 10 Create Password Backup Reset Disk Window is Hidden


In Windows 10, I found a bug,  when I tried to run Create a password reset disk for the 1st time, the window is hidden from view. I could Alt-Tab and see it listed but I could not get it into view. 

This is normal way to invoke Create a password reset disk.
  1. Plug your USB drive 
  2. Press Windows key + S on your keyboard to bring up the search bar.
  3. Type Control Panel and open it.
  4. Type in User accounts in Search Control Panel.
  5. Click on User accounts.
  6. Select Create a password reset disk *USB must be present to run
If you can't see the Create a password reset disk Wizard windows. You can try, to cascade windows. But this did not work for me. 




I had to run this manually for me to be able to backup my password. 

Soln: Create a password reset disk from RUN

Open the Run box, type the following and hit Enter to open it directly:
rundll32.exe keymgr.dll,PRShowSaveWizardExW