当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 全局变量 |如何定义全局变量?

作者:精品下载站 日期:2024-12-14 20:33:29 浏览:12 分类:玩电脑

PowerShell 全局变量 |如何定义全局变量?


[玩转系统] PowerShell 全局变量 |如何定义全局变量?

PowerShell全局变量简介

变量是 PowerShell 不可或缺的一部分。变量的类型有很多种;本文将重点讨论全局变量。全局变量是当前会话中所有脚本、函数或任何 cmdlet 都可以使用的变量。通常,全局变量是在开头声明的,大多数是在顶部。全局变量的主要用途是它的可重用性。本文将详细解释全局变量、其使用语法以及适当的示例。

PowerShell 全局变量的语法

创建全局变量的方式如下

$global:test="This is a test"

或者

$global:test = $null

示例:

Write-Host "Example of global variable in PS" -ForegroundColor Green
$global:test=""
Function test ($tes)
{
$global:test+=$tes
}
test -tes "this "
$global:test+="is "
$global:test+="test "
Write-Host $global:test -ForegroundColor Green

输出:

[玩转系统] PowerShell 全局变量 |如何定义全局变量?

示例:

通过函数设置全局变量

Write-Host "Demo of calling global variable from function" -ForegroundColor Green
$global:fln = $null
function getfn ($fn, $ln)
{
$fn +"  "+ $ln
}
$global:fln = getfn vignesh krishnakumar
Write-Host "the function value is set to global variable" -ForegroundColor Green
write-Host "the value is "$global:fln
$global:fln1 = $null
function getfn1 ($fn1, $ln1, $chn1)
{
$fn1 +"  "+ $ln1 +"  "+ $chn1
}
Write-Host "Demo of global variable with function as three parameters" -ForegroundColor Green
$global:fln1 = getfn1 vignesh krishnakumar Chennai
Write-Host " the value is " $global:fln1

输出:

[玩转系统] PowerShell 全局变量 |如何定义全局变量?

示例:

输入:

Write-Host "Demo of passing global variable as a parameter to a function"
Write-Host "global variable needs to be declared as reference"
$global:finalsum =""
function sum ($no1, $no2, [REF]$resr)
{
$resr.Value = $no1 + $no2
}
#You can then call it like this:sum 51 61 ([REF]$global:finalsum)
Write-Host " the value of the global variable is" $global:finalsum
$global:finalsum1 =""
function sum1 ($no11, $no22, $no33,$no44,[REF]$resr1)
{
$resr1.Value = $no11 + $no22 +$no33 + $no44
}
#You can then call it like this:sum1 51 61 71 81 ([REF]$global:finalsum1)
Write-Host " the value of the global variable is" $global:finalsum1

[玩转系统] PowerShell 全局变量 |如何定义全局变量?

全局变量的优点

  • 它可以存储所有类型的数据类型值,例如字符串、对象、数组、整数和哈希表。
  • 它可以存储不同对象类型的集合
  • 每个变量都分配有一个内存
  • 如果没有为全局变量赋值,则默认情况下将其视为空。
  • 尽管不是强制性的,但最好在顶部声明全局变量以便于理解。

范围层次结构

如前所述,一旦您发送 PowerShell 会话,PowerShell 就会在全球范围内为您创建一些内容。这些东西可以是容量、假定名称的因素或 PSDrive。您在 PowerShell 会话中定义的任何内容也将在全局范围内定义。由于默认情况下您位于全球范围内,因此如果您正在执行创建另一个范围的操作(例如执行脚本或运行研讨会),则会创建一个子范围,而父范围将成为全球范围。范围就像父母和孩子的表格。因此,在父范围(在本例中为全球范围)中具有特征的任何内容都将在子范围内可用。但这些东西在它们所描述的范围内是可以编辑的。

不同类型的范围修饰符:

全球:这表示全球范围

本地:这表示仅当前范围

私有:这表示该项目是私有的并且在当前范围内可用

脚本:这表示可以在整个脚本中访问该项目

使用:这用于从其他脚本访问项目

工作流程:这表示工作流程中使用的项目

使用修饰符:

这用于处理远程 cmdlet 中的项目。每当需要在当前会话之外访问任何内容时,都应该使用 using 修饰符。这比全局变量高一级。一些示例 cmdlet 包括 Start-Job 和 Invoke-Command。

示例:

输入:

Write-Host "Demo of setting global variable value" -ForegroundColor Green
Set-Variable -Name "TD" -Value (Get-Date) -Scope global
Get-Variable -Name "TD"
Set-Variable -Name "NAME1" -Value "vignesh" -Scope global
Set-Variable -Name "age1" -Value "28" -Scope global
Set-Variable -Name "city" -Value "chennai" -Option Private -Scope global
Set-Variable -Name "status" -Value "married" -Scope global
Set-Variable -Name "gender" -Value "male" -Scope global
Get-Variable -Name "NAME1"
Get-Variable -Name "age1"
Get-Variable -Name "city"
Get-Variable -Name "status"
Get-Variable -Name "gender"

输出:

[玩转系统] PowerShell 全局变量 |如何定义全局变量?

示例:

输入:

Write-Host "Example of global variable in PS" -ForegroundColor Green
$global:eg=""
Function test ($par)
{
$global:eg+=$par
}
test -par "welcome"
$global:eg += "user "
$global:eg+="mark "
Write-Host $global:eg -ForegroundColor Green
$global:abc = $null
function geabcdtfn ($fn, $ln)
{
$fn +"  "+ $ln
}
$global:abc = geabcdtfn vijaya sethupathi
write-Host "the value is "$global:abc
$global:fln1 = $null
function getfn1 ($fn1, $ln1, $chn1)
{
$fn1 +"  "+ $ln1 +"  "+ $chn1
}
Write-Host "Demo of global variable with function as three parameters" -ForegroundColor Green
$global:fln1 = getfn1 vignesh krishnakumar Chennai
Write-Host " the value is " $global:fln1
Set-Variable -Name "tdate" -Value (Get-Date) -Scope global
Set-Variable -Name "mark1" -Value "100" -Scope global
Set-Variable -Name "mark2" -Value "90" -Scope global
Set-Variable -Name "mark3" -Value "100" -Option Private -Scope global
Set-Variable -Name "mark4" -Value "100" -Scope global
Set-Variable -Name "total" -Value "390" -Scope global
Get-Variable -Name "mark1"
Get-Variable -Name "mark1"
Get-Variable -Name "mark1"
Get-Variable -Name "mark1"
Get-Variable -Name "total"
Get-Variable -Name "tdate"

输出:

[玩转系统] PowerShell 全局变量 |如何定义全局变量?

[玩转系统] PowerShell 全局变量 |如何定义全局变量?

结论

由此,文章详细解释了PowerShell中的全局变量。它详细解释了如何定义全局变量、为全局变量设置值、将全局变量传递给函数以及将函数返回的值设置为全局变量。尽管全局变量可用于整个会话,但不建议将所有变量创建为全局变量,这会导致不良实践。要了解更多详细信息,建议编写示例脚本并进行练习。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯