How to close cmd window after a batch job run - powershell

Hi I have written a batch file which will run through a scheduler. The batch file job is to truncate the data from a file. But the problem I am facing here is after the batch file ran the cmd window is not closing automatically. That is why next time scheduler won't able to run the batch job throwing an error" process can not access the file".
And also scheduler status is always showing "Running" .It should be " Ready" state after completion of job. but the here job is not completing as the cmd window is opened.
Can anyone help me out how to exit from cmd window?
#echo off
powershell.exe -noexit -Command "Clear-Content -Force E:\Logs\pgbouncer.log";
exit %ERRORLEVEL%

-NoExit
Doesn't exit after running startup commands.
Powershell.exe Documentation
You have -noexit switch enabled. Use:
powershell.exe -Command "Clear-Content -Force E:\Logs\pgbouncer.log";

Related

How to auto close batch file after executing powershell script

I have a batch file to run a powershell script but I am unable to find a command to auto close the batch file after running it.
This is my batch file code:
powershell -ExecutionPolicy Bypass -File C1.ps1
I tried the following code to auto close batch file but it wouldn't.
timeout 2 >nul
exit
move nul 2>&0

I'm trying to run a powershell script from a batch file batch file but it appears to terminate the batch file

I have a PowerShell script that I'm trying to call in a batch file. The Powershell script renews some credentials used by a subsequent .exe file, which expire after an hour. I would then like to kill the PowerShell script, restart the PowerShell script, and restart the .exe. Here's a simple example that reproduces my problem:
Test.bat
::#echo off
title Started at %time%
pause
Call CallPSWrapper.bat
::Never hits this pause
pause
echo call the .exe here
CallPSWrapper.bat:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle hidden -ExecutionPolicy bypass -NoProfile -File .\MyPowerShell.ps1
MyPowerShell.ps1:
Write-Output "Waiting"
Start-Sleep -Seconds 30
I initially tried calling MyPowerShell.ps1 from test.bat, but tried changing it to being called by an intermediate batch file in the hope that PowerShell would only kill CallPSWrapper.bat.

Windows scheduler not executing task

The below command-line will successfully execute a powershell command invisibly; saving the output to a text file:
C:\Windows\System32\wscript.exe C:\temp\invisible.vbs C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -WindowStyle Hidden -command ( (Get-StartApps -Name 'RDP (Tools)').AppID > c:\Temp\AppB.txt )
invisible.vbs:
CreateObject("Wscript.Shell").Run "" & WScript.Arguments(0) & "", 0, False
However, if I create a Windows scheduler task where the Action is: EXE = C:\Windows\System32\wscript.exe and...
ARGUMENTS = C:\temp\invisible.vbs C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -WindowStyle Hidden -command ( (Get-StartApps -Name 'RDP (Tools)').AppID > c:\Temp\AppB.txt )
...the text file doesn't get created when I run the scheduler task; even though it says operation completed successfully (0x0).
How can I create a Windows scheduler task that does the same thing as from a command line?
HINT: When I run the scheduled task, a powershell.exe process is spawned by Task Scheduler. However, it doesn't appear to be doing anything. Something is causing the PowerShell process to not run as expected. Unfortunately, I can't tell what's happening.
HINT2: When I completely eliminate the VBScript; where Task Schedule executes only PowerShell; and, the respective command, it works fine. Unfortunately, I don't know vbscript well enough to know why this isn't working.
MKNANET,
FWIW: I had no problem running w/o the VBS as follows:
Task Scheduler--
Command: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Arguments: -noprofile -WindowStyle Hidden -command (Get-StartApps -Name "*Office*").AppID >> G:\Test\AppB.txt
Note: as I didn't have any 'RDP (Tools)' items in my app list I changed it to look for Office items.
HTH

How to fetch an already opened cmd window and run commands in it from a batch file?

There is a bat file in which a command (nqcmd) is executed. This command only executes, when we open the batch file as an administrator. Otherwise, an error comes.
I tried to run the batch file as administrator from powershell, like this,
powershell.exe -Command "start-process -filepath C:\foo.cmd -verb runas"
It opens two cmd windows - a normal cmd window, in which the batch file executes and an error comes:
'nqcmd is not recognized as an internal or external command, operable program or batch file',
and an administrator's cmd window.
But, in this window, that batch file does not executes. We have to type the batch file's name again and then it executes.
I wanted to ask that, in a separate batch file, can we take that instance of administrator's cmd window and then run commands in it, without manually typing in that window?
Thank you.

Get bat file to contiune when lauching PowerShell

I have a bat file that is launching a powerShell script. I would like for the bat file to keep moving after it launches the script and not wait for the powerShell script to complete. Every time right now when i launch the powerShell script the bat files waits till the powerShell script finishes before it moves on. Here is how I'm calling my powerShell script:
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "&'C:\Users\sharph\Desktop\test.ps1'"
SS64 'start' help page
You'll want to start it with the start command, like this;
start "" "PowerShell"
This will start a program without waiting for it to close, although that behavior can be re-added with the /w or /wait option. The blank "" is in place of the title, not always needed but generally a safe thing to add.
Perhaps this will work?
start "" "PowerShell" -NoProfile -ExecutionPolicy Bypass -Command "^& 'C:\Users\sharph\Desktop\test.ps1'"
of course, the & had to be delimited to ^&.