one man loves powershell once he failed

Post on 15-Jan-2015

145 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

1

One man loves PowerShellonce he failed

@gab_km12th July, 2014

Japan PowerShell meetup #3

2

Who am I?

● an engineer works in an IT services company

● use .NET mainly, love F# in private.

● be interested in Python, D language, ...

● and PowerShell!

3

The topics I will tell you about

● I was bad at PowerShell, but now feel to fit it

● I found the way how to overcome to struggle

4

Once upon a time...

5

Once upon a time...

In this tweet, I have started PowerShell 2 years ago from this time(24/Mar/2011).

6

What made me hard for PowerShell

There were 2 cases I had trouble using PowerShell:

● I seemed there are a lot of differences in command system between PowerShell and bat.

● I could not some operations shortly in PowerShell while I could them in good old bat.

7

In these days...

8

In these days...

I tweeted this in the spring of 2013.It makes me improve doing in PowerShell that I tried to write some PS1 things for a year.

9

Case 1

First I wrote bat files which were executed in booting my PC to run some useful tools like a mailer, connect file servers or do something convenient.

10

Case 1

First I wrote bat files which were executed in booting my PC to run some useful tools like a mailer, connect file servers or do something convenient.              ↓When I was learning Python, I rewrote these booting scripts in Python, which helped me to learn it.

11

Case 1

First I wrote bat files which were executed in booting my PC to run some useful tools like a mailer, connect file servers or do something convenient.              ↓When I was learning Python, I rewrote these booting scripts in Python, which helped me to learn it.              ↓I tried rewriting them in PowerShell again for my learning PowerShell.

12

Case 2

13

Case 2

Chocolatey is one of the best package managers for Windows and I like it. As I should connect using a proxy server in my workplace, this package manager fails installing. :-(So I sent a pull request to achieve 'chocolatey install' with a proxy server.

14

Case 2

Chocolatey is one of the best package managers for Windows and I like it. As I should connect using a proxy server in my workplace, this package manager fails installing. :-(So I sent a pull request to achieve 'chocolatey install' with a proxy server.

* As of 12th July, 2014, this pull request is not merged.

15

Case 3

16

Case 3

# Rename all mp4 files with “Last Write Time”Get-ChildItem |Where { $_.Name.StartsWith("MAKERNAME") } |ForEach { Rename-Item $_ -newname ($(Get-ItemProperty $_).LastWriteTime.ToString("yyyyMMdd_hhmm") + ".MP4") }

17

What gave me the power to the shell

I overcame the troubles in using PowerShell with seeing the way to do things above.

● File specification and execution

● File operation

● Pipeline

18

What gave me the power to the shell

I overcame the troubles in using PowerShell with seeing the way to do things above.

● File specification and execution

● File operation

● Pipeline

Even if you are a person who are not good at PowerShell, the abilities to do the 3 things above make you break down the obstacle.

19

● File specification and execution

● File operation

● Pipeline

20

File specification and execution

PS> notitle.ps1

This is a script file named 'notitle.ps1' and created in your current directory.You input like above to execute it.If it does well, your terminal shows you “Hello, there!” as output message.

21

PS> notitle.ps1notitle.ps1 : 用語 'notitle.ps1' は、コマンドレット、関数、スクリプト ファイル、または操作可能なプログラムの名前として認識されません。名前が正しく記述されていることを確認し、パスが含まれている場合はそのパスが正しいことを確認してから、再試行してください。発生場所 行 :1 文字 :1+ notitle.ps1+ ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (notitle.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundExceptionPS>

File specification and execution

22

PS> notitle.ps1notitle.ps1 : 用語 'notitle.ps1' は、コマンドレット、関数、スクリプト ファイル、または操作可能なプログラムの名前として認識されません。名前が正しく記述されていることを確認し、パスが含まれている場合はそのパスが正しいことを確認してから、再試行してください。発生場所 行 :1 文字 :1+ notitle.ps1+ ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (notitle.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundExceptionPS>

You will see this error message when you simply replace from .bat commands to .ps1 commands.

File specification and execution

23

To specify files(1)- Absolute path

PS> C:\my\posh\notitle.ps1

24

To specify files(1)- Absolute path

PS> C:\my\posh\notitle.ps1Hello, there!PS>

This is a foolproof and verbose method.

25

To specify files(2)- Use '.'

PS> .\notitle.ps1

26

To specify files(2)- Use '.'

PS> .\notitle.ps1Hello, there!PS>

When you want to specify a file or a folder in your current directory, give '.\' to the head of a relative path.

27

Case - You cannot execute a file

PS> $myScript = “C:\my\posh\notitle.ps1”PS> $myScript

There are some cases where you cannot execute a file with the way we have seen.

28

PS> $myScript = “C:\my\posh\notitle.ps1”PS> $myScriptC:\my\posh\notitle.ps1PS>

Case - You cannot execute a file

29

PS> $myScript = “C:\my\posh\notitle.ps1”PS> $myScriptC:\my\posh\notitle.ps1PS>

Though we want to execute a file, this way shows us only the path we set.

Case - You cannot execute a file

30

To execute files(1)- Use '&'

PS> & $myScriptHello, there!PS>

In this case, you can pass a path to execute to an ampersand symbol('&') and you will execute it.

The ampersand symbol works whether there are spaces between the symbol and the path or not.

31

To execute files(2)- Use '.'

PS> . $myScriptHello, there!PS>

In the same case, when you use a dot symbol(.) with a path, you will execute it.

This method is called 'Dot-Sourcing'.

32

To execute files(2)- Use '.'

PS> . $myScriptHello, there!PS>

In the same case, when you use a dot symbol(.) with a path, you will execute it.

This method is called 'Dot-Sourcing'.

For more details, you will find when you search the words “dot sourcing” or ask PowerShell wizards.( ◜◡◝ )

33

● File specification and execution

● File operation

● Pipeline

34

File operation

● Copy

● Rename

● Delete

There were demands for files to:

35

File operation

● Copy

● Rename

● Delete

There were demands for files to:

Though we can use 'good old' commands from cmd, we are born to be PowerShell geeks and we are wannabes to use Cmdlet.

36

Copy files(1)

PS> Copy-Item -Path .\notitle.ps1 -Destination C:\my\etcPS> ls C:\my\etc | where Name -eq notitle.ps1

ディレクトリ : C:\my\etc

Mode LastWriteTime Length Name---- ------------- ------ -----a--- 2014/07/11 23:42 31 notitle.ps1

PS>

When we want to copy files, we use Copy-Item.

37

Copy files(2)

PS> Get-ChildItem C:\my\etc | % { $_.Name }PS> Copy-Item -Path C:\my\posh\n* -Destination C:\my\etcPS> Get-ChildItem C:\my\etc | % { $_.Name }

As -Path parameter of Copy-Item understands wildcard notations, we can use this cmdlet as following:

38

PS> Get-ChildItem C:\my\etc | % { $_.Name }PS> Copy-Item -Path C:\my\posh\n* -Destination C:\my\etcPS> Get-ChildItem C:\my\etc | % { $_.Name }notitle.ps1nanimoyarukishinai.txtPS>

Copy files(2)

As -Path parameter of Copy-Item understands wildcard notations, we can use this cmdlet as following:

39

Rename files

PS> ls | % { $_.Name }notitle.ps1PS> Rename-Item -Path .\notitle.ps1 -NewName foobar.ps1PS> ls | % { $_.Name }foobar.ps1PS>

Use Rename-Item to rename files.

40

Delete files

PS> ls | % { $_.Name }notitle.ps1PS> Remove-Item .\notitle.ps1PS> ls | % { $_.Name }PS>

Use Remove-Item to delete files.

41

Delete files

PS> ls | % { $_.Name }notitle.ps1PS> Remove-Item .\notitle.ps1PS> ls | % { $_.Name }PS>

Use Remove-Item to delete files.

In PowerShell, cmdlets are defined by setting their names on “Verb-Objective” rules, so we can find them intuitively.

42

● File specification and execution

● File operation

● Pipeline

43

Pipeline

There is a strong relationship like this:“Associate PowerShell with pipeline,

44

Pipeline

There is a strong relationship like this:“Associate PowerShell with pipeline, associate pipeline with F# PowerShell”

45

Pipeline

in my humble opinion.

● filtering

● serial processing

There is a strong relationship like this:“Associate PowerShell with pipeline, associate pipeline with F# PowerShell”

When we want to use pipeline, the reason is classified into 2 broad categories:

46

Pipeline - filtering

@("hoge", "fuga", "bar") | where { $_.Length -eq 4 }# "hoge"# "fuga"

Filtering is one of the basis operations for pipeline.Returns only the elements of the collection for which the given condition block returns $True.

47

Pipeline - filtering

@("hoge", "fuga", "bar") | where { $_.Length -eq 4 }# "hoge"# "fuga"

Filtering is one of the basis operations for pipeline.Returns only the elements of the collection for which the given condition block returns $True.

$_ is the variable for the current value in the pipeline.When I was one of the PowerShell newbies, I had trouble remembering the variable and searched again and again...

48

Pipeline - serial processing(1)

@("hoge", "fuga", "bar") | foreach { $_.ToUpper() }# "HOGE"# "FUGA"# “BAR”

Applies the given block for ForEach-Object to each element of the collection.

49

Pipeline - serial processing(1)

@("hoge", "fuga", "bar") | foreach { $_.ToUpper() }# "HOGE"# "FUGA"# “BAR”

Applies the given block for ForEach-Object to each element of the collection.

It is similar to the functions map, iter or something like these in your natural functional programming languages.

50

Pipeline - serial processing(2)

@(1 .. 3) | % { $_ + 2 }# 3# 4# 5

There is an useful alias for Foreach-Object: '%' symbol.

51

The topics I told you about

● I was bad at PowerShell, but now feel to fit it

● I found the way how to overcome to struggle

52

The topics I told you about

● I was bad at PowerShell, but now feel to fit it

● I found the way how to overcome to struggle

● File specification and execution

● File operation

● Pipeline

53

The topics I told you about

● I was bad at PowerShell, but now feel to fit it

● I found the way how to overcome to struggle

● File specification and execution

● File operation

● Pipeline

Learn You Your PowerShell

for Great Good!

top related