Windows Vista Forums
Vista Forums Home Join Vista Forums 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 Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Converting a BAT script into a PowerShell Script

Reply
 
Old 05-15-2009   #1 (permalink)
Tony Valenti


 
 

Converting a BAT script into a PowerShell Script

Hi all,
I am in the process of trying to convert a couple of our .BAT scripts into
powershell scripts, however, I'm running into a couple of pesky problems that
I haven't been able to figure out that are related to variable replacement.

Here's my script so far:
------------
$hostname = "A66R66"
$password = "password99"
$drive = "C:"

net user $hostname $password /add /expires:never /passwordchg:no
MKDIR $drive\Backups\$hostname
net share $hostname$=$drive\Backups\$hostname /GRANT:$hostname`,FULL
CACLS $drive\Backups\$hostname /e /p $hostname:f
------------

The problem that I am having is whenever there is a colon ( : ) touching a
variable, things do not do what I expect. For example, in the last line:
CACLS $drive\Backups\$hostname /e /p $hostname:f

it appears as $hostname:f is treated as the variable name instead of
$hostname .

Also, just so I know how to do this in the future, what would be the correct
syntax for putting a variable and a literal right next to each other, for
example:
echo $VARIABLEliteral

so that there will be no space between the two.

Thanks a ton!

My System SpecsSystem Spec
Old 05-15-2009   #2 (permalink)
tojo2000


 
 

Re: Converting a BAT script into a PowerShell Script

On May 15, 12:05*pm, Tony Valenti
<TonyVale...@xxxxxx> wrote:
Quote:

> Hi all,
> I am in the process of trying to convert a couple of our .BAT scripts into
> powershell scripts, however, I'm running into a couple of pesky problems that
> I haven't been able to figure out that are related to variable replacement.
>
> Here's my script so far:
> ------------
> $hostname = "A66R66"
> $password = "password99"
> $drive = "C:"
>
> net user $hostname $password /add /expires:never /passwordchg:no
> MKDIR $drive\Backups\$hostname
> net share $hostname$=$drive\Backups\$hostname /GRANT:$hostname`,FULL
> CACLS $drive\Backups\$hostname /e /p $hostname:f
> ------------
>
> The problem that I am having is whenever there is a colon ( : ) touching a
> variable, things do not do what I expect. *For example, in the last line:
> CACLS $drive\Backups\$hostname /e /p $hostname:f
>
> it appears as $hostname:f is treated as the variable name instead of
> $hostname .
>
> Also, just so I know how to do this in the future, what would be the correct
> syntax for putting a variable and a literal right next to each other, for
> example:
> echo $VARIABLEliteral
>
> so that there will be no space between the two.
>
> Thanks a ton!
Colons are used to separate the scope of a variable and its name (E.g.
$env:computername, $global:servers, $script:starttime)

You should be either escaping the colon or using $($variable) to
disambiguate the intended interpolation.

My System SpecsSystem Spec
Old 05-16-2009   #3 (permalink)
Charlie Russel - MVP


 
 

Re: Converting a BAT script into a PowerShell Script

NET commands are particularly fussy. However, enclosing in "double quotes",
or escaping the colon with a backquote seems to be the easiest solution.

As for combining Variables and literals into a single string, the + sign is
the solution to get them combined, but direct juxtaposition if PowerShell
could think it's a positional parameter. So:

PSH> $Drive="C:"
PSH> $Drive
C:
PSH> $pathone="temp"
PSH> $pathtwo=$Drive + "\" + $pathone
PSH> $pathtwo
C:\temp
PSH> copy somefile.txt $pathtwo + "\subdir"
will give you an error (it sees three parameters)
PSH> copy somefile.txt $pathtwo"\subdir"
PSH> dir C:\temp\subdir
somefile.txt


--
Charlie.
http://msmvps.com/blogs/xperts64
http://mvp.support.microsoft.com/profile/charlie.russel

"Tony Valenti" <TonyValenti@xxxxxx> wrote in message
news:B9563A1D-947C-4E4D-B785-52D5AE194F4D@xxxxxx
Quote:

> Hi all,
> I am in the process of trying to convert a couple of our .BAT scripts into
> powershell scripts, however, I'm running into a couple of pesky problems
> that
> I haven't been able to figure out that are related to variable
> replacement.
>
> Here's my script so far:
> ------------
> $hostname = "A66R66"
> $password = "password99"
> $drive = "C:"
>
> net user $hostname $password /add /expires:never /passwordchg:no
> MKDIR $drive\Backups\$hostname
> net share $hostname$=$drive\Backups\$hostname /GRANT:$hostname`,FULL
> CACLS $drive\Backups\$hostname /e /p $hostname:f
> ------------
>
> The problem that I am having is whenever there is a colon ( : ) touching a
> variable, things do not do what I expect. For example, in the last line:
> CACLS $drive\Backups\$hostname /e /p $hostname:f
>
> it appears as $hostname:f is treated as the variable name instead of
> $hostname .
>
> Also, just so I know how to do this in the future, what would be the
> correct
> syntax for putting a variable and a literal right next to each other, for
> example:
> echo $VARIABLEliteral
>
> so that there will be no space between the two.
>
> Thanks a ton!
My System SpecsSystem Spec
Old 05-16-2009   #4 (permalink)
Vadims Podans [MVP]


 
 

Re: Converting a BAT script into a PowerShell Script

Hi, Charlie!
Just to know: powershell has excellent cmdlet to create path from parts -
Join-Path. See:
[vPodans] $drive = "C:"
[vPodans] $pathone="temp"
[vPodans] join-path $drive $pathone
C:\temp
[vPodans] join-path C: temp
C:\temp
[vPodans]

therefore author may do this:
MKDIR $(Join-Path $drive Backups $hostname)
etc.

--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"Charlie Russel - MVP" <charlie@xxxxxx> rakstīja ziņojumā
"news:eyEx5ui1JHA.4632@xxxxxx"...
Quote:

> NET commands are particularly fussy. However, enclosing in "double
> quotes", or escaping the colon with a backquote seems to be the easiest
> solution.
>
> As for combining Variables and literals into a single string, the + sign
> is the solution to get them combined, but direct juxtaposition if
> PowerShell could think it's a positional parameter. So:
>
> PSH> $Drive="C:"
> PSH> $Drive
> C:
> PSH> $pathone="temp"
> PSH> $pathtwo=$Drive + "\" + $pathone
> PSH> $pathtwo
> C:\temp
> PSH> copy somefile.txt $pathtwo + "\subdir"
> will give you an error (it sees three parameters)
> PSH> copy somefile.txt $pathtwo"\subdir"
> PSH> dir C:\temp\subdir
> somefile.txt
>
>
> --
> Charlie.
> http://msmvps.com/blogs/xperts64
> http://mvp.support.microsoft.com/profile/charlie.russel
>
> "Tony Valenti" <TonyValenti@xxxxxx> wrote in message
> news:B9563A1D-947C-4E4D-B785-52D5AE194F4D@xxxxxx
Quote:

>> Hi all,
>> I am in the process of trying to convert a couple of our .BAT scripts
>> into
>> powershell scripts, however, I'm running into a couple of pesky problems
>> that
>> I haven't been able to figure out that are related to variable
>> replacement.
>>
>> Here's my script so far:
>> ------------
>> $hostname = "A66R66"
>> $password = "password99"
>> $drive = "C:"
>>
>> net user $hostname $password /add /expires:never /passwordchg:no
>> MKDIR $drive\Backups\$hostname
>> net share $hostname$=$drive\Backups\$hostname /GRANT:$hostname`,FULL
>> CACLS $drive\Backups\$hostname /e /p $hostname:f
>> ------------
>>
>> The problem that I am having is whenever there is a colon ( : ) touching
>> a
>> variable, things do not do what I expect. For example, in the last line:
>> CACLS $drive\Backups\$hostname /e /p $hostname:f
>>
>> it appears as $hostname:f is treated as the variable name instead of
>> $hostname .
>>
>> Also, just so I know how to do this in the future, what would be the
>> correct
>> syntax for putting a variable and a literal right next to each other, for
>> example:
>> echo $VARIABLEliteral
>>
>> so that there will be no space between the two.
>>
>> Thanks a ton!
>
My System SpecsSystem Spec
Old 05-16-2009   #5 (permalink)
Charlie Russel - MVP


 
 

Re: Converting a BAT script into a PowerShell Script

Yes, I know, but thanks for the reminder. I was simply trying to build a
"safe" example that would show how to get around the CMD conversion issue
the OP was having.

--
Charlie.
http://msmvps.com/blogs/xperts64
http://mvp.support.microsoft.com/profile/charlie.russel

"Vadims Podans [MVP]" <vpodans> wrote in message
news:ePYLdRj1JHA.4632@xxxxxx
Quote:

> Hi, Charlie!
> Just to know: powershell has excellent cmdlet to create path from parts -
> Join-Path. See:
> [vPodans] $drive = "C:"
> [vPodans] $pathone="temp"
> [vPodans] join-path $drive $pathone
> C:\temp
> [vPodans] join-path C: temp
> C:\temp
> [vPodans]
>
> therefore author may do this:
> MKDIR $(Join-Path $drive Backups $hostname)
> etc.
>
> --
> WBR, Vadims Podans
> MVP: PowerShell
> PowerShell blog - www.sysadmins.lv
>
> "Charlie Russel - MVP" <charlie@xxxxxx> rakstīja ziņojumā
> "news:eyEx5ui1JHA.4632@xxxxxx"...
Quote:

>> NET commands are particularly fussy. However, enclosing in "double
>> quotes", or escaping the colon with a backquote seems to be the easiest
>> solution.
>>
>> As for combining Variables and literals into a single string, the + sign
>> is the solution to get them combined, but direct juxtaposition if
>> PowerShell could think it's a positional parameter. So:
>>
>> PSH> $Drive="C:"
>> PSH> $Drive
>> C:
>> PSH> $pathone="temp"
>> PSH> $pathtwo=$Drive + "\" + $pathone
>> PSH> $pathtwo
>> C:\temp
>> PSH> copy somefile.txt $pathtwo + "\subdir"
>> will give you an error (it sees three parameters)
>> PSH> copy somefile.txt $pathtwo"\subdir"
>> PSH> dir C:\temp\subdir
>> somefile.txt
>>
>>
>> --
>> Charlie.
>> http://msmvps.com/blogs/xperts64
>> http://mvp.support.microsoft.com/profile/charlie.russel
>>
>> "Tony Valenti" <TonyValenti@xxxxxx> wrote in message
>> news:B9563A1D-947C-4E4D-B785-52D5AE194F4D@xxxxxx
Quote:

>>> Hi all,
>>> I am in the process of trying to convert a couple of our .BAT scripts
>>> into
>>> powershell scripts, however, I'm running into a couple of pesky problems
>>> that
>>> I haven't been able to figure out that are related to variable
>>> replacement.
>>>
>>> Here's my script so far:
>>> ------------
>>> $hostname = "A66R66"
>>> $password = "password99"
>>> $drive = "C:"
>>>
>>> net user $hostname $password /add /expires:never /passwordchg:no
>>> MKDIR $drive\Backups\$hostname
>>> net share $hostname$=$drive\Backups\$hostname /GRANT:$hostname`,FULL
>>> CACLS $drive\Backups\$hostname /e /p $hostname:f
>>> ------------
>>>
>>> The problem that I am having is whenever there is a colon ( : ) touching
>>> a
>>> variable, things do not do what I expect. For example, in the last
>>> line:
>>> CACLS $drive\Backups\$hostname /e /p $hostname:f
>>>
>>> it appears as $hostname:f is treated as the variable name instead of
>>> $hostname .
>>>
>>> Also, just so I know how to do this in the future, what would be the
>>> correct
>>> syntax for putting a variable and a literal right next to each other,
>>> for
>>> example:
>>> echo $VARIABLEliteral
>>>
>>> so that there will be no space between the two.
>>>
>>> Thanks a ton!
>>
My System SpecsSystem Spec
Old 05-16-2009   #6 (permalink)
Al Dunbar


 
 

Re: Converting a BAT script into a PowerShell Script


"Charlie Russel - MVP" <charlie@xxxxxx> wrote in message
news:eyEx5ui1JHA.4632@xxxxxx
Quote:

> NET commands are particularly fussy. However, enclosing in "double
> quotes", or escaping the colon with a backquote seems to be the easiest
> solution.
>
> As for combining Variables and literals into a single string, the + sign
> is the solution to get them combined, but direct juxtaposition if
> PowerShell could think it's a positional parameter. So:
>
> PSH> $Drive="C:"
> PSH> $Drive
> C:
> PSH> $pathone="temp"
> PSH> $pathtwo=$Drive + "\" + $pathone
> PSH> $pathtwo
> C:\temp
> PSH> copy somefile.txt $pathtwo + "\subdir"
> will give you an error (it sees three parameters)
> PSH> copy somefile.txt $pathtwo"\subdir"
> PSH> dir C:\temp\subdir
> somefile.txt
$pathtwo"\subdir" works in the specific case of there being no whitespace in
any of the components of the path. So also would $pathtwo\subdir because the
"\" could not be part of a variable name. I think "$pathtwo\subdir" would be
the better practice on the off-chance that $pathtwo contained whitespace.

But the OP's issue had more to do with the case where the first character of
the literal string was not a character that would terminate the preceding
variable name.

/Al
Quote:

>
>
> --
> Charlie.
> http://msmvps.com/blogs/xperts64
> http://mvp.support.microsoft.com/profile/charlie.russel
>
> "Tony Valenti" <TonyValenti@xxxxxx> wrote in message
> news:B9563A1D-947C-4E4D-B785-52D5AE194F4D@xxxxxx
Quote:

>> Hi all,
>> I am in the process of trying to convert a couple of our .BAT scripts
>> into
>> powershell scripts, however, I'm running into a couple of pesky problems
>> that
>> I haven't been able to figure out that are related to variable
>> replacement.
>>
>> Here's my script so far:
>> ------------
>> $hostname = "A66R66"
>> $password = "password99"
>> $drive = "C:"
>>
>> net user $hostname $password /add /expires:never /passwordchg:no
>> MKDIR $drive\Backups\$hostname
>> net share $hostname$=$drive\Backups\$hostname /GRANT:$hostname`,FULL
>> CACLS $drive\Backups\$hostname /e /p $hostname:f
>> ------------
>>
>> The problem that I am having is whenever there is a colon ( : ) touching
>> a
>> variable, things do not do what I expect. For example, in the last line:
>> CACLS $drive\Backups\$hostname /e /p $hostname:f
>>
>> it appears as $hostname:f is treated as the variable name instead of
>> $hostname .
>>
>> Also, just so I know how to do this in the future, what would be the
>> correct
>> syntax for putting a variable and a literal right next to each other, for
>> example:
>> echo $VARIABLEliteral
>>
>> so that there will be no space between the two.
>>
>> Thanks a ton!
>

My System SpecsSystem Spec
Old 05-18-2009   #7 (permalink)
Josh Einstein


 
 

Re: Converting a BAT script into a PowerShell Script

I don't have any data to back it up but I think the subexpressions are
probably pretty heavy. Another way of wrapping the variable that I find is
more consistent looking than escaping the colon (and also helps to make
embedded variables stand out better) is:

"${Key}:${Value}"

Josh

"tojo2000" <tojo2000@xxxxxx> wrote in message
news:90db3eff-5788-4693-8f19-58e7381d711e@xxxxxx
Quote:

> On May 15, 12:05 pm, Tony Valenti
> <TonyVale...@xxxxxx> wrote:
>
> Colons are used to separate the scope of a variable and its name (E.g.
> $env:computername, $global:servers, $script:starttime)
>
> You should be either escaping the colon or using $($variable) to
> disambiguate the intended interpolation.
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
help converting msdn windows update vbs script to powershell PowerShell
problem passing args to script 'There is no script engine for file extenstion' VB Script
Powershell script PowerShell
Powershell script help PowerShell
when run powershell script as windows service ,powershell fail PowerShell


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