用于软件自动化的Powershell脚本


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:ProgramDataChocolatey")
{
### 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:ProgramDataChocolatey")
{
### 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:ProgramDataChocolatey")
{
### Installing Packagers
ForEach($PackageName in $Packages)
{
choco install $PackageName -y
}
}
Sir is it correct way iam getting missing statement block in switch statement clause

好的,我将修改脚本如下:

# Step 1) install Chocolatey when needed
if (-not (Test-Path -Path "$env:ProgramDataChocolateychoco.exe" -PathType Leaf)) {
# from https://chocolatey.org/install
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) 
}
# Step 2) define the array of packages you are offering
$Packages = 'googlechrome','firefox','codeblocks','windbg','nasm',
'explorersuite','pestudio','vscode','sysinternals','python'
# Step 3) define the Show-Menu function
function Show-Menu {
Clear-Host
Write-Host "**********************************************"
Write-Host "LIST OF SOFTWARES"
# write the options using the array of packages
for ($i = 0; $i -lt $Packages.Count; $i++) {
# {0,2} means right align with spaces to max 2 characters
Write-Host ('{0,2}. {1}' -f ($i + 1), $Packages[$i])
}
Write-Host " q. Exit the script"
Write-Host "*************************************************"
Write-Host
}
# Step 4) enter an endless loop you only exit if the user enters 'q'
while ($true) {
Show-Menu
$UserInput = Read-Host "Enter the software number to be installed"
# test if the user wants to quit and if so, break the loop
if ($UserInput -eq 'q') { break }
# test if the user entered a number between 1 and the total number of packages (inclusive)
if ([int]::TryParse($UserInput,[ref]$null) -and 1..$Packages.Count -contains [int]$UserInput) {
# here you install the chosen package using the array index number (= user input number minus 1)
$packageIndex = [int]$UserInput - 1
Write-Host "Installing $($Packages[$packageIndex])"
choco install $Packages[$packageIndex] -y
}
else {
$availableOptions = 1..$Packages.Count -join ','
Write-Host "Error in selection, choose $availableOptions or q" -ForegroundColor Red
}
$null = Read-Host "Press Enter to continue"
}

最新更新