【问题标题】:Powershell write IF statement into a VariablePowershell将IF语句写入变量
【发布时间】:2017-10-20 10:56:29
【问题描述】:

我想要一个 If/Else 脚本,它可以将输出写入我稍后将在脚本中使用的变量。

这是我所拥有的:

if (!(Get-Module -ListAvailable -Name servermanager)) {
    Import-Module servermanager
}

$WinFeat = Get-WindowsFeature -Name telnet-client | Where installed
if ($WinFeat -EQ $null) {
    Add-WindowsFeature telnet-client
}

基本上我在想我可以做这样的事情:

if (!(Get-Module -ListAvailable -Name servermanager)) {
    Import-Module servermanager
} else {
    $alreadyModule
}

$WinFeat = Get-WindowsFeature -Name telnet-client | Where installed
if ($WinFeat -EQ $null) {
    Add-WindowsFeature telnet-client
} else {
    $alreadyFeature
}

if (($alreadyModule) -and ($alreadyFeature) {Write-Host "STUFF INSTALLED"}

这些“东西”将用于标准输出,然后我将在 Ansible Playbook 中使用。

所以本质上我想说如果这已经安装,并且已经安装,然后编写 STUFF INSTALLED 以在 Ansible 中使用......但我似乎无法弄清楚如何在没有它的情况下编写变量开始于:

$alreadyModule = ....
$alreadyFeature = ....

任何帮助将不胜感激。

【问题讨论】:

  • $alreadyFeature = (Get-WindowsFeature -Name telnet-client).Installed?

标签: powershell if-statement output


【解决方案1】:

感谢 Ansgar,我已将您的内容包含在内并提出:

if (!(Get-Module -ListAvailable -Name servermanager)) {
    Import-Module servermanager
} else {
    $alreadyModule = (Get-Module -ListAvailable -Name servermanager)
}


$WinFeat = Get-WindowsFeature -Name telnet-client | Where installed
if ($WinFeat -EQ $null) {
    Add-WindowsFeature telnet-client
} else {
    $alreadyFeature = (Get-WindowsFeature -Name telnet-client).Installed
}

if (($alreadyFeature) -and ($alreadyModule)) {Write-Host "STUFF INSTALLED"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-18
    • 2015-07-11
    • 2015-11-09
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    相关资源
    最近更新 更多