
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變量:

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的三個正在運行的進程。

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.
這樣做將為您提供集合中的第一個對象。

不要被抓住! (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 =讀主機–提示'輸入您的名字'

Whatever you enter will then be saved in the variable.
輸入的內容將保存在變量中。

Writing output is just as easy with the Write-Output cmdlet.
使用Write-Output cmdlet可以輕松寫入輸出。
Write-Output “How-To Geek Rocks!”
寫輸出“ How-To Geek Rocks!”

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 變量