Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Windows 7 Forum Vista Tutorials Tags

Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

How to pass values from script to external batch files

Reply
 
LinkBack Thread Tools Display Modes
Old 11-06-2008   #1 (permalink)
IT Staff
Guest


 
 

How to pass values from script to external batch files

I've a batch file and a ps1 script

Test1.bat
powershell somescripts.ps1
robocopy c:\ d:\ <filename.txt>

Somescripts.ps1
$filename = "abc.txt"

After ps1 completes, i wish to pass the $filename abc.txt *outside* to
robocopy command . Note that abc.txt was created in c:\ drive

How do i do that ? Must i use process.start method in the script ?



My System SpecsSystem Spec
Old 11-06-2008   #2 (permalink)
RickB
Guest


 
 

Re: How to pass values from script to external batch files

On Nov 6, 2:53*am, "IT Staff" <jkk...@xxxxxx> wrote:
Quote:

> I've a batch file and a ps1 script
>
> Test1.bat
> powershell somescripts.ps1
> robocopy c:\ d:\ *<filename.txt>
>
> Somescripts.ps1
> $filename = "abc.txt"
>
> After ps1 completes, i wish to pass the $filename abc.txt *outside* to
> robocopy command . Note that abc.txt was created in c:\ drive
>
> How do i do that ? Must i use process.start method in the script ?
Powershell doesn't have a chance to help you because the limiting
factor is Test1.bat.
CMD doesn't have functionality that lets you do what you want
directly.
I can think of 4 answers.
The simplest is to put 'robocopy c:\ d:\ <filename.txt>' in
somescripts.ps1.
The next simplest is to convert Test1.bat to PowerShell.
You could also run SomeScripts.ps1 outside Test1.bat and have it call
Test1.bat with your values as arguments.
If you really MUST use CMD the way you've described I can only think
of an ugly way.
Have somescripts.ps1 write the value to a file then use FOR /F in
your .bat to retrieve/use it.
My System SpecsSystem Spec
Old 11-06-2008   #3 (permalink)
Gerd Schneider
Guest


 
 

Re: How to pass values from script to external batch files

Just a slightly more convenient procedure for the "ugly way":
Write an external temp.bat from ps1 containing
set filename=abc.txt
and add the statement
call temp.bat
to your test1.bat. This makes %filename% directly available in your
test1.bat environment

But as Rick already recommends: Better migrate the whole job to
somescripts.ps1 and do not mix ps1 and bat if they need to interact.

--
Gerd


"RickB" wrote:
Quote:

> On Nov 6, 2:53 am, "IT Staff" <jkk...@xxxxxx> wrote:
Quote:

> > I've a batch file and a ps1 script
> >
> > Test1.bat
> > powershell somescripts.ps1
> > robocopy c:\ d:\ <filename.txt>
> >
> > Somescripts.ps1
> > $filename = "abc.txt"
> >
> > After ps1 completes, i wish to pass the $filename abc.txt *outside* to
> > robocopy command . Note that abc.txt was created in c:\ drive
> >
> > How do i do that ? Must i use process.start method in the script ?
>
> Powershell doesn't have a chance to help you because the limiting
> factor is Test1.bat.
> CMD doesn't have functionality that lets you do what you want
> directly.
> I can think of 4 answers.
> The simplest is to put 'robocopy c:\ d:\ <filename.txt>' in
> somescripts.ps1.
> The next simplest is to convert Test1.bat to PowerShell.
> You could also run SomeScripts.ps1 outside Test1.bat and have it call
> Test1.bat with your values as arguments.
> If you really MUST use CMD the way you've described I can only think
> of an ugly way.
> Have somescripts.ps1 write the value to a file then use FOR /F in
> your .bat to retrieve/use it.
>
My System SpecsSystem Spec
Old 11-06-2008   #4 (permalink)
IT Staff
Guest


 
 

Re: How to pass values from script to external batch files

I see, so it is better to put all batch statements into ps1 and have only
one script running ...



"Gerd Schneider" <GerdSchneider@xxxxxx> wrote in message
news:0DB8E844-7DD2-42A0-AB3C-05997D31DC66@xxxxxx
Quote:

> Just a slightly more convenient procedure for the "ugly way":
> Write an external temp.bat from ps1 containing
> set filename=abc.txt
> and add the statement
> call temp.bat
> to your test1.bat. This makes %filename% directly available in your
> test1.bat environment
>
> But as Rick already recommends: Better migrate the whole job to
> somescripts.ps1 and do not mix ps1 and bat if they need to interact.
>
> --
> Gerd
>
>
> "RickB" wrote:
>
Quote:

>> On Nov 6, 2:53 am, "IT Staff" <jkk...@xxxxxx> wrote:
Quote:

>> > I've a batch file and a ps1 script
>> >
>> > Test1.bat
>> > powershell somescripts.ps1
>> > robocopy c:\ d:\ <filename.txt>
>> >
>> > Somescripts.ps1
>> > $filename = "abc.txt"
>> >
>> > After ps1 completes, i wish to pass the $filename abc.txt *outside* to
>> > robocopy command . Note that abc.txt was created in c:\ drive
>> >
>> > How do i do that ? Must i use process.start method in the script ?
>>
>> Powershell doesn't have a chance to help you because the limiting
>> factor is Test1.bat.
>> CMD doesn't have functionality that lets you do what you want
>> directly.
>> I can think of 4 answers.
>> The simplest is to put 'robocopy c:\ d:\ <filename.txt>' in
>> somescripts.ps1.
>> The next simplest is to convert Test1.bat to PowerShell.
>> You could also run SomeScripts.ps1 outside Test1.bat and have it call
>> Test1.bat with your values as arguments.
>> If you really MUST use CMD the way you've described I can only think
>> of an ugly way.
>> Have somescripts.ps1 write the value to a file then use FOR /F in
>> your .bat to retrieve/use it.
>>

My System SpecsSystem Spec
Old 11-07-2008   #5 (permalink)
RickB
Guest


 
 

Re: How to pass values from script to external batch files

If you find some reason to break up a script into pieces there is
nothing intrinsically wrong with that. Just be sure the top most one
is PowerShell. IOW call a .bat from .ps1, not the other way around.

On Nov 6, 7:03*pm, "IT Staff" <jkk...@xxxxxx> wrote:
Quote:

> I see, so it is better to put all batch statements into ps1 and have only
> one script running ...
>
> "Gerd Schneider" <GerdSchnei...@xxxxxx> wrote in message
>
> news:0DB8E844-7DD2-42A0-AB3C-05997D31DC66@xxxxxx
>
>
>
Quote:

> > Just a slightly more convenient procedure for the "ugly way":
> > Write an external temp.bat from ps1 containing
> > *set filename=abc.txt
> > and add the statement
> > *call temp.bat
> > to your test1.bat. This makes %filename% directly available in your
> > test1.bat environment
>
Quote:

> > But as Rick already recommends: Better migrate the whole job to
> > somescripts.ps1 and do not mix ps1 and bat if they need to interact.
>
Quote:

> > --
> > Gerd
>
Quote:

> > "RickB" wrote:
>
Quote:
Quote:

> >> On Nov 6, 2:53 am, "IT Staff" <jkk...@xxxxxx> wrote:
> >> > I've a batch file and a ps1 script
>
Quote:
Quote:

> >> > Test1.bat
> >> > powershell somescripts.ps1
> >> > robocopy c:\ d:\ *<filename.txt>
>
Quote:
Quote:

> >> > Somescripts.ps1
> >> > $filename = "abc.txt"
>
Quote:
Quote:

> >> > After ps1 completes, i wish to pass the $filename abc.txt *outside* to
> >> > robocopy command . Note that abc.txt was created in c:\ drive
>
Quote:
Quote:

> >> > How do i do that ? Must i use process.start method in the script ?
>
Quote:
Quote:

> >> Powershell doesn't have a chance to help you because the limiting
> >> factor is Test1.bat.
> >> CMD doesn't have functionality that lets you do what you want
> >> directly.
> >> I can think of 4 answers.
> >> The simplest is to put 'robocopy c:\ d:\ *<filename.txt>' in
> >> somescripts.ps1.
> >> The next simplest is to convert Test1.bat to PowerShell.
> >> You could also run SomeScripts.ps1 outside Test1.bat and have it call
> >> Test1.bat with your values as arguments.
> >> If you really MUST use CMD the way you've described I can only think
> >> of an ugly way.
> >> Have somescripts.ps1 write the value to a file then use FOR /F in
> >> your .bat to retrieve/use it.- Hide quoted text -
>
> - Show quoted text -
My System SpecsSystem Spec
Old 11-11-2008   #6 (permalink)
Al Dunbar
Guest


 
 

Re: How to pass values from script to external batch files

Good advice, but, imho, the limitation still exists that the called script
cannot easily pass information back to the calling script, regardless of
which is batch and which is powershell. Given that powershell encapsulates
just about every feature of batch (and then some) the best approach would be
to convert everything to powershell.

It's a bit different with a combination, for example, of batch and vbscript.
Although vbscript is much better than batch at some things (complex logic,
object programming, text parsing, etc.), it is often easier to code some
things in batch (or just leave existing code in batch), especially in the
area of file processing.


/Al

"RickB" <rbielaws@xxxxxx> wrote in message
news:77080eae-62d5-42d4-8aef-05c1dffe149e@xxxxxx
If you find some reason to break up a script into pieces there is
nothing intrinsically wrong with that. Just be sure the top most one
is PowerShell. IOW call a .bat from .ps1, not the other way around.

On Nov 6, 7:03 pm, "IT Staff" <jkk...@xxxxxx> wrote:
Quote:

> I see, so it is better to put all batch statements into ps1 and have only
> one script running ...
>
> "Gerd Schneider" <GerdSchnei...@xxxxxx> wrote in
> message
>
> news:0DB8E844-7DD2-42A0-AB3C-05997D31DC66@xxxxxx
>
>
>
Quote:

> > Just a slightly more convenient procedure for the "ugly way":
> > Write an external temp.bat from ps1 containing
> > set filename=abc.txt
> > and add the statement
> > call temp.bat
> > to your test1.bat. This makes %filename% directly available in your
> > test1.bat environment
>
Quote:

> > But as Rick already recommends: Better migrate the whole job to
> > somescripts.ps1 and do not mix ps1 and bat if they need to interact.
>
Quote:

> > --
> > Gerd
>
Quote:

> > "RickB" wrote:
>
Quote:
Quote:

> >> On Nov 6, 2:53 am, "IT Staff" <jkk...@xxxxxx> wrote:
> >> > I've a batch file and a ps1 script
>
Quote:
Quote:

> >> > Test1.bat
> >> > powershell somescripts.ps1
> >> > robocopy c:\ d:\ <filename.txt>
>
Quote:
Quote:

> >> > Somescripts.ps1
> >> > $filename = "abc.txt"
>
Quote:
Quote:

> >> > After ps1 completes, i wish to pass the $filename abc.txt *outside*
> >> > to
> >> > robocopy command . Note that abc.txt was created in c:\ drive
>
Quote:
Quote:

> >> > How do i do that ? Must i use process.start method in the script ?
>
Quote:
Quote:

> >> Powershell doesn't have a chance to help you because the limiting
> >> factor is Test1.bat.
> >> CMD doesn't have functionality that lets you do what you want
> >> directly.
> >> I can think of 4 answers.
> >> The simplest is to put 'robocopy c:\ d:\ <filename.txt>' in
> >> somescripts.ps1.
> >> The next simplest is to convert Test1.bat to PowerShell.
> >> You could also run SomeScripts.ps1 outside Test1.bat and have it call
> >> Test1.bat with your values as arguments.
> >> If you really MUST use CMD the way you've described I can only think
> >> of an ugly way.
> >> Have somescripts.ps1 write the value to a file then use FOR /F in
> >> your .bat to retrieve/use it.- Hide quoted text -
>
> - Show quoted text -

My System SpecsSystem Spec
Old 11-11-2008   #7 (permalink)
Al Dunbar
Guest


 
 

Re: How to pass values from script to external batch files

Good advice, but, imho, the limitation still exists that the called script
cannot easily pass information back to the calling script, regardless of
which is batch and which is powershell. Given that powershell encapsulates
just about every feature of batch (and then some) the best approach would be
to convert everything to powershell.

It's a bit different with a combination, for example, of batch and vbscript.
Although vbscript is much better than batch at some things (complex logic,
object programming, text parsing, etc.), it is often easier to code some
things in batch (or just leave existing code in batch), especially in the
area of file processing.


/Al

"RickB" <rbielaws@xxxxxx> wrote in message
news:77080eae-62d5-42d4-8aef-05c1dffe149e@xxxxxx
If you find some reason to break up a script into pieces there is
nothing intrinsically wrong with that. Just be sure the top most one
is PowerShell. IOW call a .bat from .ps1, not the other way around.

On Nov 6, 7:03 pm, "IT Staff" <jkk...@xxxxxx> wrote:
Quote:

> I see, so it is better to put all batch statements into ps1 and have only
> one script running ...
>
> "Gerd Schneider" <GerdSchnei...@xxxxxx> wrote in
> message
>
> news:0DB8E844-7DD2-42A0-AB3C-05997D31DC66@xxxxxx
>
>
>
Quote:

> > Just a slightly more convenient procedure for the "ugly way":
> > Write an external temp.bat from ps1 containing
> > set filename=abc.txt
> > and add the statement
> > call temp.bat
> > to your test1.bat. This makes %filename% directly available in your
> > test1.bat environment
>
Quote:

> > But as Rick already recommends: Better migrate the whole job to
> > somescripts.ps1 and do not mix ps1 and bat if they need to interact.
>
Quote:

> > --
> > Gerd
>
Quote:

> > "RickB" wrote:
>
Quote:
Quote:

> >> On Nov 6, 2:53 am, "IT Staff" <jkk...@xxxxxx> wrote:
> >> > I've a batch file and a ps1 script
>
Quote:
Quote:

> >> > Test1.bat
> >> > powershell somescripts.ps1
> >> > robocopy c:\ d:\ <filename.txt>
>
Quote:
Quote:

> >> > Somescripts.ps1
> >> > $filename = "abc.txt"
>
Quote:
Quote:

> >> > After ps1 completes, i wish to pass the $filename abc.txt *outside*
> >> > to
> >> > robocopy command . Note that abc.txt was created in c:\ drive
>
Quote:
Quote:

> >> > How do i do that ? Must i use process.start method in the script ?
>
Quote:
Quote:

> >> Powershell doesn't have a chance to help you because the limiting
> >> factor is Test1.bat.
> >> CMD doesn't have functionality that lets you do what you want
> >> directly.
> >> I can think of 4 answers.
> >> The simplest is to put 'robocopy c:\ d:\ <filename.txt>' in
> >> somescripts.ps1.
> >> The next simplest is to convert Test1.bat to PowerShell.
> >> You could also run SomeScripts.ps1 outside Test1.bat and have it call
> >> Test1.bat with your values as arguments.
> >> If you really MUST use CMD the way you've described I can only think
> >> of an ugly way.
> >> Have somescripts.ps1 write the value to a file then use FOR /F in
> >> your .bat to retrieve/use it.- Hide quoted text -
>
> - Show quoted text -

My System SpecsSystem Spec
Reply

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i pass values from external files IT Staff PowerShell 0 10-29-2008 10:42 PM
Pop-up to enter variables to pass to script Anatoli PowerShell 2 11-17-2007 01:38 AM
returning values to original script Frank PowerShell 1 05-25-2007 04:53 PM
Batch Script jon PowerShell 1 12-01-2006 07:30 PM
Run tasks asynchronously: external files vs. script blocks OK PowerShell 7 11-05-2006 01:38 PM


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53