更新:每个retryW接受的解决方案的最终工作脚本。这是实现我的总体目标的最简单的方法。
##########################
### 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)中找到的每个匹配项,并以某种方式导致问题??
感谢你到目前为止的帮助。它帮助我进步了很多!
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"
}
}
}
}
}此外,我知道使用`分隔行并不是很好的做法,但我发现这样读起来更容易。
发布于 2020-05-20 14:37:04
您可以极大地简化这一过程,只需循环遍历要安装的模块列表,而不是所有模块,即可加快速度。
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
}
}
}https://stackoverflow.com/questions/61905695
复制相似问题