Powershell Rationale

New to PS, but experienced in software engineering, and used to procedural and OO languages, most recently Ada, which is based a lot on Pascal, are are C and other languages. Procedural programs often have a main loop which basically controls the overall program flow and provide user entry and exit to the program.

  1. Would it be reasonable to think of the PS pipe (ing) capability as something like steps or blocks of code in a procedural language’s main flow, functions or procedures (even if I don’t declare a function within PS, I can have steps that are function like. Though I read best practice seem to be to group code into logical functions, like most programming. It seems pipe (ing) will be necessary for many, many blocks of code or function procedure like steps, whether they are programmed as formal functions or procedures.

2)What is a good source for understanding the scope, visibility, and lifetime of PS’s objects, and user defined variables (which are objects too?)?

  1. Can I pipe, not to an out-file, but to a function or variable within an overall script, providing my functions and variables are declared before us. And, need I only declare, specify a functions before it is used, but not necessarily at the beginning of a script?

 

 

 

 

Hi Michael,

 

I cant speak to other languages (as i only really know powershell), but i hope i can answer your other questions.

 

2. What is a good source for understanding the scope, visibility, and lifetime of PS's objects, and user defined variables (which are objects too?)?
Microsoft's own Technet has always been a goos place to read about some of Powershell's more technical aspects.
3. Can I pipe, not to an out-file, but to a function or variable within an overall script, providing my functions and variables are declared before us. And, need I only declare, specify a functions before it is used, but not necessarily at the beginning of a script?
Absolutley! a Function only needs to be declared before it is called, not specifically at the top of the script (although i think it may just be a best practice.)

For the most part variables dont need to be excplicitly declared before you give them data, for example

$services = get-service

will not only initialize the variable, but will also use it to store the output of the Get-Service cmdlet

For functions that are part of larger modules, as long as the variable is contained in a location specified in the $ENV:PSMoldulePath system variable you dont even need to specifically import that module to use that Function or CMdlet .

you can even pipe the results of one cmdlet directly into another, for example

Get-Service -Name 'WinRM' | Restart-Service

I hope that answers your question, and please let me know if there is anything i can clarify.