If & end problem- calling Powershell script -

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Master001
Posts: 1
Joined: 13 Nov 2013 07:44

If & end problem- calling Powershell script -

#1 Post by Master001 » 13 Nov 2013 07:59

Hi, :)

I have made a simple batch script that checks for the server hostname

if the servername eguals srv012 then run powershell script (this powershell script changes the contents of a ini) and then run exe file and then waits 50 seconds and reboot. Some issues here: if the computername iguals the actual hostname, do nothing and to go to end with text !!INI CONTENTS NOT CHANGED!! something is wrong in my code.. please help. Thanks in advance


@echo off
if "%COMPUTERNAME%" == "srv012" goto :RUN
:RUN
Powershell.exe -executionpolicy remotesigned -File c:\gts\replace_hostname.ps1
c:\foldername\programname.exe
timeout /t 50
shutdown /r
:eof
echo !!INI CONTENTS Not CHANGED!!
PAUSE

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: If & end problem- calling Powershell script -

#2 Post by ShadowThief » 13 Nov 2013 08:07

I've never seen a %COMPUTERNAME% value that was lowercase. Also, you don't need the colon in front of the line label when you're calling it with a goto. Try making that line

Code: Select all

if /I "%COMPUTERNAME%"=="srv012" goto RUN

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: If & end problem- calling Powershell script -

#3 Post by foxidrive » 13 Nov 2013 08:10

Small change here - it will run the code in the parentheses only if the computername equals srv012, otherwise it will print message to the console and pause.

Code: Select all

@echo off
if /i "%COMPUTERNAME%" == "srv012" (
   Powershell.exe -executionpolicy remotesigned -File c:\gts\replace_hostname.ps1
   c:\foldername\programname.exe
   timeout /t 50
   shutdown /r
  goto :EOF
)
echo !!INI CONTENTS Not CHANGED!!
PAUSE

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: If & end problem- calling Powershell script -

#4 Post by foxidrive » 13 Nov 2013 08:12

ShadowThief wrote:you don't need the colon in front of the line label when you're calling it with a goto.


It's good practice to use it all the time, so you aren't scratching your head when it is needed and you forget. goto :EOF for example.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: If & end problem- calling Powershell script -

#5 Post by ShadowThief » 13 Nov 2013 08:23

foxidrive wrote:
ShadowThief wrote:you don't need the colon in front of the line label when you're calling it with a goto.


It's good practice to use it all the time, so you aren't scratching your head when it is needed and you forget. goto :EOF for example.

I honestly wasn't even aware it worked with the colon outside of goto :eof

Post Reply