header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Help calling a DOS .bat file from PowerShell
Last Post 25 Jan 2008 06:42 PM by halr9000. 10 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
24 Jan 2008 06:53 PM  

From a powershell command prompt or script; how would I run either of the following commands?  I want these commands to execute via command.com a.k.a. the DOS prompt. 

  1) Perl awstats.pl -config=extranet.mydomain.com -update

  2) HelloWorld.bat

The Perl command above runs from the command prompt but not from inside Powershell.  I would like my Poweshell script to therefore have it run outside of Powershell.

 

My blog: http://blogs.powershellcentral.com/kscriss/
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
24 Jan 2008 07:26 PM  
Let's back up a half-step here. The perl command should work, it may require tweaking as powershell has some special characters which sometime trip up command-lines that work in CMD. I'd like to see the error output when you try it.

Two, you can't execute batch files from powershell by design. Just like you can't run a vbscript (or well, Perl) script without the runtime, in this case, cmd.exe is the runtime engine for batch files. If you _really_ want to, you can certainly run this at a powershell prompt and it would work fine:

PS> cmd /c helloworld.bat
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
24 Jan 2008 07:27 PM  
Another thought, you may have a working directory issue where either perl or the awstats script can't find other files they need. The error message will help to determine what the deal is.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
24 Jan 2008 07:41 PM  

I will send the error when I can.  I am running it manually from the command prompt right now.  My team leader thinks it runs for a couple of hours when it is running correctly.

I like your "cmd /c helloworld.bat" idea.  I will also give that a try.  I was also thinking about some kind of "at" command as well.

You are right the perl script complains it can't find a file that it is trying to locate.  I tried "Set-Location", but it was not working.

My blog: http://blogs.powershellcentral.com/kscriss/
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
24 Jan 2008 08:19 PM  
Note to myself:

When writing out the .bat via the Powshell | OUT-FILE method, don't let it use the default encoding scheme as a .bat file won't work with this type of encoding. Switch it to | Out-File -encoding ASCII myfile.bat instead.

My blog: http://blogs.powershellcentral.com/kscriss/
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
24 Jan 2008 08:51 PM  
Your "cmd /c helloworld.bat" idea works for me. Thanks a million.

I created a .bat file with the perl script inside it. I then executed this bat file manually from a Powershell prompt and it is running currently. I belive I should also be able to call it (or invoke it?) from withing a powershelll script as well.
My blog: http://blogs.powershellcentral.com/kscriss/
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
24 Jan 2008 09:02 PM  
Obviously, you ideally want to get it working directly from PS. If you can grab that error message we can help to make it happen.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
bsonposhUser is Offline
Basic Member
Basic Member
Posts:393
Avatar

--
24 Jan 2008 09:39 PM  
I would be interested why you need .bat file at all. You can move all that to a PS1 file.
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
25 Jan 2008 05:03 PM  

Sorry guys for not posting the error message sooner as requested.  I really appreciate the help.  I kinda felt the need to sterilize it a bit though.  Below you will see the error I am recieving when I try to call perl directly.

Perl D:\Inetpub\Web\XXX\stats.XXX.YY.gov\wwwroot\cgi-bin\awstats.pl -config=extranet.YY.gov -update
Can't open perl script "D:\Inetpub\Web\XXX\stats.XXX.YY.gov\wwwroot\cgi-bin\awstats.pl -config=extranet.YY.gov -update":
 No such file or directory

We were recieveing this error on our 32-bit development box.  We have since moved the Powershell script to a dedicated 64-bit box which will be dedicated to our web statistics.  My team-leader says he has Perl in the system path now and it sould work better. 

My script copies the IIS logs of a couple hundred web sites and then Perl process them for the AWstats web site.  We will have a scheduled task, a .bat file, fore each site name, and we hope to be able to run more than one scheduled task at a time.  Inside the Powershell script that is scheduled by the scheduled task .bat file,  I am invoke another sitename.bat file to do the perl processing because I couldn't call Perl directly on my development box.  It would have been nice to skip that 2nd bat file for the Perl, but I have to keep on moving. 

2nd note to myself:  When calling the bat file from within Powershell you have to use tripple quotes it like so.

cmd /c """$HelloWorld.bat"""

My blog: http://blogs.powershellcentral.com/kscriss/
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
25 Jan 2008 05:09 PM  
Maybe this woud have worked?

$Perlstring = "awstats.pl -config=extranet.mydomain.com -update"
Perl """$PerlString"""

I never tried it though.
My blog: http://blogs.powershellcentral.com/kscriss/
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
25 Jan 2008 06:42 PM  
Few things:
1. The problem isn't Perl not being in the path--your error msg isn't from Powershell, it's from perl.exe. The problem is that Perl can't find the script. The answer may be as simple as quoting the path to the script, or it may be more complicated.
2. Since the path is going to be dynamically generated by your script, you should probably pull it out into a variable which you can then get into a loop. e.g.

$id = 'xxx' # insert an actual one here
$domain = 'foo' # and here
$script="D:\Inetpub\Web\$id\stats.$id.$domain.gov\wwwroot\cgi-bin\awstats.pl"
perl $script -config="extranet.$id.gov" -update

Try something like that and let us if it errors out and what error you get. Then you can work on the code to create those variables above. That might be something like

$domains = Get-ChildItem D:\Inetpub\Web\ | where-object { $_.PsIncontainer }
$domains | foreach-object {
$domain = $_
# then the above code
}

3. Triple quotes? That may work but did you work that out logically or by trial and error? :) That may be able to be improved upon. I got the following to work with zero quotes, and the variable definitely has some spaces in it. *and*, it's passing it to a 16-bit executable. :)

cmd /c edit $profiledir\profile.ps1
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer