Monthly Archive

Categories

Monthly Archives: September 2019

Receive-Job Keep parameter

The Receive-Job Keep parameter is required if you want the data contained in the job to remain available. if you don’t use the –Keep parameter (a switch) the data will be deleted.

 

Its a pain to remember to use the Keep parameter. I’ve been working with jobs a lot just recently and the number of times I had to rerun jobs because I forgot to use Keep doesn’t bear thinking about.

 

I finally got round to adding it to the default parameter values.

This line in my profile

$PSDefaultParameterValues = @{'Install-Module:Scope'='AllUsers'; 'Update-Module:Scope'='AllUsers'; 'Receive-Job:Keep' = $true}

 

ensures I won’t forget ever again. Because Keep is a switch you have to set the value to true.

PowerShell Day UK 2019 slides and code

Had a great day at PowerShell Day UK 2019. The PowerShell Day UK 2019 slides and code for my session are available at https://github.com/RichardSiddaway/PSDay2019.

UK PowerShell day is Saturday

The UK PowerShell day is Saturday – 28 September  - two days away. If you want to be there tickets are still available - https://psday.uk/

 

If you’re going – say hello

 

See you there

Ternary operator

PowerShell v7 preview 4 adds a Ternary operator to PowerShell.

 

A ternary operator is a way to provide shortened coding for a simple if-else block. Its an operator that takes three operands rather than the usual two hence the name ternary.

for example

PS> $a = 5
PS> $b = 3
PS> if ($a -gt $b){'Greater'}else{'Not greater'}
Greater

 

Using a ternary operator this becomes

PS> $a -gt $b ? 'Greater' : 'Not greater'
Greater

 

The generic form is

<condition> ? <value if true> : <value if false>

 

The ternary operator is an experimental feature so needs to be enabled

PS> Enable-ExperimentalFeature PSTernaryOperator

 

and PowerShell restarted.

 

If I use the new operator I think its going to be when I’m working interactively. For scripts and modules I prefer to use the more verbose if statement as its easier to read and understand.

Get-ExecutionPolicy

I’ve used Get-ExecutionPolicy since PowerShell v1 and never stopped to think about it. The cmdlet normally returns just the current policy

PS> Get-ExecutionPolicy
RemoteSigned

 

However, if you dig a little deeper

PS> Get-ExecutionPolicy -List

Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine RemoteSigned

 

What you’re seeing is the LocalMachine policy.

 

You can use the Scope parameter on Set-ExecutionPolicy to create a more granular approach to execution policy if required

PowerShell v6.2.3

PowerShell v6.2.3 is available from https://github.com/PowerShell/PowerShell/releases

 

It fixes a debugging performance issue and updates the .NET SDK and runtime framework version.

 

Similar fixes are available in v6.1.6 released at the same time

Hyper-V VM IP address

Saw a question about getting the Hyper-V VM IP address.

 

One thing with Hyper-V is that the VM has to be running for you to retrieve the IP address.

 

Once you VM is running you can get the IP address

PS> Get-VM -Name W19ND01 | select -ExpandProperty NetworkAdapters | select VMname, Name, IPAddresses

 

You’ll get the IPv4 and IPv6 address returned