Understanding PowerShell Variable Scope in Scripts and Modules

preview_player
Показать описание
Not all PowerShell variables are created equally! In this video, learn about the different scopes available when working with PowerShell variables.

👇 Get the Code Here! 👇

📖 Read More About This 📖
PowerShell Variable Scope Guide: Using Scope in Scripts and Modules

Twilio PowerShell Module:

⏰ Chapters ⏰
00:00 Introduction
00:17 Scope Definitions
01:29 Comparing Global and Script Scope
05:20 Demonstrating Variable/Block Scoping
06:57 Sharing Variables Between Functions
09:40 Reviewing Scope in a Custom Module
12:01 Summary

Social Links
Рекомендации по теме
Комментарии
Автор

When I need to save some kind of state in a module, I’ll use a script scope variable as well. Typically I’ll explicitly declare that variable in my PSM1 so that there’s one place to go to know all script scope variables used in the module. And I’ll try to minimize the functions that touch the variable directly. Usually by having private get/set functions and ensuring that any other function that needs the variable will get it from the private function rather than accessing it directly.

Depending on the circumstances, environment vars can fill a similar role except they’re available in the global scope making them transparently available to the user and any other script/module running in that session.

psjoshooaj