I'm trying to shutdown/restart a remote server at 3AM using this script
shutdown /r /f /t $([int]([datetime]"03:00AM"-(Get-Date)).TotalSeconds)
But PS doesn't like it because it's going into the next day, if I run this instead it works:
shutdown /r /f /t $([int]([datetime]"11:59AM"-(Get-Date)).TotalSeconds)
I've looked around but can't figure out how to get this command to run at 3AM, anyone any ideas?
Check if "3:00AM" is in the past, then add 1 day:
$threeAM = [datetime]"03:00AM"
if($threeAM -lt (Get-Date)){
$threeAM = $threeAM.AddDays(1)
}
shutdown /r /f /t $([int]($threeAM - (Get-Date)).TotalSeconds)
Related
I have problem. All working on local machine but when use psexec i get error.
This is scrap of my .bat file
psexec.exe \%pc% cmd.exe /k for /f "tokens=2 delims=:" %%g in ('certutil -verifystore My ^| findstr /i "serial"') do echo %%g
On local cmd all its work but on remote i get error:
"At this point | was unexpected"
Could you help me?
Correct part of code
(...) in ('"certutil -verifystore My | findstr /i serial"') do (...)
I have a below script which is attached to a windows service on stop command:-
wmic service where name='%NS_SERVICE_NAME%' get ProcessId | more +1 > tmp.txt
set /p NS_PID=<tmp.txt
del tmp.txt
for /f %%i in ('wmic process where "(ParentProcessId=%NS_PID%)" get ProcessId ^| more +1') do (
for /f %%a in ('wmic process where "(ParentProcessId=%%i)" get ProcessId ^| more +1') do (
echo %%a|findstr "^[-][1-9][0-9]*$ ^[1-9][0-9]*$ ^0$">nul && TASKKILL /F /PID %%a || rem
IF %ERRORLEVEL% NEQ 0 do (
exit
)
)
)
which works fine on Windows 7 and service stops quickly in 1-2 seconds, but in case of Windows Server 2012 the service does not stop and keeps in stopping state unless the process related to it is ended.
The error which I get in logs is as below:- "No Instance(s) Available."
for /f "tokens=1-7 delims=,: " %a in ('query user ^| find /i "disc"') do logoff %b
This above code is used for logoff remote desktop users where state is "Disconnected" in windows 2003.It will work perfect when I run in command prompt. But it will not run when I made a .bat file or .cmd file in windows 2003.so may know where i am going wrong?
Inside batch files the percent signs used in the for replaceable parameters need to be escaped
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do logoff %%b
User585,
Yes, inorder to implement the for loop inside a bat/cmd session, you need to place the variable with
%%a
like this
for /f %%a in (.\hosts) do quser /server:\\%%a
I have a batch file I would like to search a folder for any xlsm file updated today.
If it finds an xlsm file from today, it then runs another bat file. If there is no updated file it should run a .vbs file (to send an email) and then exit.
Currently I have the following:
#echo off
set var=%date:~-10%
dir "*.xlsm"|find "%var%">nul&&CALL Update.bat||EXIT
I think I probably need to include some sort of IF/ELSE instead of the current method, but my skills are lacking..
EDIT:
The actual solution I've gone with, based on the answer from #Monacraft is:
#echo off
forfiles /p C:\ /m *.xlsm /d 0 /c "cmd /c call Update.bat"
if ERRORLEVEL 1 start FailEmail.vbs
If using windows 7 you could do forfiles:
#echo off
set found=FALSE
forfiles /p "C:\...[path to folder]" /m *.xlsm /d +1 /c "cmd /c Echo Updating...&CALL Update.bat&set found=TRUE"
if /i %found%==False (
start email.vbs
) else (
Exit
)
And that should work fine
Mona
This worked for me:
#echo off
forfiles /p C:\ /m *.xlsm /d 0 /c "cmd /c call Update.bat"
if ERRORLEVEL 1 start FailEmail.vbs
I think your code should work - some small changes here.
It will just echo the commands to the screen, for you to test.
One point to consider is that the DIR command will also match *.xls files in the short name.
#echo off
set "var=%date:~-10%"
dir "*.xlsm"|find "%var%">nul && (echo CALL Update.bat) || (echo start failemail.vbs)
pause
Is there any way I can reboot my computer after each component installation specified in RunOnceEx.CMD file?
I am creating a unattended setup disk for windows XP which would install some default software after installing windows on the system. I am using RunOnceEx.cmd file to define the software that needs to be installed, what I want is to reboot my the system after installation of each software.
Thanks and regards,
Yep there is. Although it's not a supported feature. I do something similar.
This might not be the the fanciest solution but it works reliably. The key is to stop the RunOnceEx process (rundll32.exe) before commencing the reboot procedure. If it's not stopped, Windows will stop all processes before shutting down in an unknown order. And if that order means killing our "Reboot" process before killing the RunOnceEx process it will continue on the next item on the RunOnceEx list before being killed (and thus aborted, which is not what we want).
The simple answer, add a reboot key that kills the RunOnceEx process and then reboots:
set %KEY%=HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnceEx
REG ADD %KEY%\009 /ve /D "Reboot.." /f
REG ADD %KEY%\009 /v 1 /D "cmd.exe /c taskkill.exe /f /im rundll32.exe & shutdown /r /t 0 /f" /f
This might leave remnant keys during next startup. So to make it look cleaner, add an instruction to remove the key manually before killing and rebooting:
set %KEY%=HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnceEx
REG ADD %KEY%\009 /ve /D "Reboot.." /f
REG ADD %KEY%\009 /v 1 /D "cmd.exe /c REG DELETE %key%\009 /va /f & taskkill.exe /f /im rundll32.exe & shutdown /r /t 0 /f" /f
Hope it helps.
Edit:
In XP you might have to use tskill instead of taskkill, but the principle is the same.