Asynchronous PowerShell Profile Loading

This script is meant to go in your PowerShell $PROFILE (\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1) file.

It loads the profile in a non blocking fashion allowing long running scripts like this quotation one to work and still enabling access to use PowerShell while it is being loaded in the background. Cross dependent scripts can’t be loaded using this as dependencies cannot be determined.  To add more startup items just add more {Start-Job -ScriptBlock {} -Name “StartUp#”} items before the timer section. This is mostly unlimited.

#Example quote script

$quoteScript = {
$test = Invoke-WebRequest http://www.quotationspage.com/random.php3
$what = $test.ParsedHtml.getElementsByTagName("dl")
$childNodes = $what[0].childnodes
$countme = 0
foreach($childNode in $childNodes){
    if($childNode.nodeName -eq "DT"){
        $quote += $childNode.innerText
    }

    if($childNode.nodeName -eq "DD"){
    #Match and remove - More  quotations *
        $quote += ($childNode.innerText -replace '- More quotations on:\s+\S+', '')
        $quote += "`n`n`n"
        $countme++
    }
    if($countme -gt 2){ break}
}
Write-Host "`n $quote" -ForegroundColor Cyan
}

#Non blocking timer object
Start-Job -ScriptBlock $quoteScript -Name "StartUp1"
$timer = New-Object System.Timers.Timer
$timer.Interval = 1000
$timer.AutoReset = $true
Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier "StartUp0" -Action {
    $jobs = Get-Job -Name "StartUp*"
    if($jobs.count -gt 1){
        foreach ($job in $jobs){
            if($job.State -ne "Running"){
                Receive-Job $job.Name
                Remove-Job $job.Name
            }

        }
    }
    else{
        $timer.stop()
        Unregister-Event "StartUp0"
        Remove-Job "StartUp0"
        Write-Host  "Asynchronous profile load completed" -ForegroundColor Green
    }
}
Write-Host "Asynchronous profile load starting ..." -ForegroundColor Yellow
$timer.Start()

Install Arch-linux on Raspberry Pi From Windows

The Needs

  • Raspberry Pi 3
  • Memory Card
  • USB Drive
  • Power
  • A screen
  • A Keyboard
  • Internet

The Downloads

Download older image from Raspberry Pi archive http://downloads.raspberrypi.org/arch/images/

Latest available is http://downloads.raspberrypi.org/arch/images/arch-2014-06-22/ArchLinuxARM-2014.06-rpi.img.zip

Latest root file system from http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-latest.tar.gz

on windows you need both. Alternatively this is just the boot sector from an image that works

Download 7-zip http://www.7-zip.org/ – This is used to extract the downloaded image file

Download Win32 Disk Imager from https://sourceforge.net/projects/win32diskimager/files/ – This is used to load the downloaded image to a memory card.

To just make it work this is the product of this guide.

Copying the Image to the SD card

win32diskimager

Use Win32 Disk Imager to write the extracted ArchLinuxARM-2014.06-rpi.img to the memory card. Be careful  at this step because if you select the wrong drive you can lose information or cause your PC to be unusable. This process should take somewhere between 3 – 10 minutes depending on the speed of your SD card.

A new drive should show up with a few file. Delete everything and copy boot from the latest image to boot on the SD Card.  Overwrite everything.

Then change the following line in /boot/cmdline.txt to make it work. This is already done in my boot sector download

Original (Truncated for clarity):

root=/dev/mmcblk0p2 rw rootwait console=ttyAMA0,.................

Modified (Truncated for clarity):

root=/dev/mmcblk0p5 rw rootwait console=ttyAMA0,..................

You can now insert the card into the Raspberry Pi. Attach it to your screen and power it up.

If everything goes well you have working old system.

If you see Kernel Panic, check the cmdline.txt for errors.

If everything works and you see this:

Arch Linux 4.4.23-1-ARCH (tty1)

alarmpi login:

you are ready to start getting to the latest Arch.

Round 1

Attach a network cable and insert the USB Drive. To save some time you can copy the ArchLinuxARM-rpi-latest.tar.gz downloaded before onto the USB Drive before this step.

We will now use the old image to format the USB Drive similar to the instructions at archlinuxarm.org with a few twists mainly because we do not hav mkfs.vfat  on the image downloaded. Then we will create a disk image, copy that to your Windows PC and finaly use Win32 Disk Imager to write your own image to the SD Card.

login as: root
root's password: root

[root@alarmpi ~]# mount /dev/sda1 /mnt/
[root@alarmpi ~]# cp ArchLinuxARM-rpi-latest.tar.gz /root
[root@alarmpi ~]# umount /dev/sda1
[root@alarmpi ~]# fdisk /dev/sda

In the fdisk prompt:

  • 0 – clears old partitions
  • p – make sure it is clear
  • n – New Partition
  • p – Primary Partition
  • 1 – First Partition
  • ENTER – Accepts Default
  • +100M – Make it 100MB big
  • t – Set partition type
  • c – W95 FAT32 (LBA)
  • n – New Partition
  • p – Primary Partition
  • 2 – Second Partition
  • ENTER – Accepts Default
  • ENTER – Accepts Default
  • w – Write Partition Table
[root@alarmpi ~]# dd fi=/dev/mmblck0p1 fo=/dev/sda1
[root@alarmpi ~]# mkfs.ext4 /dev/sda2
[root@alarmpi ~]# mkdir /mnt/root
[root@alarmpi ~]# mkdir /mnt/boot
[root@alarmpi ~]# mount /dev/sda1 /mnt/boot
[root@alarmpi ~]# mount /dev/sda2 /mnt/root
[root@alarmpi ~]# bsdtar -xpf ArchLinuxARM-rpi-2-latest.tar.gz -C /mnt/root
[root@alarmpi ~]# mv /mnt/root/boot /mnt/boot
[root@alarmpi ~]# umount /dev/sda1
[root@alarmpi ~]# umount /dev/sda2
[root@alarmpi ~]# dd fi=/dev/sda fo=/dev/root/alarm.img

You can now copy this image to your computer. Either with another USB drive or by using scp/Filezilla to copy it via SFTP.

Once the image is on your computer use Win32 Disk Imager to write the new Image to the same or new SD Card. This SD Card is now ready to boot your Raspberry Pi and includes Wifi capabilities.

Round 2

On the console with a screen and keyboard.

login as: root
root@xxx.xxx.xxx.xxx's password: root

Create a file for wpa_supplicant

[root@alarmpi ~]# nano /etc/netctl/wlan0
Description="Wifi"
Interface=wlan
Connection=wireless
Security=wpa-configsection
IP=dhcp
WPAConfigSection=(
'ssid="wifiName"'
'psk="Password"'
)

Test the wireless configuration

[root@alarmpi ~]# netctl start wlan0

You can check by running

[root@alarmpi ~]# ip addr

.....................
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
 link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
 inet xx.xx.xx.xx/xx brd xx.xx.xx.xx scope global wlan0
................................

If it is working enable it at startup

[root@alarmpi ~]# netctl enable wlan0

You can now reboot the Raspberry PI to ensure it is working as expected.

[root@alarmpi ~]# reboot

After a successful reboot check SSH is working and then your system is ready to go headles(root cannot login to ssh directly)

login as: alarm
root@xxx.xxx.xxx.xxx's password: alarm

 

and upgrade to the latest and greatest. (This takes time)
I had to change the mirrors to the SA one as the Geo-IP based mirror was timing out the whole time.

[alarm@alarmpi ~]$ su
Password: root

[root@alarmpi ~]# nano /etc/pacman.d/mirrorlist

Replace

Server = http://mirror.archlinuxarm.org/$arch/$repo

With – obviously do this with something that makes sense for you from here

Server = http://za.mirror.archlinuxarm.org/$arch/$repo

Upgrade pacman db to latest format

[root@alarmpi ~]# pacman-db-upgrade
[root@alarmpi ~]# pacman -Syu --force

Reboot

[root@alarmpi ~]# reboot

And you are in business. Almost.

Increase the security

Regenerate the SSH keys to ensure uniqueness

[root@alarmpi ~]# ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
[root@alarmpi ~]# ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa
[root@alarmpi ~]# ssh-keygen -f /etc/ssh/ssh_host_ecdsa_key -N '' -t ecdsa
[root@alarmpi ~]# ssh-keygen -f /etc/ssh/ssh_host_ed25519_key -N '' -t ed25519

Change the passwords

[root@alarmpi ~]# passwd root
[root@alarmpi ~]# passwd alarm

 

That’s it

Now you should have a working system that you can do what you please with and expand on. More guides may follow but most likely there will be more snippets than complete guides.

Powershell One Liners

Create Profile (Only if it doesn’t exist)

if(-not (Test-Path $PROFILE)){new-item -Path $PROFILE -Type file -force}

GnuCash Finance Quote Fix

I use GnuCash to track my finances. It has a steep learning curve, if you are not an accountant who knows double entry, but gives a lot of control and is opensource.

I use the automatic stock price updater for the cents I have in savings but alas being South African I found the ZA_UnitTrust.pm module in Perl Finance-Quote broke shortly after I started using it. I create a fix and that was that. Not being a Perl guy I did not feel comfortable to submit it to anywhere or even really attempted to figure out where.

It has been a while and no one else submitted a fix so I submitted my fix.

For now the way I do it is install GnuCash, Strawberry Perl and Finance Quote. Then I replace the relevant files in ‘Path\to\perl\site\lib\Finance\Quote’ with my own and then it works.

ZA_UnitTrusts.pm
ZA.pm