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.pswp.cn/news/279388.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279388.shtml
英文地址,請注明出處:http://en.pswp.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;并且集合不需…