Powershell/Bat file Merger - powershell

I'm a complete newbie when it comes to scripting Powershell but have a reasonable experience with writing Bat files.
I have a powershell script that unzips a bat, a reg file and a msi, the powershell script then launches the bat file, I dont think this is the best way to do it, is there a way to just get the powershell script to unzip and then install the msi and reg files rather than running a seperate bat file?
Here are the 2 files:
BAT:
#echo off
echo Installing Teamviewer, please wait...
msiexec /qn /i "C:\Temp\TeamViewer.msi" /passive
echo[
regedit /s C:\Temp\TeamViewer_Settings.reg
Pause
exit
PS:
$BackUpPath = "C:\Temp\Install.zip"
$Destination = "C:\Temp"
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::ExtractToDirectory($BackUpPath, $destination)
Start-Process "C:\Temp\installer.bat"
Is there a way to just run the powershell script and extract and install the required files?
Many thanks and be kind, this is my first Powershell script.

you use start-process to run the bat file so just use the same to run the msi file instead.
Since there isnt a powershell cmdlet to import regitry files you can use regedit.exe to import the reg file.
Start-Process -FilePath 'msiexec.exe' -ArgumentList "/qn /i 'C:\Temp\TeamViewer.msi' /passive" -Wait
Start-Process -Filepath 'regedit.exe' -ArgumentList "/s 'C:\Temp\TeamViewer_Settings.reg'"

Related

Dockerfile and MSI installation

I'm trying to install an application that is an MSI file during the build process of an image with Dockerfile.
I can get the MSI to install properly when I enter a PowerShell session on the container by running:
C:\Windows\Syswow64\msiexec.exe /i "C:\Folder\File Name with spaces.msi" /qn /l*v "C:\log.log"
I have tried everything I can think with various RUN commands using powershell and cmd in the dockerfile.
Anyone have any ideas if this is just a matter of properly escaping quotes or the spaces?
Thanks,
Using powershell you can install msi file using below piece of code.
$msiPath = "your msi file path"
$arguments = "/i `"$msiPath`" /quiet"
Start-Process msiexec.exe -ArgumentList $arguments -Wait
Use same quotes

generate a war file using Powershell

I am new to Powershell.I am trying to generate a war file in the location of jar.exe , any inputs highly appreciated.
I have tried to generate the war file using Start Process, But it is not generating the war file
$wargen=Start-Process -FilePath "cmd.exe" -ArgumentList "/K cd /d D:\Temp\ws\9.0\base\java\8.0\bin\jar.exe -cvf foo.war" -Verb "runas" -Verbose
You don't need to try so hard. It's not necessary to invoke Start-Process or cmd.exe to run a command. PowerShell is a shell; it can run commands. Just type the command at the PowerShell prompt and press Enter.
PS C:\> D:\Temp\ws\9.0\base\java\8.0\bin\jar.exe -cvf foo.war
I am wondering about why your example uses the runas shell verb to run the command as an elevated process; my guess is that this is unnecessary.

Select USB flash drive where the drive letter changes

I want to run a script from a USB drive that installs software on the computer the script is run on.
The problem is one of my programs is on the USB drive itself, but the drive letter changes on every computer I plug it into so the path for the file also changes.
Is there a way I can get the script to check what drive letter the USB drive has been assigned?
$out = "Z:\Pc\Powershell Script for maskiner utenfor domenet\Software\SMART.msi"
Start-Process -filePath "msiexec.exe" -ArgumentList "/i $out /quiet /norestart /l c:\SMARTinstallog.txt"
If you have PSv3+ you can use $PSScriptRoot as this contains the fully-qualified path to the directory that contains the script file.
If your script and software locations were:
Z:\Pc\Powershell Script for maskiner utenfor domenet\script.ps1
Z:\Pc\Powershell Script for maskiner utenfor domenet\Software\SMART.msi
$PSScriptRoot would be Z:\Pc\Powershell Script for maskiner utenfor domenet as this is the root folder for the script.
Using this your command would be:
$out = "$PSScriptRoot\Software\SMART.msi"
Start-Process -filePath "msiexec.exe" -ArgumentList "/i $out /quiet /norestart /l c:\SMARTinstallog.txt"

Run cmd from a given folder inside PowerShell script

Is there any possibility to run a cmd command from a different folder then the script home location (e.g. C:\ScriptHome)?
I mean e.g.
Cmd /C "C:\Test\test.exe"
but this exe should be called from e.g. "C:\RunTestExeHere"
Basically, it can be done in pure cmd, like cd "C:\RunTestExeHere" and after
C:\RunTestExeHere>C:\Test\test.exe
but can it be done in powershell?
Thank you!
Your best bet is to sandwich your external command between Push-Location and Pop-Location commands.
A simple example:
Push-Location -EA Stop C:\ # You'd use C:\RunTestExeHere instead
cmd /c dir # You'd use & "C:\Test\test.exe" instead
Pop-Location
Another option (the invocation syntax is less convenient):
Start-Process -Wait -NoNewWindow cmd -ArgumentList /c, dir -WorkingDirectory C:\

How to call .cmd file as administrator?

Please let me know how to call .cmd file as administrator from PowerShell script:
The second line below should open as Administrator from a PowerShell script:
Set-Location "C:\client\service"
Invoke-Item "C:\client\service\_install.cmd"
Then the command prompt should wait after execution. This needs to handle in PowerShell script not possible to write in _install.cmd file.
Batch-scripts runs in CMD.exe, so you need to start a CMD.exe process as admin.
Start-Process -FilePath "C:\Windows\System32\cmd.exe" -ArgumentList "/k","C:\client\service\_install.cmd" -Verb RunAs -Wait
Start-Process is the cmdlet to start a process
-FilePath "C:\Windows\System32\cmd.exe" starts cmd.exe process
-ArgumentList "/k","C:\client\service\_install.cmd" tells cmd to leave the console open after running the script (is this what you wanted? if not, replace with /c so the cmd-window will close when done). The second argument is your script.
-Verb RunAs tells Start-Process to start the process as admin (you will recieve a UAC-window if enabled)
-Wait tells Start-Process to wait until the process is finished. With cmd /k this means after you exited the command prompt. If you've changed that to cmd /c, then it waits until the script is done.
If you need to change the working directory inside the cmd-file, then you need to modify the .cmd, or write a wrapper-script, like:
#echo off
cd /d C:\client\service
C:\client\service\_install.cmd