powershell 变量_极客学院:学习PowerShell变量,输入和输出

powershell 变量

powershell 变量

As we move away from simply running commands and move into writing full blown scripts, you will need a temporary place to store data. This is where variables come in.

随着我们不再只是运行命令而转而编写完整的脚本,您将需要一个临时位置来存储数据。 这是变量进入的地方。

Be sure to read the previous articles in the series:

确保阅读本系列中的先前文章:

  • Learn How to Automate Windows with PowerShell

    了解如何使用PowerShell自动执行Windows

  • Learning to Use Cmdlets in PowerShell

    学习在PowerShell中使用Cmdlet

  • Learning How to Use Objects in PowerShell

    学习如何在PowerShell中使用对象

  • Learning Formatting, Filtering and Comparing in PowerShell

    学习在PowerShell中进行格式化,过滤和比较

  • Learn to Use Remoting in PowerShell

    了解在PowerShell中使用远程处理

  • Using PowerShell to Get Computer Information

    使用PowerShell获取计算机信息

  • Working with Collections in PowerShell

    在PowerShell中使用集合

And stay tuned for the rest of the series all week.

并继续关注本系列的其余部分。

变数 (Variables)

Most programming languages allow the use of variables, which are simply containers which hold values. In PowerShell, we too have variables and they are really easy to use. Here is how to create a variable called “FirstName” and give it the value “Taylor”.

大多数编程语言都允许使用变量,这些变量只是保存值的容器。 在PowerShell中,我们也有变量,它们确实非常易于使用。 这是创建变量“ FirstName”并为其赋予值“ Taylor”的方法。

$FirstName = “Taylor”

$ FirstName =“泰勒”

The first thing most people seem to ask is why we put a dollar sign in front of the variables name, and that is actually a very good question. Really, the dollar sign is just a little hint to the shell that we want to access the contents of the variable (think what’s inside the container) and not the container itself. In PowerShell, variable names do not include the dollar sign, meaning that in the above example the variables name is actually “FirstName”.

多数人似乎要问的第一件事是为什么我们在变量名前加一个美元符号,这实际上是一个很好的问题。 确实,美元符号只是向shell提示我们要访问变量的内容(请考虑容器内部的内容),而不是容器本身。 在PowerShell中,变量名称不包含美元符号,这意味着在上面的示例中,变量名称实际上是“ FirstName”。

In PowerShell, you can see all the variables you have created in the variable PSDrive.

在PowerShell中,您可以看到在变量PSDrive中创建的所有变量。

gci variable:

gci变量:

image

Which means you can delete a variable from the shell at anytime too:

这意味着您也可以随时从外壳中删除变量:

Remove-Item Variable:\FirstName

删除项变量:\ FirstName

Variables don’t have to contain a single object either; you can just as easily store multiple objects in a variable. For example, if you wanted to store a list of running processes in a variable, you can just assign it the output of Get-Process.

变量也不必包含单个对象。 您可以轻松地将多个对象存储在一个变量中。 例如,如果要在变量中存储正在运行的进程的列表,则只需为其分配Get-Process的输出即可。

$Proc = Get-Process

$ Proc =获取进程

The trick to understanding this is to remember that the right hand side of the equals sign is always evaluated first. This means that you can have an entire pipeline on the right hand side if you want to.

理解这一点的技巧是要记住,始终首先评估等号的右侧。 这意味着您可以根据需要在右侧拥有整个管道。

$CPUHogs = Get-Process | Sort CPU -Descending | select -First 3

$ CPUHogs =获取进程| 排序CPU-降序| 选择-前3个

The CPUHogs variable will now contain the three running processes using the most CPU.

现在,CPUHogs变量将包含使用最多CPU的三个正在运行的进程。

image

When you do have a variable holding a collection of objects, there are some things to be aware of. For example, calling a method on the variable will cause it to be called on each object in the collection.

当确实有一个保存对象集合的变量时,有一些事情要注意。 例如,在变量上调用方法将导致在集合中的每个对象上调用该方法。

$CPUHogs.Kill()

$ CPUHogs.Kill()

Which would kill all three process in the collection. If you want to access a single object in the variable, you need to treat it like an array.

这将杀死集合中的所有三个过程。 如果要访问变量中的单个对象,则需要将其视为数组。

$CPUHogs[0]

$ CPUHogs [0]

Doing that will give you the first object in the collection.

这样做将为您提供集合中的第一个对象。

image

不要被抓住! (Don’t Get Caught!)

Variables in PowerShell are weakly typed by default meaning they can contain any kind of data, this seems to catch new comers to PowerShell all the time!

默认情况下,PowerShell中的变量类型较弱,这意味着它们可以包含任何类型的数据,这似乎一直吸引着PowerShell的新手!

$a = 10

$ a = 10

$b = ‘20’

$ b ='20'

So we have two variables, one contains a string and the other an integer. So what happens if you add them? It actually depends on which order you add them in.

因此,我们有两个变量,一个包含一个字符串,另一个包含一个整数。 那么,如果添加它们会怎样? 实际上,这取决于您添加它们的顺序。

$a + $b = 30

$ a + $ b = 30

While

$b + $a = 2010

$ b + $ a = 2010

In the first example, the first operand is an integer, $a, so PowerShell thinks thinks that you are trying to do math and therefore tries to convert any other operands into integers as well. However, in the second example the first operand is a string, so PowerShell just converts the rest of the operands to strings and concatenates them. More advanced scripters prevent this kind of gotcha by casting the variable to the type they are expecting.

在第一个示例中,第一个操作数是整数$ a,因此PowerShell认为您正在尝试进行数学运算,因此也尝试将任何其他操作数也转换为整数。 但是,在第二个示例中,第一个操作数是一个字符串,因此PowerShell只会将其余操作数转换为字符串并将它们连接起来。 更高级的脚本编写者通过将变量强制转换为期望的类型来防止这种情况。

[int]$Number = 5 [int]$Number = ‘5’

[int] $ Number = 5 [int] $ Number ='5'

The above will both result in the Number variable containing an integer object with a value of 5.

上面两者都会导致Number变量包含一个值为5的整数对象。

输入输出 (Input and Output)

Because PowerShell is meant to automate things, you’re going to want to avoid prompting users for information wherever possible. With that said, there are going to be times where you can’t avoid it,  and for those times we have the Read-Host cmdlet. Using it is really simple:

由于PowerShell旨在使事情自动化,因此您将避免在任何可能的情况下提示用户输入信息。 话虽如此,在某些时候您将无法避免它,并且在那些时候,我们拥有Read-Host cmdlet。 使用它真的很简单:

$FirstName = Read-Host –Prompt ‘Enter your first name’

$ FirstName =读主机–提示'输入您的名字'

image

Whatever you enter will then be saved in the variable.

输入的内容将保存在变量中。

image

Writing output is just as easy with the Write-Output cmdlet.

使用Write-Output cmdlet可以轻松写入输出。

Write-Output “How-To Geek Rocks!”

写输出“ How-To Geek Rocks!”

image

Join us tomorrow where we tie everything we have learned together!

明天加入我们,在这里我们会将我们学到的一切联系在一起!

翻译自: https://www.howtogeek.com/141099/geek-school-learning-powershell-variables-input-and-output/

powershell 变量

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279388.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

offsetTop、offsetLeft、offsetWidth、offsetHeight、style中的样式

< DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd> 假设 obj 为某个 HTML 控件。 obj.offsetTop 指 obj 距离上方或上层控件的位置&#xff0c;整型&#xff0c;单位像素。 obj.offsetLeft 指 obj 距离左方或上层控件的位置&#xff0…

Mock2 moco框架的http协议get方法Mock的实现

首先在Chapter7文件夹下再新建一个startGet.json startget.json代码如下&#xff0c;因为是get请求&#xff0c;所以要写method关键字&#xff0c;有两个&#xff0c;一个是有参数&#xff0c;一个是无参数的请求。 [{"description":"模拟一个没有参数的get请求…

imessage_如何在所有Apple设备上同步您的iMessage

imessageMessages in iCloud lets you sync your iMessages across all of your Apple devices using your iCloud account. Here’s how to set it up. 通过iCloud中的消息&#xff0c;您可以使用iCloud帐户在所有Apple设备上同步iMessage。 设置方法如下。 Apple announced t…

“.Net 社区大会”(dotnetConf) 2018 Day 1 主题演讲

Miguel de Icaza、Scott Hunter、Mads Torgersen三位大咖给大家带来了 .NET Core ,C# 以及 Xamarin的精彩内容&#xff1a;6月份已经发布了.NET Core 2.1, 大会上Scott Hunter 一开始花了大量的篇幅回顾.NET Core 2.1的发布&#xff0c;社区的参与度已经非常高&#xff0c;.NET…

长时间曝光计算_如何拍摄好长时间曝光的照片

长时间曝光计算In long exposure photography, you take a picture with a slow shutter speed—generally somewhere between five and sixty seconds—so that any movement in the scene gets blurred. It’s a way to show the passage of time in a single image. Let’s …

深度学习入门3

CNN 第一周&#xff1a; title: edge detection example 卷积核在边缘检测中的应用&#xff0c;可解释&#xff0c;卷积核的设计可以找到像素列突变的位置 把人为选择的卷积核参数&#xff0c;改为学习参数&#xff0c;可以学到更多的特征 title: padding n * n图片&#xff0c…

图像大小调整_如何在Windows中调整图像和照片的大小

图像大小调整Most image viewing programs have a built-in feature to help you change the size of images. Here are our favorite image resizing tools for Windows. We’ve picked out a built-in option, a couple of third party apps, and even a browser-based tool.…

Spring Data JPA例子[基于Spring Boot、Mysql]

阅读目录 关于Spring Data关于Spring Data子项目关于Spring Data Jpa例子&#xff0c;Spring Boot Spring Data Jpa运行、测试程序程序源码参考资料关于Spring Data Spring社区的一个顶级工程&#xff0c;主要用于简化数据&#xff08;关系型&非关系型&#xff09;访问&am…

The way of Webpack learning (IV.) -- Packaging CSS(打包css)

一&#xff1a;目录结构 二&#xff1a;webpack.config.js的配置 const path require(path);module.exports {mode:development,entry:{app:./src/app.js},output:{path:path.resolve(__dirname,dist),publicPath:./dist/,//设置引入路径在相对路径filename:[name].bundle.js…

文本文档TXT每行开头结尾加内容批处理代码

文本文档TXT每行开头结尾加内容批处理代码 读A.TXT ,每行开头加&#xff1a;HTMLBodytxt HTMLBodytxt chr(10) aaaaaaaa结尾加&#xff1a;bbbbbbbb处理后的文档写入到B.TXT For /f "delims" %%i in (a.txt) do echo HTMLBodytxt HTMLBodytxt chr(10) aaaaaaaa%%…

windows运行对话框_如何在Windows运行对话框中添加文本快捷方式?

windows运行对话框Windows comes prepackaged with a ton of handy run-dialog shortcuts to help you launch apps and tools right from the run box; is it possible to add in your own custom shortcuts? Windows预包装了许多方便的运行对话框快捷方式&#xff0c;可帮助…

Zabbix 3.0 安装

Zabbix 3.0 For CentOS6安装 1 概述2 安装MySQL3 安装WEB4 安装Zabbix-Server5配置WEB1概述 对于3.0&#xff0c;官方只提供CentOS7的RPM包&#xff0c;Ubuntu的DEB包&#xff0c;对于CentOS6&#xff0c;默认不提供RPM包&#xff0c;为了照顾到使用CentOS6的兄弟们&#xff0c…

[Hadoop in China 2011] 中兴:NoSQL应用现状及电信业务实践

http://tech.it168.com/a2011/1203/1283/000001283154.shtml 在今天下午进行的NoSQL系统及应用分论坛中&#xff0c;中兴云计算平台研发总工、中兴通讯技术专家委员会专家高洪发表主题演讲“NoSQL技术的电信业务实践”&#xff0c;介绍了NoSQL的发展现状及其在电信业务中的应用…

qmediaplayer获取流类型_Java 流API

流相关的接口和类在java.util.stream包中。AutoCloseable接口来自java.lang包。所有流接口从继承自AutoCloseable接口的BaseStream接口继承。AutoCloseable|--BaseStream|--IntStream|--LongStream|--DoubleStream|--Stream如果流使用集合作为其数据源&#xff0c;并且集合不需…

田刚:庞加莱猜想与几何

&#xff08;作者 田刚&#xff09; 时间&#xff1a;2015年11月1日 地点&#xff1a;北京大学北京国际数学研究中心 主题&#xff1a;未来论坛“理解未来”讲座北大专场&#xff1a;庞加莱猜想与几何 田刚&#xff1a; 非常高兴能够有这个机会来参加未来论坛讲演。我今天要讲的…

进化:从孤胆极客到高效团队_极客学校:学习Windows 7 –远程管理

进化:从孤胆极客到高效团队In this installation of Geek School, we look at how we can administer our machines remotely using Remote Assistance, Remote Desktop, Windows Remote Management also known as WinRM, and PowerShell. 在此Geek School安装中&#xff0c;我…

打印墨水调钢笔墨水_如何节省墨水并改善网站打印质量

打印墨水调钢笔墨水Printing out web pages you want a hard copy of can be a little hit and miss. Unlike other documents, it is not easy to tell exactly how many pieces of paper will be needed, and whether or not there will be any awkward clipping. Add to thi…

highcharts 怎么去掉鼠标悬停效果_练瑜伽减肥没效果什么原因?

没有心的参与&#xff0c;瑜伽就不是瑜伽曾经有很多人问&#xff1a;自己想用瑜伽来减肥&#xff0c;但练习瑜伽这么久&#xff0c;为什么还是减不下来&#xff1f;一点效果都没有。瑜伽是什么&#xff1f;瑜伽只是一种单纯的运动吗&#xff1f;只让身体参与进去就可以了吗&…

Java基础学习总结(23)——GUI编程

2019独角兽企业重金招聘Python工程师标准>>> 一、AWT介绍 所有的可以显示出来的图形元素都称为Component&#xff0c;Component代表了所有的可见的图形元素&#xff0c;Component里面有一种比较特殊的图形元素叫Container&#xff0c;Container(容器)在图形界面里面…

在Windows 7或Vista(或Windows 8.x,Sorta)上禁用Aero

The Windows Aero Glass interface for Windows 7 or Vista requires a decent video card, you won’t be able to use it on an old clunker computer. For those worried about performance, sometimes squeezing every last drop requires disabling Aero. Windows 7或Vist…