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

推荐订阅源

U
Unit 42
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News | PayPal Newsroom
Application and Cybersecurity Blog
Application and Cybersecurity Blog
O
OpenAI News
S
Security @ Cisco Blogs
宝玉的分享
宝玉的分享
Hacker News: Ask HN
Hacker News: Ask HN
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
TaoSecurity Blog
TaoSecurity Blog
Help Net Security
Help Net Security
Latest news
Latest news
NISL@THU
NISL@THU
S
Security Affairs
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
AI
AI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
小众软件
小众软件
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
AWS News Blog
AWS News Blog
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
I
InfoQ
Schneier on Security
Schneier on Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com
IT之家
IT之家
T
Threatpost
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
H
Heimdal Security Blog
S
SegmentFault 最新的问题

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.