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 Tutorial - Has anyone sucessfully read the value of the $OFS variable?

Reply
 
Old 01-08-2008   #1 (permalink)
Bob Landau
Guest


 
 

Has anyone sucessfully read the value of the $OFS variable?

All the code I search in books and online will do something like this

$OFS = <some character>

but none of them first save the orignal value

The problem I'm having is the default OFS variable seems to be instanciated
on the fly

dir variable:OFS

will return an error stating it doesn't exist nor does the following see it

Get-Variable -Scope global -Name o*

so if you by habit use Set-PSDebug -Strict in your scripts it will error out.

According to About_Automatic_Variables all of these are set by Powershell
while I can see some of the "read-only" type variables being created on the
fly; I don't see why the OFS variable should be.

I would like to keep the user default value for OFS if I need to override
it. so that it can be restored i.e.

$oldOFS = $OFS
set-variable -name oldOFS -value $OFS

I feel like I'm missing something here but I don't see what or how to achive
this short of catching the error.

thx
bob

My System SpecsSystem Spec
Old 01-08-2008   #2 (permalink)
Tao Ma
Guest


 
 

Re: Has anyone sucessfully read the value of the $OFS variable?

You can use Test-Path to check if the ofs exist, if so, the PowerShell use
it as delimiter to concatenate the array element.
If it doesn't exist, PowerShell use blank space instead.

You can use Remove-Item variablefs to investigate above hehavior.

Best wishes.

Tao Ma

"Bob Landau" <BobLandau@xxxxxx> дÈëÓʼþ
news:B9F9BCDC-BA13-4A67-A374-9EFBF06103B4@xxxxxx
Quote:

> All the code I search in books and online will do something like this
>
> $OFS = <some character>
>
> but none of them first save the orignal value
>
> The problem I'm having is the default OFS variable seems to be
instanciated
Quote:

> on the fly
>
> dir variable:OFS
>
> will return an error stating it doesn't exist nor does the following see
it
Quote:

>
> Get-Variable -Scope global -Name o*
>
> so if you by habit use Set-PSDebug -Strict in your scripts it will error
out.
Quote:

>
> According to About_Automatic_Variables all of these are set by Powershell
> while I can see some of the "read-only" type variables being created on
the
Quote:

> fly; I don't see why the OFS variable should be.
>
> I would like to keep the user default value for OFS if I need to override
> it. so that it can be restored i.e.
>
> $oldOFS = $OFS
> set-variable -name oldOFS -value $OFS
>
> I feel like I'm missing something here but I don't see what or how to
achive
Quote:

> this short of catching the error.
>
> thx
> bob

My System SpecsSystem Spec
Old 01-08-2008   #3 (permalink)
Bruce Payette [MSFT]
Guest


 
 

Re: Has anyone sucessfully read the value of the $OFS variable?

If $OFS doesn't exist and an operation requires it, then the default value
of ' ' (a single space) is used. Depending on what you're doing, the
pattern:

# define an array we'll expand in a string later...
$array = 1,2,3

# expand the array with plus signs separating it...
$s = & { $OFS='+'; "$array" } # set $OFS in a nested scope

may be the easiest solution since you don't have to save OFS - variable
scoping takes care of it for you. Also, just to be clear, $OFS can be any
string. It doesn't need to be a single character:

PS (1) > $array = 1,2,3
PS (2) > & { $OFS = '"+"'; "`"$array`"" }
"1"+"2"+"3"

(And yes - it is a bug that $OFS isn't defined by default...)

-bruce

--
Bruce Payette [MSFT]
Principal Developer, Windows PowerShell
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx
My Book: http://manning.com/powershell

"Bob Landau" <BobLandau@xxxxxx> wrote in message
news:B9F9BCDC-BA13-4A67-A374-9EFBF06103B4@xxxxxx
Quote:

> All the code I search in books and online will do something like this
>
> $OFS = <some character>
>
> but none of them first save the orignal value
>
> The problem I'm having is the default OFS variable seems to be
> instanciated
> on the fly
>
> dir variable:OFS
>
> will return an error stating it doesn't exist nor does the following see
> it
>
> Get-Variable -Scope global -Name o*
>
> so if you by habit use Set-PSDebug -Strict in your scripts it will error
> out.
>
> According to About_Automatic_Variables all of these are set by Powershell
> while I can see some of the "read-only" type variables being created on
> the
> fly; I don't see why the OFS variable should be.
>
> I would like to keep the user default value for OFS if I need to override
> it. so that it can be restored i.e.
>
> $oldOFS = $OFS
> set-variable -name oldOFS -value $OFS
>
> I feel like I'm missing something here but I don't see what or how to
> achive
> this short of catching the error.
>
> thx
> bob

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
read strings to variable VB Script
How to read a user input value from command line and store it in a variable? VB Script
Re: How to display a variable value in a dialog box and how to read user input? VB Script
Read-Host issue, won't store to variable when using multiple read-host lines PowerShell
Sucessfully Installed Windows Server Code Name Long Horn Build 5600, Just FYI. Vista General


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