Dev Ops on AWS with PowerShell (PowerShell Conference Asia 2016)
One man loves powershell once he failed
-
Upload
km-gab -
Category
Technology
-
view
146 -
download
1
description
Transcript of One man loves powershell once he failed
![Page 1: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/1.jpg)
1
One man loves PowerShellonce he failed
@gab_km12th July, 2014
Japan PowerShell meetup #3
![Page 2: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/2.jpg)
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!
![Page 3: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/3.jpg)
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
![Page 4: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/4.jpg)
4
Once upon a time...
![Page 5: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/5.jpg)
5
Once upon a time...
In this tweet, I have started PowerShell 2 years ago from this time(24/Mar/2011).
![Page 6: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/6.jpg)
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.
![Page 7: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/7.jpg)
7
In these days...
![Page 8: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/8.jpg)
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.
![Page 9: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/9.jpg)
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.
![Page 10: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/10.jpg)
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.
![Page 11: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/11.jpg)
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.
![Page 12: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/12.jpg)
12
Case 2
![Page 13: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/13.jpg)
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.
![Page 14: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/14.jpg)
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.
![Page 15: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/15.jpg)
15
Case 3
![Page 16: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/16.jpg)
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") }
![Page 17: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/17.jpg)
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
![Page 18: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/18.jpg)
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.
![Page 19: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/19.jpg)
19
● File specification and execution
● File operation
● Pipeline
![Page 20: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/20.jpg)
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.
![Page 21: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/21.jpg)
21
PS> notitle.ps1notitle.ps1 : 用語 'notitle.ps1' は、コマンドレット、関数、スクリプト ファイル、または操作可能なプログラムの名前として認識されません。名前が正しく記述されていることを確認し、パスが含まれている場合はそのパスが正しいことを確認してから、再試行してください。発生場所 行 :1 文字 :1+ notitle.ps1+ ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (notitle.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundExceptionPS>
File specification and execution
![Page 22: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/22.jpg)
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
![Page 23: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/23.jpg)
23
To specify files(1)- Absolute path
PS> C:\my\posh\notitle.ps1
![Page 24: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/24.jpg)
24
To specify files(1)- Absolute path
PS> C:\my\posh\notitle.ps1Hello, there!PS>
This is a foolproof and verbose method.
![Page 25: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/25.jpg)
25
To specify files(2)- Use '.'
PS> .\notitle.ps1
![Page 26: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/26.jpg)
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.
![Page 27: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/27.jpg)
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.
![Page 28: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/28.jpg)
28
PS> $myScript = “C:\my\posh\notitle.ps1”PS> $myScriptC:\my\posh\notitle.ps1PS>
Case - You cannot execute a file
![Page 29: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/29.jpg)
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
![Page 30: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/30.jpg)
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.
![Page 31: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/31.jpg)
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'.
![Page 32: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/32.jpg)
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.( ◜◡◝ )
![Page 33: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/33.jpg)
33
● File specification and execution
● File operation
● Pipeline
![Page 34: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/34.jpg)
34
File operation
● Copy
● Rename
● Delete
There were demands for files to:
![Page 35: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/35.jpg)
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.
![Page 36: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/36.jpg)
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.
![Page 37: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/37.jpg)
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:
![Page 38: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/38.jpg)
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:
![Page 39: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/39.jpg)
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.
![Page 40: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/40.jpg)
40
Delete files
PS> ls | % { $_.Name }notitle.ps1PS> Remove-Item .\notitle.ps1PS> ls | % { $_.Name }PS>
Use Remove-Item to delete files.
![Page 41: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/41.jpg)
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.
![Page 42: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/42.jpg)
42
● File specification and execution
● File operation
● Pipeline
![Page 43: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/43.jpg)
43
Pipeline
There is a strong relationship like this:“Associate PowerShell with pipeline,
![Page 44: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/44.jpg)
44
Pipeline
There is a strong relationship like this:“Associate PowerShell with pipeline, associate pipeline with F# PowerShell”
![Page 45: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/45.jpg)
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:
![Page 46: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/46.jpg)
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.
![Page 47: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/47.jpg)
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...
![Page 48: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/48.jpg)
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.
![Page 49: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/49.jpg)
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.
![Page 50: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/50.jpg)
50
Pipeline - serial processing(2)
@(1 .. 3) | % { $_ + 2 }# 3# 4# 5
There is an useful alias for Foreach-Object: '%' symbol.
![Page 51: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/51.jpg)
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
![Page 52: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/52.jpg)
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
![Page 53: One man loves powershell once he failed](https://reader033.fdocuments.in/reader033/viewer/2022052504/54b721584a795903798b47a2/html5/thumbnails/53.jpg)
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!