Browsing:

Category: Code

Powershell Profile

Here is a quick and dirty way to create a Powershell profile.

First, start the Powershell terminal and type:


new-item $profile -Force
notepad $profile

An empty profile file appears, and you can type the commands you want to run at start up:

cd ~/Documents

Fortune cookie

If you want to be greeted with a fortune cookie, add the following line:

$fc = ((Invoke-WebRequest https://gdgnoco-fortune.appspot.com/api/fortune).content) | ConvertFrom-Json
write-host `t `"$($fc.fortune)`"

Thanks to the Google Developers Group Nort Colorado for hosting the api!

Some nice ASCII art

Finally, add a nice ASCII picture to spice up your day. I found one here. Clone the repo and save Get-MOTD.ps1 in your $env:HOMEPATH\Documents\WindowsPowershell\ directory.

Your $profile file will look like this:

. $env:HOMEPATH\Documents\WindowsPowershell\Get-MOTD.ps1
Get-MOTD
write-host ""
write-host ""
$fc = ((Invoke-WebRequest https://gdgnoco-fortune.appspot.com/api/fortune).content) | ConvertFrom-Json
write-host `t `"$($fc.fortune)`"
write-host ""
cd ~

And it if you start the Powershell terminal you will be greeted like this:

2016-04-25 12_57_37-Windows PowerShell

Tip: don't do this in ISE. Also, install posh-git and follow the profile adjustments described on their github site.


My predictions for 2016 and beyond

For the first time a post not about coding or information technology, but about my predictions for 2016 and beyond. Here goes. These are going to get huge in 2016:

  • Blockchain is going to get momentum. Blockchain helps us solve the problem of the trusted third parties (like banks), which have created an inefficient society with has led to inequality. Blockchain is a technology which allows us to keep a ledger together. The authority to control, to divide and to conquer belongs to us!

 

  • Soylent, the food replacement. When I first read about Soylent it was still a crowdfunding initiative from Rob Rhinehart at Kickstarter in 2013. Soylent is open source, so you can create your own. But you can also buy it from several suppliers (mostly in the Netherlands funny enough). I have tried Joylent myself and I must admit it is rather tasty. Now I'm eagerly awaiting my Jakefood Light shipment. What is so disruptive about Soylent? Well, it is a food drink that contains only what the body needs. Better than pizzas and hamburgers for sure. For just 9 euros per day.
    Imagine, this can decrease our ecological footprint. It might be a solution for world hunger as well.

 

  • Blue Origin and Spacex. Well, they are inventing rockets that can be re-used. Space travel will be cheaper. It allows us to dream about a future where we can colonize Mars. And this dream can become reality.

  • Ok, I have to admit. Microsofts Continuum is pretty cool. Imagine the situation where you only need to own a smartphone. It will become a desktop computer if you attach it to a dock.  Now we only have to wait until applications become Continuum aware. (But I'm not sure if I would ditch my Macbook).

 

So, there you have it. These are my predictions for the future. They will be big. Maybe not so in 2016, but we are well on our way.

 


ASP.NET 5 getting started from scratch

Let's see how ASP.NET 5 works and discover how we can build a basic website with an API and an Angularjs frontend. Just like we're used to doing with Node.js. Let's see how the Microsoft way compares. Maybe I'm a little late because the current release seems to be 1.0.0.-rc2 on Github. So there's tons of info on the Internet already. And here is my bit as well. 🙂

I will use the Visual Studio Community Edition and I've got a VirtualBox VM running. And let's also host the app in Azure and see how we can collaborate using the Visual Studio Online tools.

First, select File, New, Project and select the Empty ASP.NET 5 template:

aspnet5_1

 

 

 

 

 

 

 

Let's debug and run it immediately!

aspnet5_2

 

 

 

 

Cool! Now where did that came from?

Startup.cs

Startup.cs is the entrypoint for an ASP.NET 5 application. I feel it compares to the app.js or index.js in an Express application which requires all the dependencies needed for the application. It is what the Global.asax was before. There are 2 sections: ConfigureServices and Configure. I think the comments describes their purposes really well:

public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {

           
            app.UseIISPlatformHandler();
              
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });             
        }

        // Entry point for the application.
        public static void Main(string[] args) => WebApplication.Run(args);
    }

Adding a static page

If we follow along the Express.js workflow, I would now need to add the possibility to add a static file (index.html). And I would need to plug that in to the Startup.cs. And I would need to install the dependencies in a package.json sort of file:

And that is correct. There is a project.json file in the solution and I need to add a dependency to Microsoft.AspNet.StaticFiles. Add it like I did at line 10. (It has intellisense, cool!)

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

Now I can add an index.html file in wwwroot and edit Startup.cs. Delete everything in the Configure method and add 'app.UseStaticFiles();'.

 public void Configure(IApplicationBuilder app)
        {
            app.UseStaticFiles();
        }

Run the app and now this page is served:
aspnet5_3

 

 

 

 

But of course I don't want to type in the URI. I want the web app to server default files.
Then it seems I still need to add middleware to serve the index.html. Add 'app.UseDefaultFiles(); to the Configure method in Startup.cs:

public void Configure(IApplicationBuilder app)
        {
            app.UseDefaultFiles();
            app.UseStaticFiles();
        }

You can find a great explanation here.

So great! That means I can go ahead and add Bootstrap and Angular and that I can write some serious api in C#.

BTW, if you want to know how Microsoft (and the community I must add) has envisioned an ASP.NET 5 web project, please try the MVC web app template.


Linked server #Back to Basics

Really back to basic, but here is another example how to move data from one server to the other in a scheduled job.

First make sure, when you use an sql account to connect both servers, the account exist on both ends of the line.

Now we create a linked server, after all my former tutorials, I don't have to go into detail, either script it or use SSMS gui, un this case I'm using it for a MSSQL to MSSQL this is easy peasy.

LS_make

Create a database and table if it doesn't exsist on the server you want to insert the data in.

If all is setup, write a simple INSERT INTO script:

INSERT INTO [FunFactory_II].[dbo].[Accounts_NAWT]
(AccountNumber]
,[Name]
,[Street]
,[PostalCode])
SELECT
[AccountNumber]
,[Name]
,[Street]
,[PostalCode]
FROM [FunFactory_I].[dbo].[BSN].[Accounts]

And the last step is to create a job, to start the import automatically and schedule it to run on a given time.

This is my really #back to basic linked server tutorial, but you can add and tweak functions into the script and built fully automated solutions for your data.

For example a scheduled sql job to insert data from the linked server.
insertIntoFF


Connect your Azure database from your local SSMS #Error 40615

Ofcourse we wanna access our fresh imported database on Azure locally!

And this is so easy if you just follow these simple steps.

In order to be able to access the database located on your Azure cloud, you have to know the Azure address, which you can find on the Windows Azure Management Portal. Click the database icon and open up the database. Here you will find the connectionstring.

azure_link

Next step is to make sure Azure knows your device by adding it to the trusted devices in Azure, if your device is unknow to Azure it will bring you the following message:1error-whenlocal_connection

you can add the device by going to the database icon in Azure and click the CONFIGURE tab. Here you add and save your device IP.2local_adTosavelist

Now you can login to your Azure cloud with the credentials you made while creating the database on Azure.
3Local_db connect