I am creating a script that will check if a file is in the downloads folder, silently install if found or silently download if not found.
Steps:
Look in the downloads folder for the .exe file
Silently install the .exe file if found
Silently download the .exe file from a URL if not found, then silently install
I am not sure how to silently perform the above, I keep getting a window that pops up showing the download. Here is my script, I am a beginner with PS so go easy on me
if ( Get-ChildItem -Path C:\Users\*\Downloads\7z1900.exe )
{
7z1900.exe /install /norestart /quiet
}
else {
start /quiet "https://www.7-zip.org/a/7z1900.exe"
}
How about using curl cmdlet?
curl http://some.url --output some.file
Or in your case
curl https://7-zip.org/a/7z1900-x64.msi --output 7z1900-x64.msi
Followed by
7z1900-x64.msi /quiet
(I have used the *.msi file, since the exe does not seem to support a silent installation)
Related
I am trying to create an intunewin file to update dell command update on all computers (via MS endpoint manager).
Dell CU will not install itself, if the older version of the app is present on the pc. Or rather it will install, but it won't run.
Solution - To create a powershell script, that first uninstalls the older versions of dell CU, and only then installs the newest one.
The code:
Remove-Item -Path "C:\Program Files\Dell\CommandUpdate" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -Path "C:\Program Files (x86)\Dell\CommandUpdate" -Recurse -Force -EA SilentlyContinue -Verbose
./Dell-Command-Update-Windows-Universal-Application_601KT_WIN_4.5.0_A00_01.EXE
This works just fine, when run like this on my computer. Actually I run the cmd script:
powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -File .\script.ps1
Where script.ps1 is the first script above.
So I have 3 files in the folder - the ps1 script, the cmd command, and the EXE file itself. From these 3 I create the intunewin file.
When pushed via intune, the app does not install itself. I can see 'downloading' notification, but never receive installation notification, neither successful nor failed one.
Can this be related to intune settings itself? The detection method and install command are most likely correct and were working before, when I was just using the exe file for intunewin creation.
I have to change this, because Dell CU won't install itself if the older version is there - as mentioned in the first sentence.
I assume this might be related to the powershell code. Maybe intune does not understand
./Dell-Command-Update-Windows-Universal-Application_601KT_WIN_4.5.0_A00_01.EXE
anymore, when it is given intunewin file instead?
If that's the case, how can I modify my script to 'make sense in intune'?
Thank you in advance for all the advices
I've built a few FFmpeg powershell scripts for me and a few others to use and I'm attempting to make the setup and update process as easy as possible. The end goal is to be able to run 1 batch file that installs Chocolatey, FFmpeg, git, clones the github repo (for updates), and edits the Windows registry to add the actual FFmpeg powershell scripts / console programs to the Windows Explorer contextual menu. This way I just pass them the folder containing everything once and any time I change or add something to the project I can just tell them to run the batch file again, and presto everything is up to date.
However I'm struggling to find a way to install Chocolatey, then git with Chocolatey, and then run a git command with the execution of a single .bat file. From what I can tell after installing Chocolatey I need to restart the shell entirely before I can install git, and then I have to restart the shell again before I can use a git command. As of right now most of the actual processing is happening via Powershell scripts that are launched from the .bat file, and as each step is taken I update a txt file, attempt to restart the batch script, and read the txt file to pick up where I left off:
#echo off
echo Administrative permissions required. Detecting permissions...
echo.
net session >nul 2>&1
if %errorLevel% == 0 (
echo Success: Administrative permissions confirmed.
echo.
) else (
echo Failure: Current permissions inadequate.
PAUSE
exit
)
set relativePath=%~dp0
set relativePath=%relativePath:~0,-1%
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%relativePath%\Setup\CheckRequiredPackages.ps1" -relativePath "%relativePath%"
set /p step=<"%relativePath%\Setup\Step.txt"
if %step% == 1 (
(echo 2) > "%relativePath%\Setup\Step.txt"
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%relativePath%\Setup\GetChocolatey.ps1"
start "" "%relativePath%\RunMe.bat"
exit
)
if %step% == 2 (
(echo 3) > "%relativePath%\Setup\Step.txt"
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%relativePath%\Setup\GetRequiredPackages.ps1"
start "" "%relativePath%\RunMe.bat"
exit
)
if %step% == 3 (
(echo 0) > "%relativePath%\Setup\Step.txt"
PowerShell -NoProfile -ExecutionPolicy Bypass -File "%relativePath%\Setup\Update.ps1" -relativePath "%relativePath%"
)
PAUSE
Exit
The problem is using the start command in the batch script doesn't seem to work, I'm guessing since that new process is spawned from the same process that handles the Chocolatey install it doesn't count as actually restarting the shell. Is there any way to actually restart the shell and somehow have the batch file start back up without user intervention?
I'm not sure why I didn't initially think of reloading the path environment variable but that's a whole lot more reasonable than restarting the script 4 times with an intermediary file.
Firstly I moved 99% of the heavy lifting from the .bat file to a Powershell script, as the only reason I'm using Batch is so the user can easily run the file by clicking it in Explorer. I couldn't get RefreshEnv to work, which is a feature of Chocolatey, but running this between each new package worked great:
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
So I have something like this now, and the Batch scrip just launches this Powershell Script:
Write-Host "Installing / updating required packages..."
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://chocolatey.org/install.ps1'))
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
choco install ffmpeg -y
choco install git -y
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
Write-Host "Deleting old files..."
Remove-Item -LiteralPath $relativePath -Force -Recurse
Start-Sleep 2
Write-Host "`nUpdating Files..."
git clone https://github.com/TheNimble1/FFmpegContextCommands.git $relativePath
Which installs Chocolatey, refreshes the path, installs FFmpeg & Git, refreshes the path, deletes the old files, and then clones the git to replace with new files.
Indeed, a start-launched process inherits the calling process' environment rather than reading possibly updated environment-variable definitions from the registry.
Chocolatey comes with batch file RefreshEnv.cmd (C:\ProgramData\chocolatey\bin\RefreshEnv.cmd, but C:\ProgramData\chocolatey\bin should be in the %PATH%) specifically to avoid having to start a new, independent session for environment updates to take effect.
Therefore, something like the following may work:
:: Assumes that Chocolatey was just installed to the default location.
call "%ProgramData%\chocolatey\bin\RefreshEnv.cmd"
:: If Chocolatey was *previously* installed and its installation directory
:: has already been added to %Path%, the following will do:
:: call RefreshEnv.cmd
call "%relativePath%\RunMe.bat"
Since Chocolatey is only being installed during your script's execution and its binaries folder is therefore not yet in %Path%, you'll have to call RefreshEnv.cmd by its full path, as shown above - which assumes the default install directory.
Your own answer now shows how to refresh the $env:Path (%Path%) environment variable using .NET methods directly _from PowerShell, which is a pragmatic solution.
Note, however, that RefreshEnv.cmd is more comprehensive in that it reloads all environment-variable definitions and therefore potentially newly added and modified ones.
Note that calling RefreshEnv.cmd from PowerShell does not work, because it then runs out of process (which means that it cannot update the calling process' environment).
However, Chocolatey offers an Update-SessionEnvironment PowerShell command (aliased to refreshenv), which you can make available immediately after a Chocolatey install as follows:
# Import the module that defines Update-SessionEnvironment aka refreshenv
Import-Module "$env:ProgramData\Chocolatey\helpers\chocolateyProfile.psm1"
# Refresh all environment variables.
Update-SessionEnvironment # or: refreshenv
See this answer for a more robust approach that doesn't rely on assuming that the default location was installed to.
We package our software to MSI files (using Wix).
We use VSTS for Builds and Releases.
Is there a standard way to deploy MSI file as part of the Release?
Yes, I can run msiexec /i ... as PowerShell or batch script. But we would need a few other things, for example checking exit code, uploading install log file back to VSTS Release or analysing error message, etc.
This all sounds like quite common thing people would like to do, but there is no such standard VSTS step \ extension for this.
I ended up packaging VSTS extension for this:
https://marketplace.visualstudio.com/items?itemName=ivanboyko.vsts-deploy-MSI
Source code is open-sourced:
https://github.com/IvanBoyko/vsts-install-MSI.git
You can specify log file in msiexec command to install MSI file, then check the detail log content (whether contains errors) by using PowerShell, if there are errors in the log, you can log error or warning by using ##vso[task.logissue].
Regarding upload log file, you can use ##vso[build.uploadlog]local file path to upload installer log file.
More information about logging commands, you can refer to this article: Logging Commands.
Simple code to install MSI and wait for the installer to finish:
$filePath='[msi file path]'
$DataStamp = get-date -Format yyyyMMddTHHmmss
$logFile = 'c:\{0}-{1}.log' -f 'nodejsInstall',$DataStamp
$MSIArguments = #(
"/i"
('"{0}"' -f $filePath)
"/qn"
"/norestart"
"/L*v"
$logFile
)
Start-Process "msiexec.exe" -ArgumentList $MSIArguments -Wait -NoNewWindow
I'm experiencing an error creating a logfile during an application installation script. This is the first time I've used PowerShell for this purpose. The reason for using it is that I need to dynamically build the option string during the installation, based on other software installed on the server.
The MCVE to generate the error is as follows:
$cmd = '"Installer.exe" /s /v"INSTALLDIR=D:\ OPTION_1=VALUE_1 OPTION_2=VALUE_2 OPTION_3=VALUE_3 REBOOT=ReallySuppress /L*v \"C:\Temp\Install_Log.log\" /qb"'
Invoke-Expression "cmd /c ${cmd}"
The error I receive is:
Error opening installation log file. Verify that the specified log
file location exists and is writable.
Note that /qb is being used for debugging purposes only. It will be changed to /qn prior to deployment; the script also fails when /qn is used.
However, when I open a Command Prompt in the correct folder and paste the contents of $cmd into it, the application installs as expected. It also works as expected if I remove /L*v \"C:\Temp\Install_Log.log\" from $cmd. The log path does exist. I have also attempted to create the log file prior to executing the installer, but this also failed.
I'm developing and testing this in PowerShell v2.0, if that matters, since some of the servers this will be deployed to have 2.0 installed.
I am trying to install “azure-powershell.0.8.7.msi” through a .cmd file using command
msiexec.exe /i ".\azure-powershell.0.8.7.MSI" /passive
This msi file is part of solution explorer(part of project, I have to do it in this way only).
Although I am able to install/uninstall when this msi file when it’s on local disk ( i.e. on some drive)
I tried to log the error it is:
“This installation package could not be opened. Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package.”
It is a known error of Microsoft. I tried each and every proposed solution on internet but it doesn’t work.
Note: The current user/admin of the system have all the access(read,write,modify).
if your MSI-file is in the same directory like the cmd-file you have to us the following command
msiexec /i "%~dp0azure-powershell.0.8.7.MSI" /qb
%~dp0 is refering to cmd-file directory and in this case to the MSI-file.
If you want to create a log-file use the /l and the logfilepath plus name after /qb.
For example:
msiexec /i "%~dp0azure-powershell.0.8.7.MSI" /qb /l*v %temp%\azure-powershell.log