【发布时间】:2021-10-29 01:17:16
【问题描述】:
function show-menu
{
Clear-Host
write-host "**********************************************"
write-host "LIST OF SOFTWARES"
write-host " 1. googlechrome"
write-host " 2. firebox"
write-host " 3. CodeBlocks"
write-host " 4. windbg"
write-host " 5. nasm"
write-host " 6. explorer suite"
write-host " 7.pestudio"
write-host " 8.vscode"
write-host " 9. sysinternals"
write-host " 10. python"
write-host " q. Exit the script"
write-host " ************************************************"
}
do
{
show-menu
$UserInput = read-host "Enter the software number to be installed "
switch($UserInput)
{
1 {googlechrome;
pause
}
2 {firefox;pause}
3 {codeblocks;pause}
4 {windbg;pause}
5 {nasm;pause}
6 {explorersuite;pause}
7 {pestudio;pause}
8 {vscode;pause}
9{sysinternals;pause}
10{python;pause}
q {break}
default {write-host "Error in selection, choose 1,2,3,4,5,6,7,8,9,10 or q";pause}
}
}
while ($UserInput -ne 'q')
$Packages = 'googlechrome',
'firefox',
'codeblocks',
'windbg',
'nasm',
'explorersuite',
'pestudio',
'vscode',
'sysinternals',
'python'
If(Test-Path -Path "$env:ProgramData\Chocolatey")
{
### Installing Packagers
ForEach($PackageName in $Packages)
{
choco install $PackageName -y
}
}
Else
{
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]:: SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
###### package install stuff ###############
ForEach($PackageName in $Packages)
{
Choco install $PackageName -Y
}
}
脚本运行不正常。当我输入任何数字时,软件没有下载,但是当我退出时,软件正在下载如果我的脚本有任何问题,请告诉我怎么做,在我的系统中已经安装了一些软件,但脚本正在下载软件,但在安装脚本期间会给出软件已安装的消息。我需要在我的脚本中设置一些条件,如果软件已经安装在 pc 中,则脚本不应该下载软件,它只应该给出软件已经安装的消息。 请帮助我完成我的任务 谢谢你
谢谢你
enter code here
do
{
show-menu
$Packages = 'googlechrome','firefox','codeblocks', 'windbg','nasm','explorersuite','pestudio','vscode','sysinternals','python'
$UserInput = read-host "Enter the software number to be installed "
switch($UserInput)
{
case1: {googlechrome;pause}
If(Test-Path -Path "$env:ProgramData\Chocolatey")
{
### Installing Packagers
ForEach($PackageName in $Packages)
{
choco install $PackageName -y
}
}
Else
{
############### INSTALLING CHOCOLATEY ####################################################################################
########### Before Executing the script type the command in the powershell terminal to get the admin rights ##############
########## Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass ##################################################
Set-ExexutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]:: SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
###### package install stuff ###############
ForEach($PackageName in $Packages)
{
choco install $PackageName -Y
}
}
case2: {firefox;pause}
If(Test-Path -Path "$env:ProgramData\Chocolatey")
{
### Installing Packagers
ForEach($PackageName in $Packages)
{
choco install $PackageName -y
}
}
Sir is it correct way iam getting missing statement block in switch statement clause
【问题讨论】:
-
这个问题一直被问到.. 你可能和 [jhansi jatavath](jhansi jatavath) 是同一个人还是在同一个教室?正如我在之前的一个问题中所评论的那样,您的代码中的逻辑在哪里?您显示一个菜单,使用所选选项执行 nothing,然后安装某个变量
$Packages.. 中的所有内容 -
先生,实际上我们正在合作。可能她会问先生,我们是 power-shell 脚本的新手,我们不知道如何在该脚本中添加逻辑,所以请帮助先生,这对我们会有帮助。
-
真的没那么难,再想一想:你现在在循环中运行
Show-Menu函数,但只有当有人进入q时才退出。对于所有其他选项,什么都没有完成。只有当用户使用q(用于QUIT)退出该循环时,安装才会发生无论用户想要安装什么。我的提示:在循环中,您可以在其中测试用户输入的内容,然后在此处安装所选的软件包。删除while ($UserInput -ne 'q')之后的所有内容
标签: powershell automation menu display user-interaction