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请求…

Android 干货,强烈推荐

本文主要收集 Android开发中常用的干货技术&#xff0c;现做出目录&#xff0c;此文不断更新中&#xff0c;欢迎关注、点赞、投稿。Android 四大组件与布局1. Activity 使用详解2. Service 使用详解3. Broadcast 使用详解4. ContentProvider 使用详解5. 四大布局 使用详解6. Re…

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…

Windows 2003 NTP 时间服务器设置

需要在局域网中架设一台时间同步服务器&#xff0c;统一各客户端及服务器的系统时间&#xff0c;在网上查找大多是基于Linux下的 确&#xff2e;&#xff34;&#xff30;服务器&#xff0e;搜索&#xff0c;实验及总结&#xff0c;写一篇采用Windwos2003自带的W32Time服务用于…

React 深入学习:React 更新队列

path&#xff1a;packages/react-reconciler/src/ReactUpdateQueue.js 更新 export type Update<State> {expirationTime: ExpirationTime, // 到期时间tag: 0 | 1 | 2 | 3, // 更新类型payload: any, // 负载callback: (() > mixed) | null, // 回调函数next: Updat…

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

长时间曝光计算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 …

思科设备snmp配置。

1、设置IOS设备在IOS的Enable状态下&#xff0c;敲入 config terminal进入全局配置状态 Cdp run启用CDP snmp-server community gsunion ro \\配置本路由器的只读字串为gsunion snmp-server community gsunion rw \\配置本路由器的读写字串为gsunion snmp-server enable trap…

Python——逻辑运算(or,and)

print(0 and 2 > 1) #结果0 print(0 and 2 < 1) #结果0 print(1 and 2 > 1) #结果True print(1 and 2 < 1) #结果False print(2 > 1 and 0) #结果0 print(2 < 1 and 0) #结果False print(2 > 1 and 1) #结果1 print(2 < 1 and 0) #结果False# and 前或…

深度学习入门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;可帮助…

前后台分离--概念相关

js 包管理器:  1、npm  2、bower 包管理器的作用&#xff1a;&#xff08;之前满世界找代码&#xff0c;现在统一地址了。类似于360软件管家&#xff0c;maven仓库。&#xff09;  1、复用别人已经写好的代码。  2、管理包之间的依赖关系。 JS &#xff1a;语言&#…

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;并且集合不需…