首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写if exist语句块以检查是否已使用名称数组安装了Powershell模块

如何编写if exist语句块以检查是否已使用名称数组安装了Powershell模块
EN

Stack Overflow用户
提问于 2020-05-20 13:24:51
回答 1查看 632关注 0票数 0

更新:每个retryW接受的解决方案的最终工作脚本。这是实现我的总体目标的最简单的方法。

代码语言:javascript
复制
##########################
### INSTALL PS MODULES ###
##########################
Function inmod { Clear-Host

$modulesArray = @(
  "Convert",
  "Get-UserProfile",
  "Get-WindowsVersion",
  "PoshInternals",
  "PSConsoleTheme",
  "PSSpeedTest",
  "UpdateOS",
  "WindowsPSModulePath",
  "WindowsConsoleFonts",
  "xFailOverCluster"
)

ForEach($mod in $modulesArray) {
    If(Get-Module -ListAvailable $mod) {
        Continue; CLS
    } Else {
        Write-Host "Installing '$mod'"
        Install-Module $mod -Force -ErrorAction 'SilentlyContinue'
    }
  }
}

#############################
### UN-INSTALL PS MODULES ###
#############################
Function unmod { Clear-Host

$modulesArray = @(
  "Convert",
  "Get-UserProfile",
  "Get-WindowsVersion",
  "PSConsoleTheme",
  "PSSpeedTest",
  "WindowsPSModulePath",
  "WindowsConsoleFonts",
  "xFailOverCluster"
)

ForEach($mod in $modulesArray) {
    If(Get-Module -ListAvailable $mod) {
        Continue; CLS
    } Else {
        Write-Host "Un-Installing '$mod'"
        Uninstall-Module $mod
    }
  }
}

#################################
### UN-INSTALL ALL PS MODULES ###
#################################
Function unmodall { Clear-Host

ForEach($mods in (Get-Module -ListAvailable *).Name | Get-Unique) {
   Write-Host "Un-Installing '$mod'"
   Uninstall-Module $mod
  }
}

#########################
### UPDATE PS MODULES ###
#########################
Function upmod { Clear-Host

$modulesArray = @(
  "Convert",
  "Get-UserProfile",
  "Get-WindowsVersion",
  "PoshInternals",
  "PSConsoleTheme",
  "PSSpeedTest",
  "UpdateOS",
  "WindowsPSModulePath",
  "WindowsConsoleFonts",
  "xFailOverCluster"
)

ForEach($mod in $modulesArray) {
    If(Get-Module -ListAvailable $mod) {
        Continue; CLS
    } Else {
        Write-Host "Updating '$mod'"
        Update-Module $mod -Force
    }
  }
}

旧更新:对Lee_Dailey的响应

使用他的一些观点/建议,我已经将我的脚本进行到了下面这一点。

然而,我认为我遇到了新的问题,尽管我这次找到了匹配的对象。

If ($_ -match $item)似乎运行得很好。

If ($_ -notmatch $item)似乎不起作用。

我想我知道为什么,但我不完全确定。它可能会匹配ForEach ($item in (Get-Module -ListAvailable *).Name | Get-Unique)中找到的每个匹配项,并以某种方式导致问题??

感谢你到目前为止的帮助。它帮助我进步了很多!

代码语言:javascript
复制
Function imods {
Write-Host; CLS

$AddModulesArray = @(
"Convert",
"Get-UserProfile",
"Get-WindowsVersion",
"PoshInternals",
"PSConsoleTheme",
"PSSpeedTest",
"UpdateOS",
"WindowsPSModulePath",
"WindowsConsoleFonts",
"xFailOverCluster"
)

ForEach ($item in (Get-Module -ListAvailable *).Name | Get-Unique) {
$_ = $item
$AddModulesArray | ForEach-Object {
If ($_ -notmatch $item) {
  Write-Host
  Write-Host Installing $_
  Install-Module -Name $_
  Read-Host -Prompt "Press Enter to continue"
  }
Else {
If ($_ -match $item) {
  Write-Host
  Write-Host $_ Already Exists
  Read-Host -Prompt "Press Enter to continue"
    }
   }
 }
}
}

此外,我知道使用`分隔行并不是很好的做法,但我发现这样读起来更容易。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-20 14:37:04

您可以极大地简化这一过程,只需循环遍历要安装的模块列表,而不是所有模块,即可加快速度。

代码语言:javascript
复制
function imods {
    # Clear screen
    Clear-Host

    # Modules we need
    $modulesArray = @(
        "Convert",
        "Get-UserProfile",
        "Get-WindowsVersion",
        "PoshInternals",
        "PSConsoleTheme",
        "PSSpeedTest",
        "UpdateOS",
        "WindowsPSModulePath",
        "WindowsConsoleFonts",
        "xFailOverCluster"
    )

    # Rather than looking through both arrays, just loop through the ones we care about
    foreach($mod in $modulesArray) {
        if(Get-Module -ListAvailable $mod) {
            # Module exists
            Write-Host "Module '$mod' is already installed"
        } else {
            # Module does not exist, install it
            Write-Host "Installing '$mod'"
            Install-Module $mod
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61905695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档