PowerShell – Prompt/Confirm User’s Choice (A Simple Yes/No function)

Here’s a sample of a custom function inspired from a previous PowerShell Tip of the Week:

function Confirm-UserChoice
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([bool])]
    Param
    (
        [Parameter(Mandatory=$true,
                   Position=0)]
        [Alias('Title')]
        [string] 
        $PromptTitle,
        [Parameter(Mandatory=$true,
                   Position=1)]
        [Alias('Message')] 
        [string]
        $PromptMessage,
        [Parameter(Mandatory=$false,
                   Position=2)]
        [Alias('YesDesc')]
        [string]
        $YesOptionDescription,
        [Parameter(Mandatory=$false,
                   Position=3)]
        [Alias('NoDesc')]
        [string]
        $NoOptionDescription
    )
    Begin
    {
        if ($PSBoundParameters['YesOptionDescription'])
        {
            $yes = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes',$YesOptionDescription
        }
        else
        {
            $yes = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'
        }
        if ($PSBoundParameters['NoOptionDescription'])
        {
            $no = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&No',$NoOptionDescription
        }
        else
        {
            $no = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&No'
        }
        $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
        #$maybe = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&Maybe','Maybe we want to do provide more options'
        #$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no,$maybe)
    }
    Process
    {
        $result = $Host.UI.PromptForChoice($PromptTitle, $PromptMessage, $options, 0) # Index of default option. '0' in this case represents 'Yes' in the above example, '1' for 'No'.

    switch ($result)
        {
            0 {return $true}
            1 {return $false}
        }
    }
    End
    {
        Remove-Variable -Name yes,no,options,result
    }
}

Where the usage looks like this:

Confirm-UserChoice -PromptTitle 'Confirm Action' -PromptMessage 'Do you really want to do this?' -YesOptionDescription 'Yes, I do.' -NoOptionDescription 'No, I do not.'

Bonus example: a slightly, possibly bad design decision, that let’s you make a mini pop-up selection at once:

function Confirm-OnTheFlyChoice
{
    [CmdletBinding(DefaultParameterSetName='Default', 
                  PositionalBinding=$false,
                  ConfirmImpact='Medium')]
    [Alias('cotfc')]
    Param
    (
        [Parameter(Mandatory=$true, 
                   Position=0,
                   ParameterSetName='Default')]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias('Title')] 
        [string]
        $PromptTitle,
        [Parameter(Mandatory=$true, 
                   ValueFromPipeline=$true,
                   Position=1,
                   ParameterSetName='Default')]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias('Message')] 
        [string]
        $PromptMessage,
        [Parameter(Mandatory=$true,
                   Position=2,
                   HelpMessage='Menu options:',
                   ParameterSetName='Default')]
        [string[]]
        $Option
    )
    Begin
    {
        $OptionArray = New-Object -TypeName System.Collections.ArrayList
        Write-Verbose -Message 'Listing Parameters utilized:'
        $PSBoundParameters.GetEnumerator() | ForEach-Object -Process { Write-Verbose -Message "$($PSItem)" }
    } # END: Begin
    Process
    {
        for ($i = 0; $i -lt $Option.Count; $i++)
        { 
            $OptionArray.Add("&$($Option[$i])") | Out-Null
        }
            $options = [System.Management.Automation.Host.ChoiceDescription[]]($OptionArray)
	        $result = $Host.UI.PromptForChoice($PromptTitle, $PromptMessage, $options, 0)
    } # END: Process
    End
    {
        return $Option[$result]
        Remove-Variable -Name OptionArray,options,result
    } # END: End
}

Where the usage would be:

Confirm-OnTheFlyChoice -PromptTitle 'I needed a fast menu' -PromptMessage 'What do you select?' -Option Bird,Cat,Dog,Fish

Note on a bug: the Confirm-OnTheFlyChoice would introduce an issue as-is if you have two values with the same starting letter.
Example:

-Option Fish,Ferret
Advertisements

T-SQL Bulk Insert From Text (TXT) File Into A Temporary Table

First, have a text file with some values you would like to BULK INSERT.
The example below is a text file called ‘animals.txt’ with contents of:

 cat
 dog
 fish
 bird
 snake

The SQL snippet:

 CREATE TABLE #TempTable (Pets CHAR(16));
 BULK INSERT #TempTable
 FROM 'C:\SQLTesting\animals.txt'
 WITH
 (
 DATAFILETYPE= 'char',
 FIELDTERMINATOR=',',
 ROWTERMINATOR='\r' -- Carriage Return
 );
 SELECT Pets FROM #TempTable;
 -- Remove our temporary table.
 DROP TABLE #TempTable;

CREATE TABLE:
https://msdn.microsoft.com/en-us/library/ms174979.aspx

BULK INSERT:

https://msdn.microsoft.com/en-us/library/ms188365.aspx

PowerShell – Single Quote vs. Double Quote

What’s the difference with quotes?

Single quote: It is taken literally and provided back.
Double quotes: PowerShell looks for the $ character inside of the double quotes. Once found, it assumes the following characters are the name of a variable up to the first space, is a variable name.

Sample:

[System.String]$a = 'Hello'
$b = '$a World!'
# $b prints: $a World!
$c = "$a World!"
# $c prints: Hello World!

Error Message: Windows Update cannot currently check for updates, because the service is not running. You need to restart your computer.

Have had a few Windows 7 builds over the last couple weeks with this issue, and a single Vista machine.

Note: Always back-up the files or move them into a “TEST” folder in the same directory, rather than outright deleting the file. You may need it back!

Try the following steps to resolve the following Error Message:

Windows Update cannot currently check for updates, because the service is not running. You need to restart your computer.

 

Review the WindowsUpdate.log file for error messages. It is located in %windir%\WindowsUpdate.log

1. If you have a proxy server:

  • Stop Automatic Update Service via an Elevated Command Prompt:
    net stop wuaserv
  • Then run the following command from the Elevated Command Prompt:
    proxycfg.exe -u
  • Start Automatic Update Service again:
    net start wuaserv
  • Restart and check for Windows Updates/Force Update check

2. Re-create the Datastore (this is what worked on most of the machines in my case):

  • Stop Automatic Update Service via an Elevated Command Prompt:
    net stop wuaserv
  • Rename/delete “%windir%\SoftwareDistribution” folder. 
  • Start Automatic Update Service again:
    net start wuaserv
  • Restart and check for Windows Updates

3. Update the “Intel Rapid Storage” driver on Intel-based systems:

http://www.intel.com/p/en_US/support/highlights/chpsts/imsm