I'm trying to download an exe in my .bat file but I can't hide the output with
$progressPreference = 'silentlyContinue'
| Out-Null
and $null 2>&1.
I don't know why none of them worked.
This is the script that I'm using.
powershell "Invoke-WebRequest http://example.com/example.exe -OutFile "%UserProfileDir%\AppData\LocalLow\example_Data\example.exe"
It downloads the exe perfectly but I can't hide the output.
You can get inspired by this batch file :
#echo off
set URL=https://www.google.tn/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
set FileName=%~dp0Google_Logo.png
Call :Download %URL% %FileName%
Start "" %FileName% & Exit
::*********************************************************************************
:Download <URL> <FILE>
Powershell.exe -command "(New-Object System.Net.WebClient).DownloadFile('%1','%2')"
exit /b
::*********************************************************************************
Related
I try to open a ps1 powershell file thats updates a gitrepo
i have tryed
powershell -ExecutionPolicy Bypass -Command "& '%USERPROFILE%\folder1\update.ps1'
this is working but when it comes to a requirements.txt promt writes error of not found that file i think powershell is not inside this folder directly so it cant find that file what is needed
if i make this
cd %USERPROFILE%\ & REM First change to the batch file folder
echo Points as at = %time% %date%> updatebing_log.txt & REM create a simple logfile with time and end result of the batch run
echo ============================folder1=========================== >> updatebing_log.txt
cd folder1\
start powershell
cd %USERPROFILE%\folder1
powershell -ExecutionPolicy Bypass -Command "& '%USERPROFILE%\folder1\update.ps1'
echo Waiting seconds
timeout /t 10 /nobreak > NUL
it is working
but it opens a cmd (bash file) promt and a powershell then it cd to folder
powershell do nothing then
inside cmd i see that the ps1 file is executed and alsow works with the requirements.txt
As per my comment. Here's what your code is doing. Thus the response you're seeing.
# Run this in cmd.exe
cd %USERPROFILE%\ & REM First change to the batch file folder
echo Points as at = %time% %date%> updatebing_log.txt & REM create a simple logfile with time and end result of the batch run
echo ============================folder1=========================== >> updatebing_log.txt
cd folder1\
# Start a PowerShell interactive session
start powershell
cd %USERPROFILE%\folder1
# Start a new PowerShell session from cmd.exe and run this code
powershell -ExecutionPolicy Bypass -Command "& "$env:USERPROFILE\folder1\update.ps1" # note the profile change I made for you.
# Run this in cmd.exe on Powerhell exit
echo Waiting seconds
timeout /t 10 /nobreak > NUL
If that is not the flow you were after, then you need to rethink this.
i made this at work it looks likke it works but dont know i try to show the working directory but it want show it only shows were the ps1 call file is
i made a test.bat
powershell -ExecutionPolicy Bypass -Command "& '%USERPROFILE%\Brewd\updatebrewdall.ps1'"
and updatecall.ps1
cd "folder1\"
powershell -ExecutionPolicy Bypass -Command "& '.\update.ps1'"
Start-Sleep -Seconds 10
# now back to previous directory
cd ..\
cd "folder2\"
"$([Environment]::CurrentDirectory)\$ComputerName"
powershell -ExecutionPolicy Bypass -Command "& '.\update.ps1'"
Start-Sleep -Seconds 10
# now back to previous directory
cd ..\
cd "folder3\"
"$([Environment]::CurrentDirectory)\$ComputerName"
powershell -ExecutionPolicy Bypass -Command "& '.\update.ps1'"
Start-Sleep -Seconds 10
I am trying to create a 1 line code which downloads files and executes them. I have tried the & and && method to run more commands one after another. but I am having a hard time trying to put the second line with the first one using &.
powershell -Command "Invoke-WebRequest https://github.com/-----/unixC/archive/main.zip -OutFile I.zip" & powershell Expand-Archive I.zip & for /f %%a IN ('dir "I\unixC-main" /b') do move "I\unixC-main\%%a" "%cd%\"
#RD /S /Q "I" & del /q /f I.zip log.txt README.md & start unixC.bat & start /b "" cmd /c del "%~f0"&exit /b
Is there any way to make this snippet of code even shorter? and more compact?
Rasil
Like others have said, the whole thing could be done in powershell. Semicolon seperates commands. -command is the default so you can leave it out. Wget is a powershell alias for invoke-webrequest.
powershell wget https://github.com/-----/unixC/archive/main.zip -OutFile I.zip; Expand-Archive I.zip
Example powershell replacement for the whole thing:
Invoke-WebRequest https://github.com/-----/unixC/archive/main.zip -OutFile I.zip
Expand-Archive I.zip
move-item i\unixC-main\* $pwd # $pwd is optional
remove-item -recurse i
remove-item -force i.zip,log.txt,readme.md
.\unixC.bat
remove-item $args[0]
Try parenthesising for /f %%a IN ('dir "I\unixC-main" /b') do move "I\unixC-main\%%a" "%cd%\" and then append the &{second line}
Using the .bat file code below, I can get the value returned after running the program.
"C:\Program Files\www\www.exe" /start "/My app/abc" && echo %ERRORLEVEL% > c:\ResultCode.txt"
How to use the powershell code to achieve the same functionality
I use the code below and I didn't get the result.
Start-Process -FilePath "C:\Program Files\www\www.exe" -ArgumentList "/start `"/My Processes/abc`"" -RedirectStandardError "c:\ResultCode.txt"
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'"
Currently the following is my path for launching the VMware vSphere PowerCLI command prompt. I wish to run my sample.ps1 script automatically using a batch file. How can I incoporate sample.ps1 into this path and create a batch file?
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\""
If you are working with PowerShell 2.0, you can use the -file parameter of PowerShell.exe
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -file "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
If you are working with PowerShell 1.0, you can use -command parameter this way
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -command "& 'C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'"
echo off
Title,Report Script &color 9e
for /f "usebackq delims=$" %%a in (`cd`) do (
set SCRIPTDIR=%%a
)
(Set ScriptFile=%SCRIPTDIR%\Report.ps1)
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\";%ScriptFile%"
You can use this to launch arbitrary .ps1 scripts via .bat files by calling the bat file like your ps1. Then extract the name of file in batch and call powershell with it.
For a ready to use solution, use the following Gist: https://gist.github.com/JonasGroeger/10417237
I saw this code in another page, I test it in a W2012 R2 and it runs.
I hope it work:
C:\>powershell "C:\>1\file.ps1"