Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell分配和检索变量_Powershell_Variables - Fatal编程技术网

Powershell分配和检索变量

Powershell分配和检索变量,powershell,variables,Powershell,Variables,所以我整天都在和Powershell搏斗。我正试着做一些我放在这里的东西。我在脚本的开头声明了一个变量,然后声明了两个函数。一个函数为变量设置一个值,另一个函数获取变量 当获取变量时,我什么也得不到-它是空的 有人知道我做错了什么吗(我在猜一些小而愚蠢的事情) 这是一个安全问题。换上这些,试一试 "1" { $global:ImUsedInMultiplePlaces = "We chose something!"; Write-Host "I put it in there!" }

所以我整天都在和Powershell搏斗。我正试着做一些我放在这里的东西。我在脚本的开头声明了一个变量,然后声明了两个函数。一个函数为变量设置一个值,另一个函数获取变量

当获取变量时,我什么也得不到-它是空的

有人知道我做错了什么吗(我在猜一些小而愚蠢的事情)

这是一个安全问题。换上这些,试一试

    "1" { $global:ImUsedInMultiplePlaces = "We chose something!"; Write-Host "I put it in there!"  }
    "2" { $global:ImUsedInMultiplePlaces = "We chose something else!"; Write-Host "I put it in there!"  }
这是一个安全问题。换上这些,试一试

    "1" { $global:ImUsedInMultiplePlaces = "We chose something!"; Write-Host "I put it in there!"  }
    "2" { $global:ImUsedInMultiplePlaces = "We chose something else!"; Write-Host "I put it in there!"  }

全局变量的使用使得软件更难阅读和理解。由于程序中任何地方的任何代码都可以随时更改变量的值,因此了解变量的使用可能需要了解程序的大部分内容。全局变量使得将代码分离到可重用库变得更加困难。它们可能会导致命名问题,因为一个文件中定义的全局变量可能与另一个文件中用于全局变量的相同名称冲突(从而导致链接失败)。同名的局部变量可以屏蔽全局变量的访问,这同样会导致代码更难理解。设置全局变量会产生难以定位和预测的副作用。全局变量的使用使得为了单元测试而隔离代码单元变得更加困难;因此,它们可以直接降低代码的质量

为了更加清晰,我稍微重命名(并修改)了这些函数

第一个函数输出一个字符串:

function Select-Something
{
    Write-Host "1: Something"
    Write-Host "2: Something else"
    $answer = Read-Host -Prompt "Pick One"

    switch($answer)
    {
        "1" { [string]$output = "We chose something!"     ; Write-Host "I put it in there!"  }
        "2" { [string]$output = "We chose something else!"; Write-Host "I put it in there!"  }
    }

    return $output
}
通过向第二个函数添加([string])参数,它可以接受任何字符串:

function Show-Selection ([string]$Selection)
{
    Write-Host $Selection
}
如您所见,它使代码更易于阅读:

Write-Host "Welcome to this amazing script, I'm about to make you choose."
Write-Host ""

$choice = Select-Something

Write-Host ""
Write-Host "Great Choice!"
Write-Host ""

Show-Selection $choice

全局变量的使用使得软件更难阅读和理解。由于程序中任何地方的任何代码都可以随时更改变量的值,因此了解变量的使用可能需要了解程序的大部分内容。全局变量使得将代码分离到可重用库变得更加困难。它们可能会导致命名问题,因为一个文件中定义的全局变量可能与另一个文件中用于全局变量的相同名称冲突(从而导致链接失败)。同名的局部变量可以屏蔽全局变量的访问,这同样会导致代码更难理解。设置全局变量会产生难以定位和预测的副作用。全局变量的使用使得为了单元测试而隔离代码单元变得更加困难;因此,它们可以直接降低代码的质量

为了更加清晰,我稍微重命名(并修改)了这些函数

第一个函数输出一个字符串:

function Select-Something
{
    Write-Host "1: Something"
    Write-Host "2: Something else"
    $answer = Read-Host -Prompt "Pick One"

    switch($answer)
    {
        "1" { [string]$output = "We chose something!"     ; Write-Host "I put it in there!"  }
        "2" { [string]$output = "We chose something else!"; Write-Host "I put it in there!"  }
    }

    return $output
}
通过向第二个函数添加([string])参数,它可以接受任何字符串:

function Show-Selection ([string]$Selection)
{
    Write-Host $Selection
}
如您所见,它使代码更易于阅读:

Write-Host "Welcome to this amazing script, I'm about to make you choose."
Write-Host ""

$choice = Select-Something

Write-Host ""
Write-Host "Great Choice!"
Write-Host ""

Show-Selection $choice

阅读关于范围界定的内容:阅读关于范围界定的内容:我知道这很简单。。非常感谢!在这件事上浪费了很多时间(是的,它现在起作用了)我知道这件事很简单。。非常感谢!在这个问题上浪费了很多时间(是的,它现在起作用了)