我正在尝试使用.bat文件自动执行以下过程:
注意:以下发布的代码不正确,它们是我尝试过的代码。
.bat代码:
@echo off
title Check and Install MS Visual C++ Redistributable 2015
echo Checking MS Visual C++ Redistributable 2015 .....
@ECHO OFF
REM LOG LOCATION
SET LOGPATH=D:\Tool\InstallC++Log\
IF NOT EXIST %LOGPATH% MD %LOGPATH%
SET ThisScriptsDirectory=%~dp0
REM PowerShellScriptPath=C:\temp\Install_Program_MS_Visual_C++_x86.ps1
SET PowerShellScriptPath=C:\temp\Checking_Program.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'"; >> %LOGPATH%%INSTALLPATCH%.log
pause
C:\ temp \ Checking_Program.ps1代码如下:
$tempdir = Get-Location
$tempdir = $tempdir.tostring()
$appToMatch = 'Microsoft Visual C++ 2015 Redistributable (x86)'
$msiFile = $tempdir+"\microsoft.interopformsredist.msi"
$msiArgs = "-qb"
function Get-InstalledApps
{
if ([IntPtr]::Size -eq 4) {
$regpath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
}
else {
$regpath = @(
'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
)
}
Get-ItemProperty $regpath | .{process{if($_.DisplayName -and $_.UninstallString) { $_ } }} | Select DisplayName, Publisher, InstallDate, DisplayVersion, UninstallString |Sort DisplayName
}
$result = Get-InstalledApps | where {$_.DisplayName -like $appToMatch}
If ($result -eq $null) {
Write-Host "Could not find the Microsoft Visual C++ 2015 Redistributable (x86). Installing the Program..."
}
}
在此之后,我想看到安装功能的命令提示符输入。
安装.bat代码:
@echo off
title Installing MS Visual C++ Redistributable 2015
echo .....
@ECHO OFF
REM LOG LOCATION
SET LOGPATH=D:\Tool\InstallC++Log\
IF NOT EXIST %LOGPATH% MD %LOGPATH%
SET ThisScriptsDirectory=%~dp0
REM PowerShellScriptPath=C:\temp\Install_Program_MS_Visual_C++_x86.ps1
SET PowerShellScriptPath=C:\temp\Checking_Program_Install.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'"; >> %LOGPATH%%INSTALLPATCH%.log
pause
在下面安装PowerShell代码:
$computer = $env:COMPUTERNAME
$sourcefile = "C:\temp\C++\vc_redist.x86.exe"
Invoke-Command -ComputerName $computer -ScriptBlock {Start-Process 'C:\temp\C++\vc_redist.x86.exe' -ArgumentList /s -Verb runas -Wait }
安装完成后,我应该收到命令提示消息,说明功能已成功安装。请帮忙。
答案 0 :(得分:1)
因此,在检查是否已安装时,您会收到一些毫无意义的评论和声明:
INSTALL.CMD
@ECHO off
TITLE Check and Install MS Visual C++ Redistributable 2015
SET LOGPATH=D:\Tool\InstallC++Log
IF NOT EXIST %LOGPATH% MD %LOGPATH%
SET PowerShellScriptPath=C:\Temp\Install.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%PowerShellScriptPath%" >> %LOGPATH%\Redist.log
PAUSE
没有必要声明一次只调用一次的函数。我认为将当前目录用作临时文件夹是一种不好的做法;你应该使用默认值或创建默认值。最终,你的例子没有意义,所以我试着弄清楚它:
Install.ps1
#Requires -Version 3
$TargetApp = 'Microsoft Visual C++ 2015 Redistributable (x86)'
$RegPath = @('HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*')
If ([Environment]::Is64BitOperatingSystem)
{
$RegPath += 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
}
$Result = Get-ItemProperty -Path $RegPath |
Where-Object { $PSItem.DisplayName -eq $TargetApp -and
$PSItem.UninstallString } |
Select-Object -Property @('DisplayName','Publisher','InstallDate','DisplayVersion','UninstallString')
If ($Null -eq $Result)
{
Write-Output "Could not find '$TargetApp'. Installing the Program..."
$Params=@{
FilePath='C:\Temp\C++\vc_redist.x86.exe'
ArgumentList='/s'
Verb='RunAs'
Wait=$True
}
Start-Process @Params
}