Check if a program exists and if not to install it - bash

I am writing a Jenkins build with an NSIS file. I need a script to check if the device has a certain program installed on it and if it doesn't, it should install the .exe's from the installer folder. I have all the .exe's in the install folder required for the installer to work.
This should be rather simple but i am baffled in this case and could not find any information about this anywhere.

I suppose you are using a windows environment. You could write a powershell script to check it:
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName | Select-String 'Microsoft Visual C\+\+'
This will list all installed Visual C++ Redistributables
On my machine it looks like that:
#{DisplayName=Microsoft Visual C++ 2005 Redistributable (x64)}
#{DisplayName=Microsoft Visual C++ 2010 x64 Redistributable - 10.0.40219}
#{DisplayName=Microsoft Visual C++ 2012 x64 Additional Runtime - 11.0.61030}
#{DisplayName=Microsoft Visual C++ 2008 Redistributable - x64 9.0.30729.6161}
#{DisplayName=Microsoft Visual C++ 2005 Redistributable (x64)}
#{DisplayName=Microsoft Visual C++ 2008 Redistributable - x64 9.0.30729.17}
#{DisplayName=Microsoft Visual C++ 2013 x64 Additional Runtime - 12.0.21005}
#{DisplayName=Microsoft Visual C++ 2013 x64 Designtime - 12.0.21005}
#{DisplayName=Microsoft Visual C++ 2015 x64 Debug Runtime - 14.0.23506}
#{DisplayName=Microsoft Visual C++ 2015 x64 Minimum Runtime - 14.0.23506}
#{DisplayName=Microsoft Visual C++ 2013 x64 Minimum Runtime - 12.0.21005}
#{DisplayName=Microsoft Visual C++ 2012 x64 Debug Runtime - 11.0.60610}
#{DisplayName=Microsoft Visual C++ 2005 Redistributable (x64)}
#{DisplayName=Microsoft Visual C++ 2015 x64 Additional Runtime - 14.0.23506}
#{DisplayName=Microsoft Visual C++ 2013 x64 Debug Runtime - 12.0.21005}
#{DisplayName=Microsoft Visual C++ 2012 x64 Minimum Runtime - 11.0.61030}
So if your program you want to check is called ProgramWithName just use:
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName | Select-String ProgramWithName
If you want to know if the SQL Server is installed use:
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName | Select-String 'Microsoft SQL Server 2014 Express'
this gives you on my machine:
#{DisplayName=Microsoft SQL Server 2014 Express LocalDB }

Related

Visual Studio 2015 Installer doesn't install cl.exe

I had installed Visual Studio 2017 on my machine with VC2015.3 C++ compiler toolset. The VC14 compiler is now located in C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\bin\cl.exe
Now, when I install Visual Studio 2015 and select to install "Common Tools for Visual C++ 2015" there is no cl.exe in C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin as expected and as it always have been.
It's just like the installer "know" that I have already VC14 compiler installed (through VS2017) and skips it completely.
What is the issue here?

CMake -G Ninja on Windows specify x64

I am using CMake on Windows with Ninja generator
cmake -G Ninja ..
This uses the default Windows x86 toolchain.
How to specify x64 using the Ninja generator?
PS: I know how to generate x64 with Visual Studio
cmake -G "Visual Studio 12 2013 Win64 ..
You have to set the compiler environment accordingly before calling Ninja generation. If you have Visual Studio 2013 installed at the standard installation path you call:
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x64
cmake.exe -G "Ninja" ..
Edit: Thanks for the hint from #Antwane: "Or simply run CMake command from a Microsoft Visual Studio Command Prompt (x64). A shortcut to this prompt is located in Start Menu".
The naming varies over the Visual Studio versions:
When I then look into the generated CMakeCache.txt file I see:
...
//CXX compiler
CMAKE_CXX_COMPILER:FILEPATH=C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/amd64/cl.exe
...
//Flags used by the linker.
CMAKE_EXE_LINKER_FLAGS:STRING= /machine:x64
...
//Path to a program.
CMAKE_LINKER:FILEPATH=C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/amd64/link.exe
...
When I tried to run cmake on command line in Windows, trying to use Ninja and targetting the Visual Studio 14.0 compiler (2015), it kept picking up on other installed compilers (in my case gcc) instead.
The following command line worked:
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER="cl.exe" -DCMAKE_CXX_COMPILER="cl.exe" -DMSVC_TOOLSET_VERSION=140 ..
This answer assumes you have Visual Studio installed and have installed all the proper C++ libraries and what not when you installed visual studio.
Start typing in "x64 Native Tools" in your start menu. Open the file location and you will see a certain amount of shortcuts. On my visual studio installation I only have compilers for the x86/x64 architecture.
Anyway now that you have opened the file location you will be presented with a bunch of developer command prompt shortcuts:
PS C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\VC> ls
Directory: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\VC
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2/16/2021 8:12 PM 2139 x64 Native Tools Command Prompt for VS 2019 Preview.lnk
-a--- 2/16/2021 8:12 PM 2197 x64_x86 Cross Tools Command Prompt for VS 2019 Preview.lnk
-a--- 3/3/2021 9:01 PM 2139 x86 Native Tools Command Prompt for VS 2019 Preview.lnk
-a--- 2/16/2021 8:12 PM 2197 x86_x64 Cross Tools Command Prompt for VS 2019 Preview.lnk
PS C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\VC>
Let me explain what each one does:
x64 Native Tools Command Prompt for VS 2019 Preview
Use x64 compiler to compile for a x64 machine
x64_x86 Cross Tools Command Prompt for VS 2019 Preview
Use x64 compiler to compile for a x86 machine
x86 Native Tools Command Prompt for VS 2019 Preview
Use x86 compiler to compile for a x86 machine
x86_x64 Cross Tools Command Prompt for VS 2019 Preview
Use x86 compiler to compile for a x64 machine
In my opinion the last 2 are useless in the modern day. And have caused multiple CI crashes on our servers. So we only use 64 bit binaries.
Anyway you double click on one of the shortcuts:
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.10.0-pre.1.0
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview>
And bang you will have a command prompt loaded with an environment CMake can recognize. This is because the PATH environment variable now contains the compilers and what not.
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview>echo %PATH%
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\\Extensions\Microsoft\IntelliCode\CLI;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.29917\bin\HostX64\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\bin\Roslyn;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Team Tools\Performance Tools;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\\x64;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\Tools\devinit;C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64;C:\Program Files (x86)\Windows Kits\10\bin\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\\MSBuild\Current\Bin;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\Tools\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\LLVM\bin;C:\Program Files\Git\cmd;C:\Program Files\PowerShell\7\;C:\Program Files\CMake\bin;D:\Installations\Python\Scripts\;D:\Installations\Python\;C:\Users\juanr\AppData\Local\Microsoft\WindowsApps;D:\Installations\Microsoft VS Code\bin;C:\Users\juanr\AppData\Local\GitHubDesktop\bin;D:\Git\ninja;;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\Llvm\x64\bin;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja
Now you can run cmake with Ninja:
NOTE: In this example I'm manually specifying the path to Ninja. You can also just add Ninja to your path. And depending on your visual studio installation this may already be done for you. If you download the "C++ CMake tools for Windows" you have Ninja added to your path for you. You even get CMake added for you.
cmake -S . -B build -G "Ninja" -DCMAKE_MAKE_PROGRAM=C:/foobar/ninja.exe

Powershell code to list installed Windows software misbehaves when called as SYSTEM user

I have a simple powershell snippet to list installed Windows software and versions:
$msi = New-Object -ComObject WindowsInstaller.Installer
$msi.GetType().InvokeMember('ProductsEx', 'GetProperty', $null, $msi, ('', 's-1-1-0', 7)) | select #{ name='name'; expression={$_.GetType().InvokeMember('InstallProperty', 'GetProperty', $null, $_, ('ProductName')) } }, #{ name='version'; expression={$_.GetType().InvokeMember('InstallProperty', 'GetProperty', $null, $_, ('VersionString'))} }
When run as an ordinary user, this is the result:-
PS C:\Users\fmwebroot> c:\test.ps1
name version
---- -------
Microsoft Visual C++ 2008 Redistributable - x64... 9.0.21022
Microsoft Visual C++ 2010 x64 Redistributable ... 10.0.40219
Microsoft Visual C++ 2010 x86 Redistributable ... 10.0.40219
aws-cfn-bootstrap 1.4.5
EC2ConfigService 3.5.228.0
AWS Tools for Windows 3.7.543.0
Microsoft Web Deploy 3.5 3.1237.1764
IIS Advanced Logging 1.0 1.0.0625.10
Microsoft Web Platform Installer 5.0 5.0.50430.0
Mercurial 3.1.1 (x64) 3.1.1
ConEmu 140814.x64 11.140.8140
Microsoft Visual C++ 2008 Redistributable - x86... 9.0.21022
Opsview NSClient++ Windows Agent (x64) 0.3.9.336
New Relic Server Monitor 3.3.2.0
7-Zip 9.20 (x64 edition) 9.20.00.0
New Relic .NET Agent (64-bit) 3.10.43.0
Python 3.4.1 (64-bit) 3.4.1150
When run as a system user, the Python package's name is always missing. I see this on multiple machines. The OS is Windows Server 2012 R2.
PS C:\Users\fmwebroot> psexec -s cmd /c "echo . | powershell -File c:\test.ps1"
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
name version
---- -------
Microsoft Visual C++ 2008 Redistribu... 9.0.21022
Microsoft Visual C++ 2010 x64 Redis... 10.0.40219
Microsoft Visual C++ 2010 x86 Redis... 10.0.40219
aws-cfn-bootstrap 1.4.5
EC2ConfigService 3.5.228.0
AWS Tools for Windows 3.7.543.0
Microsoft Web Deploy 3.5 3.1237.1764
IIS Advanced Logging 1.0 1.0.0625.10
Microsoft Web Platform Installer 5.0 5.0.50430.0
Mercurial 3.1.1 (x64) 3.1.1
ConEmu 140814.x64 11.140.8140
Microsoft Visual C++ 2008 Redistribu... 9.0.21022
Opsview NSClient++ Windows Agent (x64) 0.3.9.336
New Relic Server Monitor 3.3.2.0
7-Zip 9.20 (x64 edition) 9.20.00.0
New Relic .NET Agent (64-bit) 3.10.43.0
3.4.1150
cmd exited on IP-0A63009C with error code 0.
PS C:\Users\fmwebroot>
I noticed this issue because this Powershell code is spawned by a running process to list installed software, and I couldn't work out why it kept thinking that Python was not installed. I managed to create this simple demo of the problem by analysing what was actually happening under the hood.
Can anyone tell me what's going on here, and how I could fix it?

installer error (windows 7 / vs2010)

When the installer reaches the point of trying to "tie in" to Visual Studio 2010, ends up generating an error.
Error running C:\Program Files\PlasticSCM5/client/tools/regpkg.exe
/root:Software\Micosoft\VisualStudio\10.0 /codebase "C:\Program
Files\PlasticSCM5/client/plasticVSextensibility.dll": regpkgL error:
Could not load file or assembly 'file:///C:\Program
Files\PlasticSCM5\client\plasticVSextensibility.dll' or one of its
dependencies. This assembly is built by a runtime newer than the
currently loaded runtime and cannot be loaded.
Machine:
Windows 7 SP1, 64bit
Visual Studio 2010 Professional SP1Rel
Microsoft .NET Framework 4.5.50709 SP1 (and 3.5 and 4.0 installed)
Anyway, this prevents me from getting the "Source Control" option for Plastic SCM in VS.
Anybody got a solution for this ?
Thanks in advance!

CMake Visual Studio Differences?

When I went to compile a project with CMake using MSVC++, I noticed something. CMake asked me to identify which copy of Visual Studio I wanted to use, and it presented me with the choices:
Visual C++ 2010
Visual C++ 10
Visual C# 2010
Visual C# 10
What is the difference between 2010 and 10?
The internal version number for VS2010 is version 10. Lucky coincidence. Version 11 won't be on your machine until 2012. Or later.
For creating a VS2010 project and compiling it from cmd, these are the options you could use:
cmake -G "Visual Studio 11 Win64" "%myProject%\src"
CD %HOMEDRIVE%\Windows\Microsoft.NET\Framework64\v4.0.30319
msbuild %myProject%\build\ALL_BUILD.vcxproj /detailedsummary /property:PlatformToolset=v110 /p:TargetFrameworkVersion=v4.5.1 /clp:ErrorsOnly /p:Configuration=Debug
msbuild %myProject%\build\ALL_BUILD.vcxproj /detailedsummary /property:PlatformToolset=v110 /p:TargetFrameworkVersion=v4.5.1 /clp:ErrorsOnly /p:Configuration=Release

Resources