Re: [MSH] WMI Help



You mention that you thought about using $global:$class for clarity. I
would caution you against doing that. This means that you will always
access the variable in the global scope whether or not the script is
dot-sourced. Instead, consider using $script:$class. If the script is
dot-sourced into the global scope then $script:class is equivalent to
$global:class. If the script is not dot-sourced then you aren't poluting
the global scope at all but the variable would be available to the entire
script.

--
Jeff Jones [MSFT]
Microsoft Command Shell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.


"/\/\o\/\/" <no@xxxxxxxx> wrote in message
news:eMQfL5e4FHA.2676@xxxxxxxxxxxxxxxxxxxxxxx
> Jeffrey Snover [MSFT] wrote:
>> Impressive stuff!
>> BTW - when you dot source the file, all side effects occur in the current
>> scope. What that means is that in addition to $classes being defined so
>> was $MS, $OPT, $MC and all the other "worker" variables that you used.
>> You can avoid this by moving the line
>> $classes = @{}
>> to the top and then putting the rest of the logic in a
>> &{
>> }
>> construct. This says, "Execute this script block in a new scope". All
>> the worker variables are created in a worker scope which gets discarded
>> at the end of the script block. This script block will not have $classes
>> defined so it will search the stack and find it in the parent scope and
>> will bind to it. If you tried to WRITE to $classes - it would make a
>> copy in the local scope and then throw that away as well but because you
>> are using methods $classes.Add(), you are modifing the contents of the
>> parent variable.
>>
>> This is a useful technique for keeping your environment tidy.
>>
> thx,
>
> I was thinking about returning it or use $global:$class (for Clearity),
>
> as I working on this from a WMI explorer I wrote in .NET a couple Years
> ago, I think it part by part and putting it together at the end.
> (that why for example the "Emty" managementScope is there (for later
> switch to another namesspace)
>
> Wat would be Nice, as Keith already mentioned,
> was using Match-string like this :
>
> $classes | match-String "win32_.*"
> or even
> $classes | fl | match-String "win32_.*"
>
> (In tryed overruling the ToString, as in a Tip of Bruce,
> but that did not work also.)
>
> Second thing I did not like was :
>
> $_.get_Qualifiers()["description"].value
>
> I would want to be able to do is this :
>
> $_.Qualifiers.description.value
>
> (as it was in .NET)
>
> gr /\/\o\/\/


.



Relevant Pages

  • Re: Name collisions in Monad
    ... We spent a lot of time working on the scoping rules in Monad. ... create a new scope when run, and variables _created_ in that new scope are ... script, call a function, or start a new instance of Windows Command ... $var is initially set to "init" in the function ...
    (microsoft.public.windows.server.scripting)
  • Re: Can com+ Hotfix Rollup Package 27 fix my memory problem with WSH. CreateObject/CreateScript?
    ... > these activities have effects that are felt outside of the scope of a script ... Of course in script, the object will be ... > Even the above definition makes no distinction between object variables ... a connection every time I read or write a key, ...
    (microsoft.public.scripting.wsh)
  • Re: One other related Q Re: basic Q: Only one way to make vars live outside of the scope of a functi
    ... >>> directly to the calling script, without having to use global variables ... to get multiple return items from a subprocedure. ... >>> better not to mix scope and use global variables for multiple return ... before terminating the object reference. ...
    (microsoft.public.scripting.vbscript)
  • Re: new to scripting.. the below script stops after first run..
    ... I have exports from my unix servers that aren't compatible with MS for a straight import so I've fiddled with the data, and now I want to just run the same script many times and pass it a bunch of parameters.. ... Echo Changing scope options ... Netsh dhcp server %server% scope %netid% add iprange %iprangestart% %iprangeend% BOTH> NUL ...
    (microsoft.public.windows.server.scripting)
  • Re: Calling scripts from functions?
    ... I am calling this script when a user presses a button ... I agree with what you say, the global scope generally is pretty bad, but I feel that Matlab handles its workspace differently then other languages. ... if you want to give things back to the user, use the OutputFcn callback. ...
    (comp.soft-sys.matlab)

Quantcast