PowerShell: long lines in Get-History

If I just save my history thus…

get-history | clip

, then I find beyond 80 characters whatever, you get truncation (note the ellipses):

PowerShellHistory01

Outputting to json helps…

Get-History | ConvertTo-Json

, but it is still a bit of a hunt to find what you want:

PowerShellHistory02.PNG

First, we will get the history into an array of strings, splitting on Newline:

PowerShellHistory03

$history = Get-History | ConvertTo-Json
$historyLines = $history -split [System.Environment]::Newline

From the previous screenshot, I am only interested in those lines which contain [CommandLine]:

$historyLines | % {if ($_ -match "CommandLine") { $_ }}

PowerShellHistory04

But I want to exclude the repeated “CommandLine” etc. For now, I’ll just cheat. I found that the start of the variable part is at offset 25, so I’ll just do this for now:

$historyLines | % {if ($_ -match "CommandLine") {$_.substring(25, $_.length - (25+2)) }}

, which gives this, which is good enough for now:

PowerShellHistory05

Finally, bringing the 3 lines together:

$history = Get-History | ConvertTo-Json
$historyLines = $history -split [System.Environment]::Newline
$historyLines | % {if ($_ -match "CommandLine") {$_.substring(25, $_.length - (25+2)) }}

asdf


Aliases, plus other ways to get the full history

h | Format-List -Property *
h | Format-List -Property CommandLine
h -id 31 -Count 10
h -id 31 -Count 10 | fl
h | fl -Property CommandLine | clip

Advertisements

Azure: more than 1 subscription?

Yes, I do have more than one.

But before getting to that, when you start a PowerShell Azure session, login like this (in a “proper” environment you might have a secured credentials file):

AddAzureRmAccount
AzurePowerShell2_01

This is the THE way to log in. Just do it. And notice the presence of RM for Resource Manager. Where there is a “with-RM” option, always take that, as alternative verb-nouns which do not include RM will tend to refer to the so-called “Classic” Azure components, aka Legacy, aka deprecated.


Moving on… I have more than 1 subscription:

AzurePowerShell2_02

I found that no permutation of Set-AzureSubscription actually set my subscription to the requested one, even though it claimed it had. For me at least, only this worked:

AzurePowerShell2_03

So, got that?
Login to your account: [AddAzureRmAccount]
Set your subscription to the required one: [Set-AzureRmContext]

AzurePowerShell2_04

And while on the basics, if PowerShell fails to recognise these commands, then run

Install-Module AzureRM

Install-Module Azure

to pick up both Resource Manager and Classic modules.

 Later… turns out that Login-AzureRmAccount is just an alias for AddAzureRmAccount

PowerShell: The basic template in ISE

The basic Ctrl-J template in the PowerShell ISE is a bit noisier than I need. This is all I want:

PSTemplate01

<#
.Synopsis
Drop set of databases
.Example
Drop-Database -DatabasePrefix “TestSet”
#>
function Drop-Database
{
[CmdletBinding(SupportsShouldProcess=$true)]
Param (
[Parameter(Mandatory=$true)]
$DatabasePrefix
)
Begin {
}
Process {
}
End {
}
}

Beyond that, you can use the New-IseSnippet function to make it part of the ISE, and use Ctrl-J to invoke it. Straight from msdn…

$m = @'
Param
(
  [parameter(Mandatory=$true)]
  [String[]]
  $<ParameterName>
)
'@

PS C:\>New-ISESnippet -Text $m -Title Mandatory -Description "Adds a mandatory function parameter." -Author "Kim Akers, Fabrikam Corp." -Force

So applying that principle to our very basic snippet at the top, we get..


$m = @'
<# .Synopsis Drop set of databases .Example Drop-Database -DatabasePrefix "TestSet" #>
function Drop-Database {
[CmdletBinding(SupportsShouldProcess)]
Param (
[Parameter(Mandatory)]
$DatabasePrefix
)
Begin {
}
Process {
}
End {
}
}
'@

New-ISESnippet -Text $m -Title DropDatabases -Description “A snippet to drop databases.” -Author “Dennis Wells, The Things I Do” -Force

Not so fast… So this was OK to execute…

PowerShellSnippetISE01

 

But then…

PowerShellSnippetISE02

Exception calling “Load” with “1” argument(s): “Unable to find or access the file C:\Users\denni\Documents\WindowsPowerShell\Snippets\DropDatabases.snippets.ps1xml”
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ISE\ISE.psm1:102 char:9
+ $psise.CurrentPowerShellTab.Snippets.Load($params[“FilePath”] …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException

Looking at the file location, the offending file is clearly there…

PowerShellSnippetISE03

, with this content:

PowerShellSnippetISE04

Executing the command line a second time also gives the error.

Hang on…

I recently rebuilt this machine.

If we try this…

New-Item -ItemType File -Path $profile -Force

PowerShellSnippetISE05

And from then on just refer to my old post here

PowerShellSnippetISE07

PowerShellSnippetISE08

PowerShellSnippetISE09

, and as admin..

PowerShellSnippetISE10

Now we shall try again…

PowerShellSnippetISE11

That means I expect to find a snippet with a title of [Mandatory] when I press Ctrl-J in the ISE.

PowerShellSnippetISE12

Hm, so that all works, but it doesn’t seem keen on the carats in the $<parametername>. Fine, don’t care.

I won’t bother with the evidence, but following that little trial, I then ran the DropDatabases snippet through the New-ISESnippet command, and it also appears in the selection list, and can be invoked.

No matter how irritating it is to get these automation-related things working, they SO pay you back for the effort.

PowerShell: change default prompt

reallThanks here

PSPrompt01

 

Now PS has setup the $profile file in the right location, edit in Notepad:

PSPrompt02

function Global:prompt {“PS [$Env:username]$PWD`n>”}

PSPrompt03

 

Oh yes, this is a new machine…

PSPrompt04

 

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

(and remember you have to run this as admin)

I’d say that’s a pretty useful prompt – the backtick gives you back your real estate once you know the directory name – thanks StackOverflow person

 

PSPrompt05