Reputation: 327
I am trying to automate below process using .bat file :
NOTE : Below posted code is not proper , they are the tried code by me .
.bat code :
@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 code below :
$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..."
}
}
After this I would like to see command prompt entry that Installing the feature .
Install .bat code :
@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
Installing PowerShell code below :
$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 }
Once Installed , I should get command prompt message saying that Feature is successfully Installed . Please help .
Upvotes: 0
Views: 2327
Reputation: 19704
So while checking if it's installed, you had a few pointless comments and declarations:
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
It's unnecessary to declare a function that is only called once. I think it's a bad practice to use the current directory as a temp folder; you should use a default or create one. Ultimately, your examples don't make sense so I tried to make some sense of it:
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
}
Upvotes: 1