Monthly Archive

Categories

PowerShell

Fibonacci series

Today’s date 23 November is 1123 id written as month then day. Those numbers are the start of the Fibonacci series. So as its Fibonacci day here’s a quick PowerShell function to create a Fibonacci series.

 

function new-fibonacci {
param (
[int]$numberofelements = 10
)

## first 2 elements

$ell1 = 1
$ell1

$ell2 = 1
$ell2

$i = 2
while ($i -lt $numberofelements){
$elnext = $ell1 + $ell2
$elnext

$i++
$ell2 = $ell1
$ell1 = $elnext

}

}

 

The function has 1 parameter which determines the number of elements of the series you want to produce.

 

The first two elements are hard coded as being equal to 1 and output from the function.

 

The bulk of the work is done in the while loop – deliberately chosen to test at the top of the loop.

 

The next element in the series is the sum of the current last and next to last elements. The counter is incremented, the current last element becomes the next to last and the newly calculated element becomes the current last element.

 

If you just want the numbers displayed

PS> new-fibonacci

 

will display the first ten elements

To get a specific number of elements

PS> new-fibonacci -numberofelements 17

 

If you want the numbers in an array for further processing

PS> $fib = new-fibonacci -numberofelements 17

 

Enjoy.

Resolve-Path

Resolve-Path is a cmdlet I haven’t used much – if at all – so I thought I should have a look at it.

At an arbitrary point in the file system:

PS> Get-Location

Path
----
C:\Scripts\Modules\Coordinates

 

.. Indicates the next level up

PS> Get-ChildItem -Path ..

Directory: C:\Scripts\Modules

etc

 

..\.. indicates two levels up

PS> Get-ChildItem -Path ..\..

Directory: C:\Scripts

etc

 

Resolve-Path will resolve any use of wildcards and other characters with meaning into the full path

PS> Resolve-Path -Path ..

Path
----
C:\Scripts\Modules

 

PS> Resolve-Path -Path ..\..

Path
----
C:\Scripts

 

Note that

PS> Resolve-Path -Path .

Path
----
C:\Scripts\Modules\Coordinates

 

is effectively the same as Get-Location

Ad hoc development

I was having a discussion about how people can learn PowerShell at the recent UK PowerShell day and mentioned ad hoc development. Surprisingly, no-one really knew what I meant.

 

Ad hoc development is a concept more than a development type. It was used extensively back in the days of PowerShell v1 and v2 but seems to have dropped off the radar these days.

 

Many people seem to learn the PowerShell language – either from a class or a book – but then don’t have any idea how to put that into practice. This is a failing of our teaching methods. Too many times I’ve seen people asking for help because they’ve dived into trying to create huge complicated scripts, or modules, and don’t have the background knowledge or experience to actually get the code to work.

 

Ad hoc development is one approach to moving from a basic knowledge of the PowerShell language to coding production level scripts and modules.

 

The starting point is the command line and working interactively. Use individual cmdlets or even a pipeline of cmdlets. If you find you’re using the same pipeline a lot then save as a script. Later, as you learn more you can parameterise the script, add all the production bells and whistles and even turn it into a module.

 

PowerShell is a huge beast these days with many parts you probably don’t need to start with. Use what you need now and add to your code as you learn rather then trying to jump right into a big complicated project. In the long run you’ll learn faster and end up getting more done with less frustration.

Hidden files

If you suspect there are hidden files in a folder you can use the Attributes parameter to discover them:

PS> Get-ChildItem -Path c:\test -Attributes H

OR

PS> Get-ChildItem -Path c:\test -Attributes h

OR

PS> Get-ChildItem -Path c:\test -Hidden

 

If you want to see all files irrespective of them being hidden use Force

PS> Get-ChildItem -Path c:\test –Force

 

which will also show system files.

There are a number of ways to make a file hidden but the most generic is:

PS> Get-ChildItem -Path C:\test\Newoutdata03.txt | ForEach-Object {$_.Attributes += 'Hidden'}

 

You can modify the criteria used to define the files passed into Foreach-Object by using the path, filter, exclude or include parameters of Get-ChildItem. The Hidden attribute is set while preserving the other attributes.

To remove the Hidden attribute:

PS> Get-ChildItem -Path C:\test\ -Hidden | ForEach-Object {$_.Attributes -= 'Hidden'}

 

The Hidden attribute is removed whilst preserving other attributes. The files passed into ForEach-Object can again be filtered with the Get-ChildItem parameters.

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

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

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

Windows Terminal v0.4.2382.0

Windows Terminal v0.4.2382.0 has ben released to the Microsoft store. if you have Windows Terminal installed it should automatically update for you.

 

Copying out of a window using the keyboard shortcuts now works – thank you – that makes the whole thing much more usable.

 

I can’t seem to find a way to make the font have a bold face which is really good for demonstrations.

 

Windows terminal automatically picks up my Windows v6.2 and Windows PowerShell v5.1 instances as well as WSL and the command line. PowerShell v7 previews are ignored but could be configured manually. Given the rate of change in the preview versions not sure that its a good idea to add them to the terminal setup.

 

So far the experience is reasonably positive for a product still under development.

 

I’d recommend you give it a try if you haven’t already done so.