惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
雷峰网
雷峰网
The Register - Security
The Register - Security
The Cloudflare Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
I
InfoQ
博客园 - 三生石上(FineUI控件)
H
Help Net Security
博客园 - 司徒正美
Vercel News
Vercel News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
B
Blog
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
L
LangChain Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recorded Future
Recorded Future
小众软件
小众软件
Martin Fowler
Martin Fowler
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
V
Visual Studio Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
V
V2EX
Blog — PlanetScale
Blog — PlanetScale

Katz Got Your Tongue

Polaris: What I'm Working On Why Ember is Different: Running Browser Tests in a Browser Exploring Ember Polaris: CSS Modules Without Addons Exploring Ember Polaris: A Fresh Take on the Component Format Ember Octane: Airtable Time Ember Octane: Pulling Out Data Ember Octane: Components Ember Octane: Let's Go "We're the Together Framework" Together: The Merb Story #EmberJS2019 More Accessible Than Ever Fun With PowerShell: Let's Get Started (Digging Deeper) PowerShell, Deduplicating Records Fun with PowerShell: Installation
PowerShell: Let's Get Started
Yehuda Katz · 2019-04-24 · via Katz Got Your Tongue

24 Apr 2019 3 min read

I've been having a lot of fun playing around with PowerShell recently. Let's see what it can do in Part 1 of a series on learning PowerShell with real-world examples!

Fun with PowerShell: Let's Get Started!

I've been having a lot of fun playing around with PowerShell recently, and wanted to write up some of my learnings.

If you're thinking: "I don't use Windows, so this post isn't for me", good news! Microsoft has released versions of PowerShell for OSX and Linux with easy installers, and I've tested the examples in this series on both Windows and Linux. I document the installation instructions in a separate post.

Let's get started. We'll interact with a REST API called the Open Movie Database. All of the APIs in OMDB require a (free) API key, so if you want to follow along, start by grabbing one.

Let's create a new directory called powershell-explore and create a new file in that directory called omdb.ps1. The ps stands for "PowerShell" and the 1 stands for "maybe we'll want a version 2 someday" (also .ps is taken by PostScript).

$key = "<PUT KEY HERE>"

Invoke-RestMethod "http://www.omdbapi.com/?apikey=$key&s=Avengers"

$key = ... is how you define a variable in Powershell. By default, Powershell variables are scoped to the script they were defined in.

Invoke-RestMethod is called a cmdlet, which is basically a function written in native code. PowerShell functions are invoked by referencing them, and arguments are space-separated. This matches the behavior of command invocation in other shells like Bash.

We're making an API call to ask for a list of all of the movies with the word "Avengers" in it. This dumps out:

Search
------
{@{Title=The Avengers; Year=2012; imdbID=tt0848228; Type=movie; Poster=https://m.media-amazon.com/images/M/MV5BNDYxNjQyMjAtNTdiOS00NGYwLWFmNTAtNThmYjU5ZGI2YTI1XkEyXkFqcGd…

This output is structured, which is one of the main points of Powershell. Unfortunately, it's not very readable.

Let's try again:

Invoke-RestMethod "http://www.omdbapi.com/?apikey=$key&s=Avengers" | Format-List

The | character means that we're passing the result of the previous command into a new function. The Format-List function takes an object as pipeline input and formats it like a list.

This time, we get slightly nicer output:

Search       : {@{Title=The Avengers; Year=2012; imdbID=tt0848228; Type=movie;
               Poster=https://m.media-amazon.com/images/M/MV5BNDYxNjQyMjAtNTdiOS00NGYwLWFmNTAtNThmYjU5ZGI2YTI1XkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_SX300.jpg},
               @{Title=Avengers: Age of Ultron; Year=2015; imdbID=tt2395427; Type=movie;
               Poster=https://m.media-amazon.com/images/M/MV5BMTM4OGJmNWMtOTM4Ni00NTE3LTg3MDItZmQxYjc4N2JhNmUxXkEyXkFqcGdeQXVyNTgzMDMzMTg@._V1_SX300.jpg},
               @{Title=Avengers: Infinity War; Year=2018; imdbID=tt4154756; Type=movie;
               Poster=https://m.media-amazon.com/images/M/MV5BMjMxNjY2MDU1OV5BMl5BanBnXkFtZTgwNzY1MTUwNTM@._V1_SX300.jpg}, @{Title=The Avengers; Year=1998;
               imdbID=tt0118661; Type=movie;
               Poster=https://m.media-amazon.com/images/M/MV5BYWE1NTdjOWQtYTQ2Ny00Nzc5LWExYzMtNmRlOThmOTE2N2I4XkEyXkFqcGdeQXVyNjUwNzk3NDc@._V1_SX300.jpg}…}
totalResults : 92
Response     : True

Better, but still pretty hard to read.

Let's pull the response apart a little.

$movies = Invoke-RestMethod "http://www.omdbapi.com/?apikey=$key&s=Avengers"

$movies.totalResults
$movies.search | Format-List

And we get:

92

Title  : The Avengers
Year   : 2012
imdbID : tt0848228
Type   : movie
Poster : https://m.media-amazon.com/images/M/MV5BNDYxNjQyMjAtNTdiOS00NGYwLWFmNTAtNThmYjU5ZGI2YTI1XkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_SX300.jpg

Title  : Avengers: Age of Ultron
Year   : 2015
imdbID : tt2395427
Type   : movie
Poster : https://m.media-amazon.com/images/M/MV5BMTM4OGJmNWMtOTM4Ni00NTE3LTg3MDItZmQxYjc4N2JhNmUxXkEyXkFqcGdeQXVyNTgzMDMzMTg@._V1_SX300.jpg

Title  : Avengers: Infinity War
Year   : 2018
imdbID : tt4154756
Type   : movie
Poster : https://m.media-amazon.com/images/M/MV5BMjMxNjY2MDU1OV5BMl5BanBnXkFtZTgwNzY1MTUwNTM@._V1_SX300.jpg

Title  : The Avengers
Year   : 1998
imdbID : tt0118661
Type   : movie
Poster : https://m.media-amazon.com/images/M/MV5BYWE1NTdjOWQtYTQ2Ny00Nzc5LWExYzMtNmRlOThmOTE2N2I4XkEyXkFqcGdeQXVyNjUwNzk3NDc@._V1_SX300.jpg

Title  : The Avengers: Earth's Mightiest Heroes
Year   : 2010–2012
imdbID : tt1626038
Type   : series
Poster : https://m.media-amazon.com/images/M/MV5BYzA4ZjVhYzctZmI0NC00ZmIxLWFmYTgtOGIxMDYxODhmMGQ2XkEyXkFqcGdeQXVyNjExODE1MDc@._V1_SX300.jpg

Title  : Ultimate Avengers
Year   : 2006
imdbID : tt0491703
Type   : movie
Poster : https://m.media-amazon.com/images/M/MV5BNDFmZTkxMjktMzRiYS00YzMwLWFhZDctOTQ2N2NlOTAyZDJhXkEyXkFqcGdeQXVyNjgzNDU2ODI@._V1_SX300.jpg

Title  : Ultimate Avengers II
Year   : 2006
imdbID : tt0803093
Type   : movie
Poster : https://m.media-amazon.com/images/M/MV5BZjI3MTI5ZTYtZmNmNy00OGZmLTlhNWMtNjZiYmYzNDhlOGRkL2ltYWdlL2ltYWdlXkEyXkFqcGdeQXVyNTAyODkwOQ@@._V1_SX300.jpg

Title  : The Avengers
Year   : 1961–1969
imdbID : tt0054518
Type   : series
Poster : https://m.media-amazon.com/images/M/MV5BZWI4ZWM4ZWQtODk1ZC00MzMxLThlZmMtOGFmMTYxZTAwYjc5XkEyXkFqcGdeQXVyMTk0MjQ3Nzk@._V1_SX300.jpg

Title  : Avengers Assemble
Year   : 2013–
imdbID : tt2455546
Type   : series
Poster : https://m.media-amazon.com/images/M/MV5BMTY0NTUyMDQwOV5BMl5BanBnXkFtZTgwNjAwMTA0MDE@._V1_SX300.jpg

Title  : Avengers Assemble
Year   : 2013–
imdbID : tt2455546
Type   : series
Poster : https://m.media-amazon.com/images/M/MV5BMTY0NTUyMDQwOV5BMl5BanBnXkFtZTgwNjAwMTA0MDE@._V1_SX300.jpg

Much nicer.

One thing you might have caught: anything that we type in a script that isn't piped into another command or assigned to a variable is printed out.

If we want to suppress that behavior, we can redirect things into $null, a special built-in variable that corresponds to "nothing". If you're familiar with Bash, $null is similar to /dev/null when used this way.

Let's try it:

$movies = Invoke-RestMethod "http://www.omdbapi.com/?apikey=$key&s=Avengers"

$movies.totalResults
$movies.search | Format-List > $null

Output:

92

To be continued.