Saturday, December 13, 2014

Powershell help - The three most important commands in the Windows PowerShell

Just starting out with the Windows PowerShell and I think the most important command are:

  • Get-help - first line support, what do I do?
  • Get-command - found the command, How do I use it?
  • Get-member - What does this do? List it's members.

Get-Help


It accepts wildcards and is not case-sesitive.
So,
get-help *service* 
will list all topics that have object in the topic.


You can find more info about the topic by asking for help on the name of the specific.
e.g.
get-help start-service
This help text is very useful but there's even more.

The full help can be found by using the verbose parameter, full
get-help start-service -full

Get-Command

get-command -noun service 
This will list all the commands with the word service in the name.

You can also look for a verb, e.g.
get-command -verb get


Get-member

To use this we need to use the pipeline command which is:
|
Basically, without explaining pipelining, the output from the first command is filtered into the second.

So. I have found that the command I am interested in is get-service.
I want to find the members of that service. So I pipe the command into the get-member command.
get-service | get-member
 Running the above we find the Properties and Methods for the command get-service


And one other thing:

-WhatIf

If you're unsure what a command will do, then add the -whatif parameter and let PowerShell tell you what will happen.

stop-process -name * -whatif 





Sunday, November 2, 2014

Use iBooks to read Kindle books on a Mac

Amazon has a lot more computer books than the iBook store so I've ended up with quite a few in Kindle over the years. I don't have a Kindle device, just the app for the Mac.
I don't like it. I never have liked it. It just irritates me. For a long time we had to use it because Apple in their infinite wisdom thought iBooks should only exist on the iOS devices but that changed a couple of years back so why am I not reading my Kindle books in iBooks?
Answer Digital Rights Management - DRM. The things that prevents copying even if you're coping it for yourself.

But there's a way around it.

1. Install Calibre http://calibre-ebook.com/download

This is an open source e-book management thing. It has the power to export e-books to other formats but it won't cope with DRM. We need to get rid of the DRM.

We need to add a free and open source plugin to Calibre but we install it in a slightly different way. For me it worked very easily and quickly.

2. Go to Apprentice Alf's page to download the tool. You'll need the Tools not the Alternative tools.

To download, be sure to press the Grey button and un-tick the tick box.




3. Ok now unzip the file somewhere.

4. Start Calibre.

5. Add the plug in.

  1. Select Preferences from the menu-bar.
  2. Select Change Calibre behaviour from the dropdown.
  3. Choose Plugins from the panel.
  4. Select the button: Load plug-in from file.
  5. Navigate the the unzipped files you downloaded.
  6. Open the DeDRM_calibre_plugin.
  7. Select the DeDRM_plugin.zip file.
  8. Choose Add etc and that's it.
No Add books into Calibre and will be DRM free. You can export to Epub and then add to your iBooks library.





The plugin is made by Apprentice Alf I think and there are all sorts of posts on the blog about this

Saturday, October 18, 2014

How to use special character in a Mac like umlaut, acute, tilde and circumflex


The Grave characters: À È Ì Ò Ù à è ì ò ù

Press the following two keys:  option ` and then the character we want.

The Acute characters: Á É Í Ó Ú á é í ó ú

Press the following two keys: option e and then the character we want.

The Circumflex: Â Ê Î Ô Û â ê î ô û

Press the following two keys: option i and then the character we want.

The Tilde: Ã Ñ Õ ã ñ õ

Press the following two keys: option n and then the character we need.

The Umlaut: Ä Ë Ï Ö Ü Ÿ ä ë ï ö ü ÿ

Press the following two keys: option u , and then the character we need.

Friday, September 5, 2014

How to use Interupts in the Raspberry Pi

Excellent.
Just found this.
Now we can do hardware interrupts with the raspberry Pi. A world better than polling.

http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio

How to connect Raspberry Pi to Mac using ethernet cable

Just a note because I did this once, then tried again and could quite remember it.

The default IP address of the Pi is 192.168.2.2

The default IP address of a home network is in the range: 192.168.0.nnn.

Open the Network preferences and set the IP address, subnet mask and DNS server as shown below.



The Airport WiFi is unchanged on my range and the Pi can share the internet via the Mac.

I have done this on Snow Leopard and Maverick. All good. The same should work on Windows I should imagine.

Use Ping in a terminal console window to check the network connection.

ping 192.168.2.2




Monday, August 25, 2014

Raspberry Pi - header pins and BCM pins

This image is taken from Meltwaters Raspberry PI Hardware.




import Rpi.GPIO as GPIO

#Set the GPIO to use the BCM device numbering
GPIO.setmode(GPIO.BCM)

#Set the GPIO to use the Rasperry Pi board header numbering.
GPIO.setmode(GPIO.BOARD)

BCM spec

The spec for the BCM2835 can be found here.


Sunday, June 22, 2014

What is the End of line key in OSX

This has been bugging me for years. What is annoying is that I have not bothered to find the keyboard shortcut for this. How can I be so pathetic.

At work for years it's been Windows and their low-tech, low quality plastic keyboards that are easy to be snobbish about and look down on. But there are two things straight away that I miss when on my Mac keyboard: the 'go to the end of the line' key, and the '#' (I'm using a UK keyboard so I'm not sure if the key is visible on other culture keyboards). I've known the '#' for a long time but not the other.


  • # - Option and 3 together
  • end of line - cmd and right arrow
  • start of line - cmd and left arrow
  • next word - option and right arrow
  • start of page - cmd and up arrow
  • bottom of page - cmd and down arrow
Ok, so here's other ones for text selection:
  • Select text to start of line - put SHIFT with the above line selection (SHIFT + CMD + LEFT ARROW)
  • Select text to end of line - as above but with right arrow.
Try up arrow and down arrow too. They work as expected.

ha! Finally got my act together. I've been using it all day.

Wednesday, December 11, 2013

Note to self - how to do many to many in Entity Framework code-first

I've probably done a handful of EF code-first projects and I always forget how to do the mappings. I always end up looking at old projects and try to figure out what went on. This is a good place to make notes. In my last project I had no idea what I was looking at. Old age has crept up.

I won't go over the whole DbContext Model classes thing,  but in case someone is reading this that isn't familiar, you make a your model classes for your application and they become mapped to your database entities in the Entity Framework ORM world.

So, for example say we have these two model classes amongst our many other model classes:
  • WatchingList
  • Thing


We would then create our DbContext class by creating a class and implementing from DbContext class. For example:

public class MyProjectDbContext : DbContext
{
   // in here we will map our model objects with DbSet<>
   public DbSet<WatchingList> WatchingLists { get; set;}
   public DbSet<Thing> Things { get; set;}

     ... and so on...
}

The above is where all the magic happens. Look elsewhere for a fuller explanation, but in short when we run our app after having done the above, we can then use LINQ to query the database objects, the database will be created for us if it doesn't already exist.

On another note, if it does already exist then we can configure our project to recreate the database on changes. I'm not going to write about that but it's great do developing though probably not great for production systems :)

Many to Many mapping

Anyway, to do the many to many mappings we'll need a

modelBuilder.Entity<WatchingList>()
      .HasMany((w => w.Things).WithMany()
      .Map(m => m.ToTable("WatchingList_Thing")
      .MapRightKey("ThingId")
      .MapLeftKey("WatchingListId"));

Basically,  ThingId and WatchingListId are the columns in the mapping table and the mapping table will be called  WatchingList_Thing

Abstract base classes

If we have some inheritance with these three model objects say,

  • Animal - maybe an abstract base class for the other two concrete classes listed here
  • Dog - inherits/extends the Animal class
  • Cat - inherits/extends the Animal class
If we have these three model classes declared with DbSet in the usual way we will get three tables with those names. We might want to have the table names in the database slightly different to reflect their relationship. We can do that by also overriding the OnModelCreating() method.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<Animal>().ToTable("Animal");
   modelBuilder.Entity<Dog>().ToTable("Animal_Dog");
   modelBuilder.Entity<Cat>().ToTable("Animal_Cat");
}
So we've controlled the naming of tables by EF.






Tuesday, December 10, 2013

XSRF Cross Site Request Forgery attack - ASP.NET MVC

This is a note on the topic that I've been meaning to write for a very long time. I'm a bit old, been around a bit and have worked with a whole range of people. The interesting thing about the XSRF is that I hardly hear it mentioned in the work place. I hope other folk have a better experience. But we just seem to be getting on with the list of functional requirements in time for the next deadline. Perhaps that why it's not talked about enough but it is probably the most 'popular' security vector being exploited by the nasty people.

A quick intro.

In simple terms:

  1. A friendly person A visits our happy site Happy.com and logs in. 
  2. Happy.com site authenticates him and gives him a cookie so that every time he visits us we know he's a good guy.
  3. Unknowingly, friendly guy A visits a bad site Evil.com.
  4. Evil site Evil.com will sneakily return a malicious form tag that will request to our Happy.com site. This malicious code will be allowed to access our server because it is attached to the authentication cookie we gave him and we think that it has already been authenticated.
  5. Bad things can happen.

This works because the authentication cookie is being transported with the request. 
The only way to prevent this is to ensure the form is created by the good user. We use an antiForgery token to help here. 


In the View we put the anti forgery helper:

<% using (Html.BeginForm()) {%>
    <% = Html.AntiForgeryToken() %>


In the controller action we use the ValidateAntiForgeryToken attribute:

[ValidateAntiForgeryToken]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, 

With this asp.net creates a crypto token that can't be created by the Evil user.



Sunday, November 24, 2013

Peter Doolittle talk on "working memory".

Working memory is the part of our consciousness that we are aware of.
1. Allows us to store immediate experiences and a little bit of knowledge
2. Allows us to reach into our long term memory and use it to achieve our current goal

We can remember about 4 things in Working Memory. It has a very limited capacity and we'll forget those things very quickly, seconds, unless we do something with them. We need to process them as they happens.

Working memory Capacity has a long history and is related to some positive effects. People that have relatively high Working memory capacity tend to be good story tellers and tend to be able to reason at high levels.

We need to use Working Memory efficiently to handle the stream of life that comes at us to make sense of it and to achieve our goals. Working memory is really quite small and this talk sheds some light on this interesting architecture. That's how I view it - as an architecture. I think the RAM, cache, disk storage solution in computing is a kind of metaphor for this. But, did our understanding of computer architecture allow us to pin this solution on our mind when actually we are just approximating the solution. Anyway, it's a nice talk.

http://www.ted.com/talks/peter_doolittle_how_your_working_memory_makes_sense_of_the_world.html