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
Related
On the Command Prompt, I want to run a PowerShell script that is stored at a URL.
Here is what I have tried:
powershell -c "iex ((New-Object System.Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1'))"
powershell -Command "& iex (New-Object System.Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1')"
powershell -NoProfile -Command "iex ((New-Object System.Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1'))"
powershell.exe -exec Bypass -C "IEX (New-Object Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1')"
I have ran each of them for 5 minutes and nothing really showed the results I wanted. It displays no error but nothing really happen after waiting.
I want to know why the above scripts does not work as intended?
I will achieve the result I want by typing this instead:
echo IEX (New-Object Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1') | powershell -NoProfile -Command -
My question is similar to:
Run Powershell script from URL without temporary file
Other references:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1
https://gist.github.com/jivoi/c354eaaf3019352ce32522f916c03d70
There is more than one method, but here's a quick one-liner that should do the trick from the command prompt:
powershell -ExecutionPolicy Bypass -Command "[scriptblock]::Create((Invoke-WebRequest "https://gist.githubusercontent.com/ChrisKibble/afea9880a184cd2b2445e5d8408715af/raw/41cbbf042af07136132f09395e4664ffab33e310/gistfile1.txt").Content).Invoke();"
This creates a script block based on the content of a file hosted at a URL.
As to why yours don't work, it's tough to say without debugging it or doing some process monitoring, but my first guess would be something wrong with your PS1 file (try something simple like just a Write-Host).
I am trying to do something really basic in a batch script, but it is not working.
I want to open two PowerShell windows each with a different current working directory.
I am using the following script
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User1"
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User2"
Unfortunately it opens only the first window.
For sure it is only a syntax error but I could not find a solution yet.
As it happens often: I posted the question after researching a lot and then I immediately found the answer!
This is what you should write in your batch file:
START C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User1"
START C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User2"
I hope my answer will help someone in the same situation.
The Start command already has a 'working directory', option, /D.
#Start /D "C:\Users\User1" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit
#Start /D "C:\Users\User2" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit
You could of course do it with a simple For loop:
#For %%G In ("C:\Users\User1" "C:\Users\User2") Do #Start /D "%%~G" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit
start C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User1"
start C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd c:\Users\User2"
You just need to add "start" at the beginning
I have a batch script to run powershell, how to set path to ps1 file if this file is in the same folder as executing BAT file? I use this but not working.
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""./Reboot.ps1""' -Verb RunAs}";
Read call /? and Links relative to the Batch Script:
You can get the pathname of the batch script itself with %0,
parameter extensions can be applied to this so %~dp0 will return the
Drive and Path to the batch script.
Use … -File ""%~dp0Reboot.ps1"" … (note that %~dp0 includes a trailing backslash!)
With ./Reboot.ps you address the current working directory. What you need is the directory of the BAT file. Use %~d0%~p0/Reboot.ps instead.
An alternative could be to temporary set the working directory to the BAT's directory:
pushd %~d0%~p%"
rem run script with ./Reboot.ps1
popd
Obviously, if your script "Reboot.ps1" actually reboots the PC, you can skip the popd.
I have a batch script being that has the following code in it:
Here is the batch script (Test.bat):
call powershell.exe -executionpolicy remotesigned -File TheScriptToBeExecuted.ps1
exit /b
The Powershell script (TheScriptToBeExecuted.ps1) has some wait built into it. But when this file is being called, the batch script is NOT waiting for the Powershell script to finish.
I ever tried:
START /wait powershell.exe -executionpolicy remotesigned -File TheScriptToBeExecuted.ps1
To no effect.
When I double click on the Test.bat file, it seems to be waiting. I have checked out a lot of answers, but couldnt find any that correspond to this issue.
Please help.
start /wait powershell.exe -executionpolicy remotesigned -File TheScriptToBeExecuted.ps1 | ECHO > nul
Try the above. Holds the command until scripts executed, without the Teminate Batch Prompt. Use start /min /wait if not needing to display or interact with the script, or Alternately:
start /wait powershell.exe -executionpolicy remotesigned -WindowStyle Hidden -File TheScriptToBeExecuted.ps1 | ECHO > nul
-WindowStyle Value
Sets the window style for the session. Valid values are Normal, Minimized, Maximized and Hidden.
* | * pipes the output of the command, however as your just starting powershell.exe and not using it in the context of utilizing a command like follows, It shouldn't be an issue.
powershell -command "((Get-date).AddDays(0)).ToString(':dd:MM:yyyy') | set-content 'captureVar.txt'" && set /p Yesterday=<captureVar.txt
It worked for the script I tested, but it's not thoroughly tested.
let me know how it goes.
I think the issue is more likely to be within "TheScriptToBeExecuted.ps1" than your bat. In fact several powershell command are working as Asyncronous.
I tested a Sleep-Start, which is not Asyncronous at all using the following code and the bat is waiting the end of it.
This is the code I used in my .bat
REM RUNTEST.BAT
time /T
echo started
powershell -executionpolicy remotesigned -File C:\Users\Alex\Desktop\DA_PROVARE\test.ps1
time /T
echo done
exit /b
The ps1 content is just one row, here below:
Start-Sleep -Seconds 25
The problem you are facing is probably because of the account under which the scheduler runs. Have faced similar problems where the .bat and/or .ps1 doesn't behave the same if launched by a scheduler or other tools
powershell -ExecutionPolicy ByPass -command ". 'C:\path\TheScriptToBeExecuted.ps1'"
This should do the it, if not then we will add < NUL to the end
powershell -ExecutionPolicy ByPass -command ". 'C:\path\TheScriptToBeExecuted.ps1'" < NUL
By using dot sourcing, we are opening powershell in the cmd window with the powershell reference in the above solution. Then the period allows the script to run in cmd window also. I did a double test and made a .ps1 with start-sleep -s 3 in it, and the above did wait for the powershell script to finish.
method 2
Using Start /wait
START /wait PowerShell.exe "& "'C:\path\TheScriptToBeExecuted.ps1'"
This method will not run inside the cmd window. It opens a powershell terminal and runs there then goes back to the bat.
Have a requirement of using batch file in Cognos to upload the output in SharePoint by using Windows batch file we are scheduling.
Here is my script:
#ECHO OFF
IF EXIST "D:\Dev\close report name-en-in" Call Powershell.exe -executionpolicy remotesigned -File 'D:\Dev\close\pwrshell.ps1'
GOTO END
:END
PowerShell script is used for uploading the script from source.
After running batch file the output is not uploaded to SharePoint (PowerShell script works fine when running alone).
Not sure if this works or not, but there is a simple error in your script.
Call Powershell.exe -executionpolicy remotesigned -File 'D:\Dev\close\pwrshell.ps1'
should be
Powershell.exe -executionpolicy remotesigned -File 'D:\Dev\close\pwrshell.ps1'
Here's some better ways to end the file.
exit /b [errorlevel]
This stops the script, and optionally set an errorlevel.
goto :eof
We don't need :EOF for this to work, goto :eof works standalone.