Have tried different approaches to make the SSMs job working with SCP command with perl script. but the job is going into loop with out having a result.
PS : The script is working fine with running from command prompt directly.
command used in the perl script:-
$Command = "scp -i D:\File1\RS2\DataFeed\Code\PrivateKey.ppk -s $InternalFile admin#sftp.world.com:$VendorName/$DestFileName";
system command used in perl
system($command);
While running the command directly from windows cmd it is correctly placing file to the SFTP. but while running this perl script from ssms agent job it seems not working and the job is keep running without any results.
Any possible leads to the actual errors will be much appreciated
Detailed Steps :
Job in SSMS :
Step :
DataFeed.cmd
%_Debug% echo off
cd /d %0\..
pushd .
setlocal
rem -----------------------------------------------------------------
rem Localize environment
rem -----------------------------------------------------------------
if exist DataFeed_Environment.cmd (
call DataFeed_Environment.cmd
) else (
echo DataFeed_Environment.cmd not found!!!
echo
goto CmdUsage
)
rem -----------------------------------------------------------------
rem Run perl package
rem -----------------------------------------------------------------
C:\Perl\bin\perl.exe DataFeedProd1.pl
if %ERRORLEVEL% NEQ 0 goto ErrorExit
goto Exit
rem -----------------------------------------------------
rem Command Usage
rem -----------------------------------------------------
:CmdUsage
Echo ---------------------------------------------------------------------
echo.
echo DataFeed.cmd
echo Wraps the call to DataFeed.pl,
echo mails log upon errors.
echo.
echo Usage:
echo DataFeed.cmd
echo.
echo ----------------------------------
rem endlocal
rem popd
rem exit 1
rem -----------------------------------------------------------------
rem Error exit
rem -----------------------------------------------------------------
:ErrorExit
echo DataFeedProd1.pl failed !!!
echo
rem endlocal
rem popd
rem exit 1
rem -----------------------------------------------------------------
rem Exit
rem -----------------------------------------------------------------
rem endlocal
rem popd
:Exit
rem exit 0
sub CopyDataFeedFileToSftp{
my ($DataFeedFileInternal, $DataFeedVendorName,$DataFeedFileName) = #_;
my($DestFileName)=$DataFeedFileName.".zip";
my($Command);
my($RetValue) = 1;
$Command = "C:\\Users\\hprasu\\Downloads\\OpenSSH-Win64\\scp.exe -i D:\\File1\\RS2\\DataFeed\\Code\\PrivateKey.ppk -s $DataFeedFileInternal a_Tne\#nasftp\.egencia.com:$DataFeedVendorName/$DestFileName";
$RetVal = &CallSystem($Command);
if ($RetVal == 0) {
&AppendFileToLog($TempFile);
&ErrorExit("Unable to copy data feed file using SCP command:\n".$Command);
}
}
The above perl method is executing the System command
You should:
have full path to perl.exe and your perl script in job's command
escape all special characters in interpolated strings for Perl and
use full path for scp command since
operating system don't know where scp.exe is located (until it in the $PATH):
check filesystem permissions for all files in the command and perl script. Job should has access those files.
So command would be
$Command = "full_path\\scp.exe -i D:\\File1\\RS2\\DataFeed\\Code\\PrivateKey.ppk -s $InternalFile admin\#sftp.world.com:$VendorName/$DestFileName";
Read this:
https://www.geeksforgeeks.org/perl-quoted-interpolated-and-escaped-strings/
In my script I need to work with the exit status of the non-last command of a pipeline:
do_real_work 2>&1 | tee real_work.log
To my surprise, $? contains the exit code of the tee. Indeed, the following command:
false 2>&1 | tee /dev/null ; echo $?
outputs 0. Surprise, because the csh's (almost) equivalent
false |& tee /dev/null ; echo $status
prints 1.
How do I get the exit code of the non-last command of the most recent pipeline?
Bash has set -o pipefail which uses the first non-zero exit code (if any) as the exit code of a pipeline.
POSIX shell doesn't have such a feature AFAIK. You could work around that with a different approach:
tail -F -n0 real_work.log &
do_real_work > real_work.log 2>&1
kill $!
That is, start following the as yet non-existing file before running the command, and kill the process after running the command.
I wrote a script that's executed by Task Scheduler every night at 23:00 to have NAS01 power on NAS02 if it's not already on, execute a GoodSync job to backup NAS01, and shutdown NAS02 upon completion. The script isn't pretty, but it does the job. The issue that I have is the timestamps. I set variables to set the format for the date and time, however, this will just reference the timestamp of the variable for the entire script. How do I keep the formatting, but be able to get the date and time at each point it's called?
As you can see from the script, there's a date/time format for the log filename, and a date/time format for the logging within the log file. What happens in my formatting:
Forced 24H time
Leading space in the hour is replaced with a '0' (IE " 6:30" becomes "06:30")
Time fomatting is HHMM for filename and HH:MM:SS for logging
Date formatting is YYYYMMDD for filename and YYYY/MM/DD for logging
Script:
#ECHO off
REM Change CMD window color and size.
color 37
mode 180,50
REM ### Set variables
SET IPADDRESS1=11.11.11.20
SET IPADDRESS2=10.0.3.1
SET HH=%time:~-11,2%
SET MM=%time:~-8,2%
SET SS=%time:~-5,2%
SET NAMETIMESTR=%HH: =0%%MM%
SET LOGTIMESTR=%HH: =0%:%MM%:%SS%
SET MYDATE=%DATE:~4,10%
SET NAMEDATESTR=%MYDATE:~6,4%%MYDATE:~0,2%%MYDATE:~3,2%
SET LOGDATESTR=%MYDATE:~6,4%/%MYDATE:~0,2%/%MYDATE:~3,2%
SET LOGFILE=C:\!SCRIPTS\logs\7xNAS02-GSync-%NAMEDATESTR%_%NAMETIMESTR%.txt
:START
REM ### Checks IF 7xNAS02 is online. IF not, power it on.
ECHO ################################################################################################################################ >> %LOGFILE%
ECHO %LOGDATESTR% %LOGTIMESTR% EXECUTING GSYNC SCRIPT >> %LOGFILE%
ECHO ################################################################################################################################ >> %LOGFILE%
ECHO.
ECHO This is an automated script to backup the storage array on 7xNAS01 onto 7xNAS02.
ECHO IF 7xNAS02 isn't powered on, the script will boot 7xNAS02, wait for it to come online,
ECHO and then execute the "STORAGE" backup job in GoodSync.
ECHO.
ECHO %LOGDATESTR% %LOGTIMESTR% Checking power status of 7xNAS02... >> %LOGFILE%
ECHO Checking power status of 7xNAS02...
ECHO.
ping -n 1 %ipaddress1% | findstr TTL && GOTO ALREADY_ON
ping -n 1 %ipaddress1% | findstr TTL || GOTO POWER_ON
:ALREADY_ON
REM Sets variable to dictate that the server was already on before executing the script.
ECHO.
ECHO %LOGDATESTR% %LOGTIMESTR% 7xNAS02 is already powered on and available. >> %LOGFILE%
ECHO 7xNAS02 is already powered on and available.
ECHO.
SET previously_off=1
GOTO EXECUTE_BACKUP
:POWER_ON
REM ### Powers on 7xNAS02
ECHO %LOGDATESTR% %LOGTIMESTR% 7xNAS02 is offline. Powering on now. >> %LOGFILE%
ECHO 7xNAS02 is offline. Powering on now.
ECHO.
SET previously_off=0
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\!SCRIPTS\7xNAS02-Power-On.ps1'" >> %LOGFILE%
ECHO %LOGDATESTR% %LOGTIMESTR% Pinging 7xNAS02 until it's available... >> %LOGFILE%
ECHO Pinging 7xNAS02 until it's available...
ECHO.
GOTO CHECK_IF_READY
:CHECK_IF_READY
REM ### Continually pings 7xNAS02 until it is online.
ping -n 1 %ipaddress1% | findstr TTL || GOTO CHECK_IF_READY
ping -n 1 %ipaddress1% | findstr TTL && GOTO EXECUTE_BACKUP
:EXECUTE_BACKUP
REM ### Executes GoodSync backup job, "STORAGE."
ECHO %LOGDATESTR% %LOGTIMESTR% Executing "STORAGE" backup job. >> %LOGFILE%
ECHO Executing "STORAGE" backup job.
ECHO.
C:\"Program Files"\"Siber Systems"\Goodsync\gsync sync "STORAGE" >> %LOGFILE%
IF %previously_off% == 0 (
ECHO.
ECHO %LOGDATESTR% %LOGTIMESTR% Sync job done. Shutting down 7xNAS02... >> %LOGFILE%
ECHO Sync job done. Shutting down 7xNAS02...
shutdown /m \\7xNAS02 /s /f /d p:0:0 /c "Executed by 7xNAS02-GSync-Script on 7xNAS01."
GOTO SHUTDOWNCHECK
)
IF %previously_off% == 1 (
ECHO.
ECHO %LOGDATESTR% %LOGTIMESTR% Sync job done. 7xNAS02 will stay powered on. >> %LOGFILE%
ECHO Sync job done. 7xNAS02 will stay powered on.
ECHO.
)
GOTO END
:SHUTDOWNCHECK
ping -n 5 %ipaddress1% >nul
IF ERRORLEVEL 1 (
REM ### 7xNAS02 has powered off.
ECHO.
ECHO %LOGDATESTR% %LOGTIMESTR% 7xNAS02 has powered off. >> %LOGFILE%
ECHO 7xNAS02 has powered off.
GOTO END
)
REM ### 7xNAS02 is still on. Looping to check IF the server has shut down.
ECHO ...
GOTO SHUTDOWNCHECK
:END
REM pause
ECHO %LOGDATESTR% %LOGTIMESTR% END OF SCRIPT >> %LOGFILE%
exit
EDIT: Thanks to #Mofi, the updated and working script is below.
#ECHO off
REM Change CMD window color and size.
color 37
mode 180,50
REM ### Set variables
SET IPADDRESS1=11.11.11.20
SET IPADDRESS2=10.0.3.1
CALL :GETDATETIME
SET LOGFILE=C:\!SCRIPTS\logs\7xNAS02-GSync-%NAMEDATESTR%_%NAMETIMESTR%.txt
:START
REM ### Checks IF 7xNAS02 is online. IF not, power it on.
ECHO ################################################################################################################################ >> %LOGFILE%
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% EXECUTING GSYNC SCRIPT >> %LOGFILE%
ECHO ################################################################################################################################ >> %LOGFILE%
ECHO/
ECHO This is an automated script to backup the storage array on 7xNAS01 onto 7xNAS02.
ECHO IF 7xNAS02 isn't powered on, the script will boot 7xNAS02, wait for it to come online,
ECHO and then execute the "STORAGE" backup job in GoodSync.
ECHO/
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% Checking power status of 7xNAS02... >> %LOGFILE%
ECHO Checking power status of 7xNAS02...
ECHO/
ping -n 1 %ipaddress1% | findstr TTL && GOTO ALREADY_ON
ping -n 1 %ipaddress1% | findstr TTL || GOTO POWER_ON
:ALREADY_ON
REM Sets variable to dictate that the server was already on before executing the script.
ECHO/
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% 7xNAS02 is already powered on and available. >> %LOGFILE%
ECHO 7xNAS02 is already powered on and available.
ECHO/
SET previously_off=1
GOTO EXECUTE_BACKUP
:POWER_ON
REM ### Powers on 7xNAS02
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% 7xNAS02 is offline. Powering on now. >> %LOGFILE%
ECHO 7xNAS02 is offline. Powering on now.
ECHO/
SET previously_off=0
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\!SCRIPTS\7xNAS02-Power-On.ps1'" >> %LOGFILE%
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% Pinging 7xNAS02 until it's available... >> %LOGFILE%
ECHO Pinging 7xNAS02 until it's available...
ECHO/
GOTO CHECK_IF_READY
:CHECK_IF_READY
REM ### Continually pings 7xNAS02 until it is online.
ping -n 1 %ipaddress1% | findstr TTL || GOTO CHECK_IF_READY
ping -n 1 %ipaddress1% | findstr TTL && GOTO EXECUTE_BACKUP
:EXECUTE_BACKUP
REM ### Executes GoodSync backup job, "STORAGE."
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% Executing "STORAGE" backup job. >> %LOGFILE%
ECHO Executing "STORAGE" backup job.
ECHO/
C:\"Program Files"\"Siber Systems"\Goodsync\gsync sync "STORAGE" >> %LOGFILE%
IF %previously_off% == 0 (
ECHO/
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% Sync job done. Shutting down 7xNAS02... >> %LOGFILE%
ECHO Sync job done. Shutting down 7xNAS02...
shutdown /m \\7xNAS02 /s /f /d p:0:0 /c "Executed by 7xNAS02-GSync-Script on 7xNAS01."
GOTO SHUTDOWNCHECK
)
IF %previously_off% == 1 (
ECHO/
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% Sync job done. 7xNAS02 will stay powered on. >> %LOGFILE%
ECHO Sync job done. 7xNAS02 will stay powered on.
ECHO/
)
GOTO END
:SHUTDOWNCHECK
ping -n 5 %ipaddress1% >nul
IF ERRORLEVEL 1 (
REM ### 7xNAS02 has powered off.
ECHO/
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% 7xNAS02 has powered off. >> %LOGFILE%
ECHO 7xNAS02 has powered off.
GOTO END
)
REM ### 7xNAS02 is still on. Looping to check IF the server has shut down.
ECHO ...
GOTO SHUTDOWNCHECK
:END
REM pause
CALL :GETDATETIME
ECHO %LOGDATESTR% %LOGTIMESTR% END OF SCRIPT >> %LOGFILE%
exit
:GETDATETIME
SET "HH=%time:~-11,2%"
SET "MM=%time:~-8,2%"
SET "SS=%time:~-5,2%"
SET "NAMETIMESTR=%HH: =0%%MM%"
SET "LOGTIMESTR=%HH: =0%:%MM%:%SS%"
SET "MYDATE=%DATE:~4,10%"
SET "NAMEDATESTR=%MYDATE:~6,4%%MYDATE:~0,2%%MYDATE:~3,2%"
SET "LOGDATESTR=%MYDATE:~6,4%/%MYDATE:~0,2%/%MYDATE:~3,2%"
GOTO :EOF
A simple solution for using region dependent but faster accessed DATE and TIME environment variables is the usage of a subroutine.
Append to end of your batch file after last command exit the lines:
:GetDateTime
SET "HH=%time:~-11,2%"
SET "MM=%time:~-8,2%"
SET "SS=%time:~-5,2%"
SET "NAMETIMESTR=%HH: =0%%MM%"
SET "LOGTIMESTR=%HH: =0%:%MM%:%SS%"
SET "MYDATE=%DATE:~4,10%"
SET "NAMEDATESTR=%MYDATE:~6,4%%MYDATE:~0,2%%MYDATE:~3,2%"
SET "LOGDATESTR=%MYDATE:~6,4%/%MYDATE:~0,2%/%MYDATE:~3,2%"
GOTO :EOF
This is the subroutine GetDateTime. And delete the lines at top of your batch file which also set those environment variables.
Above each line using either LOGDATESTR or LOGTIMESTR insert the line:
CALL :GetDateTime
Run in a command prompt window call /? for more information about this method to embed a batch file within a batch file and call it as subroutine.
One more hint: It is recommended to use echo/ instead of echo., see DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/
how can I get the exit code of a 7zip's operation?
for example:
7z x filename.zip -y
how can I get it?
at now, I do some operations in bat, but I can change language.
I found something with AHK but I cannot use that language.
ps: I'm on win server 2008 r2
ErrorLevel will be populated with the error code from the last command executed. If you were to check the ErrorLevel on the line right after the 7zip command, ErrorLevel will be populated with 7zip's exit code.
7z x filename.zip -y
echo %ErrorLevel%
This above will echo 7zip's exit code
7z x filename.zip -y
set "ExitCode=%ErrorLevel%"
echo %ErrorLevel%
This above will echo set's exit code
7z x filename.zip -y
set "ExitCode=%ErrorLevel%"
echo %ExitCode%
This above will echo 7zip's exit code
You could try using the $LastExitCode variable in PowerShell to find out the exit code of your command. It returns the exit code of the last Windows based program that was run.
For example
cmd /C exit 1
Write-Host $LastExitCode # 1
Example (batch):
7z x filename.zip -y
echo(%errorlevel%
Here is my pseudo:
Read from LIST
Ping to check if machine is awake | Report result
Check to see if SERVICE is running | Report result
NO? Is it stopped?
YES? Run it | Report result
NO? Must not be installed, remote install please! | Report result
This is driving me bonkers here... I can make the parts of this code work alone just fine, but when I combine it into a nested IF structure, it returns 'Network Error' and 'Running' if a machine does not have the service running...then a manual check at that machine shows it is still actually stopped. Same for machines without the service installed at all.Please tell me where I'm messing up.. Thanks!
#echo off
echo.
echo.
cls
set SERVICE=MyServ
for /f %%i in (\\127.0.0.1\c$\list.txt) do call :DOIT %%i
:DOIT
echo Checking %SERVICE% on %1
#ECHO off
ping -n 2 -w 1000 %1 >trial.txt
find "Reply from" trial.txt>nul
if %ERRORLEVEL% == 1 (echo %1 Network Unresponsive>> "\\127.0.0.1\c$\list_report.txt")
if %ERRORLEVEL% == 0 (
sc \\%1 query %SERVICE% | FIND "STATE" | FIND "RUNNING"
if %ERRORLEVEL% == 0 (echo %1 Running>> "\\127.0.0.1\c$\list_report.txt")
if not %ERRORLEVEL% == 0 (
sc \\%1 query %SERVICE% | FIND "STATE" | FIND "STOPPED"
if %ERRORLEVEL% == 0 (
sc \\%1 start %SERVICE%
echo %1 forced start>> "\\127.0.0.1\c$\list_report.txt""
)
if not %ERRORLEVEL% == 0 (
xcopy /f /y "\\127.0.0.1\!\theAPP.exe" "\\%1\c$\temp\"
\\127.0.0.1\!\psexec \\%1 -s -n 10 -d cmd /c "c:\temp\theAPP.exe"
echo %1 Installed>> "\\127.0.0.1\c$\list_report.txt""
)
)
)
this should fix the issue
for /f %%i in (\\127.0.0.1\c$\list.txt) do call :DOIT %%i
:DOIT
if not "%1"=="" (
echo Checking %SERVICE% on %1
echo.
ping -n 2 -w 1000 %1 | find "Reply from" >nul
if ERRORLEVEL 1 echo %1 Network Unresponsive>> \\127.0.0.1\c$\list_report.txt else (
sc \\%1 query %SERVICE% | find "RUNNING" > nul
if ERRORLEVEL 1 (
sc \\%1 start %SERVICE% > nul
echo %1 Forced Start>> \\127.0.0.1\c$\list_report.txt
) else (
echo %1 Running>> \\127.0.0.1\c$\list_report.txt)
xcopy /f /y \\127.0.0.1\c$\text.txt \\%1\c$\temp\ > nul
\\127.0.0.1\c$\psexec \\%1 -s -n 10 -d cmd /c "type c:\temp\text.txt"
echo %1 Installed>> "\\127.0.0.1\c$\list_report.txt")
)
)
part of the issue is that you have to test for if not %1="" at the top of the script. that is where you get the erroneous text..
then you had some nesting issues, so i used ELSE instead.
you will have to change the text.txt to your filename.exe (and omit the "type" command)
please check answer if this solves your problem
fyi, you will need to have administrator access on the remote machine so that you can turn on and off services