Arrays 如何编写if exist语句块以检查是否已使用名称数组安装Powershell模块
更新:根据retryW接受的解决方案的最终工作脚本。这是实现我想要做的事情的最简单的方法Arrays 如何编写if exist语句块以检查是否已使用名称数组安装Powershell模块,arrays,powershell,if-statement,foreach,module,Arrays,Powershell,If Statement,Foreach,Module,更新:根据retryW接受的解决方案的最终工作脚本。这是实现我想要做的事情的最简单的方法 ########################## ### INSTALL PS MODULES ### ########################## Function inmod { Clear-Host $modulesArray = @( "Convert", "Get-UserProfile", "Get-WindowsVersion", "PoshInternals",
##########################
### 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
}
}
}
旧更新:回应李_Dailey
我已经使用他的一些观点/建议将我的脚本发展到以下要点
我想,尽管这次我找到了匹配的,但我还是遇到了新的问题
如果($\uMatch$item)
似乎工作正常
如果($\u1-notmatch$item)
似乎不起作用
我想我知道为什么,但我不完全确定。它可能正在匹配与ForEach($item in(Get Module-listavable*).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"
}
}
}
}
}
此外,我知道使用`分隔行被认为是不好的做法,但我发现这样更容易阅读。您可以通过只循环浏览您要安装的模块列表,而不是所有模块,来大大简化并加快这一过程
函数imods{
#清屏
清除主机
#我们需要的模块
$modulesArray=@(
“转换”,
“获取用户配置文件”,
“获取Windows版本”,
“PoshInternals”,
“PSConsoleTheme”,
“PSSpeedTest”,
“更新”,
“WindowsPSModulePath”,
“WindowsConsoleFonts”,
“xFailOverCluster”
)
#不要同时查看两个数组,只需循环查看我们关心的数组即可
foreach($mod in$modulesArray){
if(获取模块-列表可用$mod){
#模块存在
写入主机“模块'$mod'已安装”
}否则{
#模块不存在,请安装它
写入主机“正在安装“$mod”
安装模块$mod
}
}
}
您使用的是foreach
-循环收缩-就好像它是foreach对象
管道构造一样。那不行。[grin]例如,如果($item-eq$\uq),那么$\ucode>从哪里来?另外,$AddModulesArray
也不需要反勾号。这是一个没有它们的有效数组定义。。。你也可以省去逗号![grin]另外,请注意,在下面的ForEach($AddModulesArray中的项)Get Module
中,在)
之后没有分隔符或{}
。感谢您的建议和工作脚本示例。我更新了我原来的帖子,用我目前的工作脚本,模仿你的答案。再次感谢!