Sergey Maskalik

Sergey Maskalik's blog

Focusing on improving tiny bit every day

Get-Help

Powershell has great documentation inside of the shell, including examples. To turn on complete documentation add -full argument.

Get-Help [command name or topic]
Get-Help [command name or topic] -full

All powershell commands start with a verb and followed by a noun.

Get-Command

To get a list of commands:

Get-command

All commands related to process or noun

get-command -noun process

All commands that have a verb

get-command -verb get

Commands

List of processes

get-process

To stop process and get help on the command, to see examples.

get-help stop-process -full	

What if?

Almost all commands have -WhatIf argument, you can try a command before executing.

Pipe

One of the most powerful features. Take output and “Pipe” to the next command get-process sort-object
get-process | sort-object -property Id

To see the list of all available properties from the command

get-process | get-member

Get-Member

Think of it when you have a question, what do I have, or what does this object have.

Filtering and blocks

get-process | where-object { $_.processname -eq 'powershell' }

Curly braces represent the filter block. The $_ represents the current item.

get process | where-object { $_.CPU -gt 1 } | foreach-object { $_.processname + " is over threashold " }

Outputting to the file

get process | where-object { $_.CPU -gt 1 } | foreach-object { $_.processname + " is over threashold " } | out-file cpuinfo.txt

Execution policy

Get-ExecutionPolicy

You won’t be able to run any scripts if the scripts are restricted. To change execution policy.

Set-ExecutionPolicy remotesigned

Scripts

Once you have your execution policy set to remotesigned you can execute scripts that were created on the local machine. Save scripts with .ps1 extension. And script can be executed by providing a path to the script in the powershell. Like ./somescript.ps1. First dot stands for the current directory.

Great Topics with Get-Help

Get-Help About_Execution_Policies
Get-Help About_Operators
Get-Help About_Common_Parameters
Get-Help About_Pipelines
Get-Help About_Scripts
Get-Help About_*

#Creating scripts to start applications and more.

Typing is better than clicking. Automate tasks that are common with windows explorer.

Get your current location

get-location
$pwd
gl

To change your location:

set-location /.documents
chdir ..
cd ./documents

Push location will save your current location before moving to the new location. Once you are done use pop-location to get back to where you were before.

push-location /
pushd ~/desktop
pop-location
popd

Common use is to control the location inside of the script

Drive -> Folder -> file

Powershell treats many things like drives, for example registry, functions and system drives.

get-psdrive

You can cd into certificate stores

cd cert:
dir

Environment

cd env:
dir

Registry

cd hkcu:
dir

Push and pop work on custom drives.

Item commands

This are very useful.

get-command *item

Invoke-item

Equivalent as double click, will open with default registered file. Alias

ii message.txt

Will open folder if set to the folder.

http://bit.ly/psphere to add to the windows explorer open in powershell

Automating projects

cd ~/documents/project
push-location "~/documents/project/test"


function push-project( $project ) 
{ 
	# 1: navigate powershll to the project folder
	pushd "~/document/project/$project"
	# 2: open windows explorer at the project folder
	invoke-item .;
	# 3: open any solution found in the project folder
	invoke-item *.sln;
}

Profiles

Powershell saves scripts into the users profile

$profile

Create a profile script file

new-item -type file -path $profile
invoke-item $profile

You can add the function to your user profile. And you can add an alias for the function

new-alias -name pp -value push-project

There are 4 profile scripts in powershell

$profile | select *

Get-Help *Location
Get-Help *Item
Get-Help About_Providers
Get-Help About_Functions
Get-Help About_Profiles

Configuring

System-level configuration, for the entire PC c:\Program Files (x86)\Git\etc\gitconfig or ~\.gitconfig on MAC. Accessed by:

git config --system

User-level configuration, stored in c:\users<name>.gitconfig

git config --global

Repository level configuration, stored .git/config

git config

Show all global options

git config --global --list

Setting properties on global

git config --global user.name
git config --global user.email

Configuring core editor

git config --global core.editor "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

Configuring beyond compare

git config --global diff.tool bc3
git config --global difftool.bc3.path "c:/program files (x86)/beyond compare 3/bcomp.exe"
git config --global difftool.prompt false
git config --global merge.tool bc3
git config --global mergetool.bc3.path "c:/program files (x86)/beyond compare 3/bcomp.exe"
git config --global mergetool.prompt false
git config --global mergetool.keepbackup false
git config --global mergetool.keeptemporaries false

Configuring auto correct for the git commands

git config --global help.autocorrect 1

Use colors to show a lot of git information

git config --global color.ui auto

Auto carriage return line feed. False is for windows only will submit it as is. True will convert. Input is for mac, it will convert crlf into lf.

git config --global core.autocrlf false

To remove a setting

git config --unset user.name

You can also edit the config files yourself.

Working Locally

Creating a local repository
git init
git status
git add file.txt
git status
git commit

Look at the history and see the commit SHA: git log

Add all modified/updated files

get add -u

Commit with message

git commit -m "Message"

To view the diffs between SHA1 hashes

git diff dd2929..333322

Easier, latest commit is know as HEAD

git diff HEAD~1..HEAD
git diff HEAD~1..

Add all files including untracked files

git add -A

Stage as two different commits, add specific files and then commit.

git add file1.txt
git commit
Deleting Files

Delete the file first. Then stage the deletion with

git add -u

Rename the file first then:

git add -A

To clear out unstaged changed to the README.txt file

git checkout README.txt

To do a global clear out, and remove all unstaged changes git reset –hard

To take out the last commit out of the repository and move back to the staging area, that way you can change that commit around

git reset --soft HEAD~1

make changes

git commit -m "reorganized commit"

To delete last commit and discard all the changes

git reset --hard HEAD~1

Remove unstaged files

git clean -n //what would it do
git clean -f //performs the operation
.gitignore

Create a file that will list all of the files and directories that need to be omitted.

Working Remote

clone repository

git clone https://....

see list of commits

git log

commits per line

git log --oneline

see number of commits

git log --oneline | wc -1

see the graph git log –graph

Authors and commit messages

git shortlog

Statistics about authors

git shortlog -sne

Look at the specific commit

git show HEAD
git show HEAD~10
git show 939393

Look at the remotes, origin is the git default where the repository came from

git remote
git remote -v //verbose shows remote  urls

To see the repository config cat .get/config

Display all the branches in the repository

git branch

Display all the remote branches

git branch -r

Look at the tags, known stable versions

git tags

To add a remote repository, is commonly down to evaluate patches and pull requests into your project

git remote add origin https://github.com/....

Once remote repository is added you can

git fetch  //all
git fetch origin

to see changes on remote repository

get log origin/master

Merge remote branch into the your current branch

git merge origin/master

Fast forward means, replay commits that happen if there were no other changes and move the HEAD

Shortcut for git fetch; git merge origin/master is

git pull

To set your branch to mirror some other remote branch, or remote tracking branch, or upstream tracking.

git branch --set-upstream master origin/master

If you can also specify without setting the remote like this. origin is the remote name and master is the remote branch.

git pull origin master

Git commit and add modified files together

git commit -am "Some message"

Push your changes to the remote git push

To remove the origin on the current branch

git remote rm origin

to tag a current version, or stable point

git tag v1.0.0

to create tag with the annotation

git tag -a v1.0

To create a tag with the signature, signed with the password

git tag -s v1.0

To verify a tag

git tag -v v1.0

To push tags to remote (by default git would not push tags)

git push --tags

Branching Merging and Rebasing with Git

list of the commits on the current branch with a graph

git log --graph --oneline

To visualize all branches, decorate applies the branch and tag information

git lot --graph --oneline --all --decorate

Adding aliases in the git config

git config --global alias.lga "log --graph --oneline --all --decorate"
git config --global alias.lg "log --graph --all --pretty=format:'%C(bold)%h%Creset -%C(auto)%d%Creset %s %C(green dim)(%cr)%Creset %C(ul)<%an>'"
git lga
git lg
cat ~/.gitconfig

To Create a new branch git branch feature1

Checkout new branch

git checkout feature1

Big difference between branches and tags is that tags always stay on the same commit where branches move along.

git checkout master

Create a branch from a specific commit

git branch fix1 9929292
git checkout fix1

To rename a branch is to move it

git branch -m fix1 bug1234

To delete a branch

git branch -d bug1234

To force delete a branch

git branch -D bug1234

To create a branch and checkout (shortcut) git checkout -b feature2

To get back a deleted branch. From a dangling commit, a commit that doesn’t have a home. These are kept for 30 days.

git reflog // log of all references
git branch bug1234 5a78c8b //with the first 8 of the SHA

To see head

git show HEAD

Save half completed work is to stash it. Temporary holding area for the changes that are not committed to the branch.

git stash
git status
git stash list //holding area
git stash apply //to pull the changes back
git stash pop //applies it and removes it from the list

Merging

Merge fiture1 into the current branch

git merge feature1
git branch -d feature1 //deletes unused branch

Fast forward merge, is just moving the branch label into the new location.

To resolve conflicts using the merge tool

git mergetool

Compare the repository to the staging area

git diff --cached

Rebasing

You can play back your commits on a certain branch rather creating a branch if there are no conflicts. Rebase current branch on top of master

git rebase master

You can also use mergetool to solve merge conflicts and then continue

git rebase --continue

Cherry Pick

If you need to get one commit and apply it on top of a branch.

git cherrypick sks939ss

#Working with remote

git fetch origin master //get latests
git push origin master

to push a branch to remote

git push origin v1.0_fixes

list remote branches

git branch -r

To delete remote branch, push to the remote without specifying the local name

git push origin :v1.0_fixes_remote_branch_name

Complete Configuration File

SPAs bring new fresh user experience

The web is constantly evolving. About 7 years ago we didn’t know what jQuery was and AJAX was mostly associated with the cleaning product. Now it’s 2013 and it is no longer enough to sprinkle simple AJAX calls to get some dynamic data without refreshing the page. Modern applications run completely in the browser and provide a very fast and intuitive user interface. In addition, it creates a new user experience that makes it feel fresh and vibrant, while going back to standard web sites with postbacks feel clumsy and slow. No wonder new startups are embracing this concept. It gives you an edge and can make or break a company when dealing with competition.

We also see a rise of backend technologies that focus on returning JSON and embracing RESTful web services. One of the example is recent addition of the ASP.NET Web API framework from Microsoft. And of course a numerous amount of javascript frameworks for building SPAs. Here is a list with comparison of many of them.

New concept, need same good programming principles

Since we are moving away from simple javascript and stepping into the realm of building complex applications in the browser we must apply time tested design principles for building maintainable, testable and readable software. We want our applications to be easy to change and understand. And the user interface get changed more frequently than anything else. Therefore it’s extremely important to have good design, follow patterns and separate your concerns.

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

  • Martin Fowler

Javascript libraries for building SPAs give you a nice starting point, but what I have discovered is that it leaves a lot of open questions and ability to shoot yourself in the foot when you don’t understand the concepts. There are however frameworks like EmberJs that will force you into doing things the predefined way, which would probably be a great starting point for beginners. But again it won’t give you as much flexibility of building a framework that works for you.

Foundation Ingridients

So where do we start, how do we begin this journey of re-inventing ourselves. Well what do you do when you start learning a new language? I usually pick up a good book on that language and learn the basics.

Javascript language

Since Javascript is the language of SPAs you as developer cannot longer get aways with simple functions or jQuery selectors. Just like any language other language you need to give it full credit and pick up a book and learn how to compose your objects, modules, how inheritance works and so on. Luckily there are excellent books out there. One of them that I thought was straight to the point and a must read is:

Learning the patterns

After learning the language, if you are not familiar with MV* patterns used most SPAs you will need to learn and understand them in order to know where things go. It helps that patterns have already been discussed thoroughly for other UI frameworks that are similar to SPAs like WPF and Silverlight. So there are plenty of resources and solutions that already have been hashed out. Rob Eisenberg has compiled a great list of resource on learning the presentation patterns.

Unit tests

Once you know how to separate your concerns and not to mix DOM access code with application logic with you will be able to greatly improve your code quality and maintainability by writing unit tests. You can start by going through a list of articles on testing here http://superherojs.com/

Paying your dues and evolving

Once you pay your dues and learning Javascript language along with patterns and principle and unit testing you will have a good foundation to make a maintainable application that will be easy to change, understand and will stand the test of time.

Finally on a more general note we as developers must learn to evolve. What’s awesome is that all the good programming principles that you’ve learned over the years can be applied at everything. It comes down to learning new patterns and language. And patterns knowledge would last a lot longer than frameworks or even languages. I’m excited to step on this road of learning and discovering. Are you with me?