Categories
Device

How scopes affect powershell scripts

PowerShell scopes protect access to variables, aliases, functions, and PowerShell drives (PSDrives) by limiting where they can be read and changed.

The basic rules of scope:

    An item you include in a scope is visible in the scope in which it was created and in any child scope, unless you explicitly make it private. You can place variables, aliases, functions, or PowerShell drives in one or more scopes.

An item that you created within a scope can be changed only in the scope in which it was created, unless you explicitly specify a different scope.

  • If you create an item in a scope, and the item shares its name with an item in a different scope, the original item might be hidden under the new item. But, it is not overridden or changed.
  • PowerShell Scopes

    Global The scope that is in effect when PowerShell starts. Variables and functions that are present when PowerShell starts have been created in the global scope. This includes automatic variables and preference variables.
    This also includes variables, aliases, and functions that are in your PowerShell profile.
    Local The current scope. The local scope can be the global scope or any other scope.
    Script The scope that is created while a script file runs. Only the commands in the script run in the script scope. To the commands in a script, the script scope is the local scope.
    Private Items in private scope cannot be seen outside of the current scope. You can use private scope to create a private version of an item with the same name in another scope.
    Numbered Scopes A numeric scope describes the relative position of one scope to another. Scope 0 represents the current, or local, scope. Scope 1 indicates the immediate parent scope. Scope 2 indicates the parent of the parent scope, and so on. Numbered scopes are useful if you have created many recursive scopes. Numbered scopes are relative unlike the named scopes above which are absolute.

    A new scope is automatically created by running a script or function, creating a session, or by starting a new instance of PowerShell.
    The result is a parent scope (the original scope) and a child scope. Unless you explicitly make the items private, the items in the parent scope are available to the child scope. However, items that you create and change in the child scope do not affect the parent scope, unless you explicitly specify the scope when you create the items.

    The default scope for scripts is the script scope.
    The default scope for functions and aliases is the local scope, even if they are defined in a script.

    Inheritance

    A child scope does not inherit the variables, aliases, and functions from the parent scope.
    Unless an item is private, the child scope can view the items in the parent scope. And, it can change the items by explicitly specifying the parent scope, but the items are not part of the child scope.

    However, a child scope is created with a set of items. Typically, it includes all the aliases that have the AllScope option, plus all the variables that have the AllScope option, plus some variables that can be used to customize the scope, such as MaximumFunctionCount.

    To find the items in a particular scope, use the Scope parameter of Get-Variable or Get-Alias.
    For example, to get all the variables in the local scope:
    PS C:> get-variable -scope local

    To get all the variables in the global scope:
    PS C:> get-variable -scope global

    Create or access items in any scope

    To specify the scope of a new variable, alias, or function, prefix the name with a scope modifier:

    $global:DemoVar = “hello”
    $script:DemoVar = “world”
    $private:DemoVar = “world”
    function global:DemoFunc

    Because the local scope is the default, these commands both do the same thing:
    $local:DemoVar = “hello”
    $DemoVar = “hello”

    To read a variable from a different scope, again prefix with a scope modifier:

    AllScope

    Variables and aliases have an -Option property that can take a value of AllScope . Items that have the AllScope property become part of any child scopes that you create.

    An item that has the AllScope property is visible in the child scope, and it is part of that scope. Changes to the item in any scope affect all the scopes in which the variable is defined.

    For more about scopes see help about_scopes

    “Health is a crown that only the sick can see”

    Related PowerShell Cmdlets:

    Dot Sourcing – persist variables and functions after a script (or scriptblock) ends.
    & Call Operator – execute a command, script or function.
    $myinvocation

    General discussion

    While working with PowerShell and writing various psm1 module files, I’ve noticed that for whatever reason, Advanced Functions in a Script Module (psm1 file) do not inherit the Preference variables (or any other variable) from the scope of the code that calls them (unless that happens to be the Global scope.) As far as the functions in the Script Module are concerned, variables exist either in the function, in the module, or in the Global scope, and that’s it. If the caller sets, for example, $ErrorActionPreference to ‘Stop’ in the Script scope and then calls your function, your function will still be acting with the value of the global $ErrorActionPreference, which is most likely still set to the default value of ‘Continue’.

    You can see this behavior by creating a test PS1 and PSM1 file as follows:

    I researched this, and at some point discovered that module functions can get at the variables in the caller’s scope by using properties and methods of the $PSCmdlet object, for example:

    However, it gets rather annoying to have to define code like that in every single function that needs to be exported from a script module (assuming you want the module’s functions to behave according to preference variables set in the caller’s scope.) I did some more digging earlier today, and discovered a way to put this code into a function, which can be called like this:

    Overview

    PowerShell is a useful tool to use for scripting server role configurations. The automation inherit in scripting means we can use it to quickly manage multiple servers in minutes, and we can do it remotely from our desktops. This is great when you have a large environment

    This tutorial will guide you through some of the administrative tasks that can be done using PowerShell.

    Creating a Scope

    Cmdlet and Syntax

    The cmdlet used to create new DHCP scopes in Windows Server 2012 R2 is Add-DhcpServerv4Scope. It has the following syntax.

    Examples of Creating a Scope

    • To create a scope named “Toronto Corporate” with an IP range of 172.30.0.1-254 and a lease duration of 8 days, we would execute the following in PowerShell.
    • To do the same scope shown above, but from a remote computer targeting a DHCP server named dhcp01, we would execute the following.

    Related Posts:

    Republish This Article

    We believe in the free flow of information. We use a Creative Commons license, so you can republish our articles for free, online or in print.

    RECOMMENDED BOOKS

    Continuous Delivery should be considered the bible for anyone in Ops, Dev, or DevOps. The book provides key strategies for improving system reliability, configuration management, and ensuring web applications can be delivered to production frequently, and easily. A recommended buy for anyone in IT.

    How scopes affect powershell scripts

    How scopes affect powershell scripts

    • Clean up user profiles with PowerShell – Mon, Jun 9 2014
    • Track user logons with a PowerShell script – Fri, Jun 6 2014
    • Configuring logon PowerShell scripts with Group Policy – Tue, Jun 3 2014

    In a previous article I explained about PowerShell’s execution policy and demonstrated how to set it from an elevated PowerShell session.

    The default policy is Restricted so if you only need to configure a policy on a handful of machines you could run the command interactively. You could even use a remoting command.

    But the smarter way is to use a Group Policy object. The primary benefit is that policies applied by a GPO can’t be overwritten, even by an administrator. To configure such a policy open the Group Policy Management Editor and edit or create a new GPO. Mine is called PowerShell Configuration.

    How scopes affect powershell scripts

    Group Policy Management Editor

    To configure, navigate under Computer Configuration to PoliciesAdministrative TemplatesWindows ComponentsWindows PowerShell. You should see a setting called Turn on Script Execution like in figure 2.

    How scopes affect powershell scripts

    Turn on Script Execution

    Double-click the setting. You will want to enable it and select an option from the drop down.

    How scopes affect powershell scripts

    Enable PowerShell execution policy

    The policy descriptions correspond to AllSigned, RemoteSigned and Unrestricted. I’m going to pick the policy to allow local and remote signed scripts. Click OK to set it. The dialog suggest you need at least Windows 7 but I am pretty sure that even with PowerShell 2 running on older systems you can use this policy. I unfortunately no longer have any such legacy systems to verify that. But this setting will apply to PowerShell 2.0 and later.

    I can verify my setting in the Group Policy Editor.

    How scopes affect powershell scripts

    Group Policy Editor

    Of course the benefit is that you can apply different settings to different organizational units so servers, user desktops and admin desktops can all have different settings that will be enforced. Remember, this policy is per machine so it will apply to your administrator desktops as well.

    Now, when the policy is in place, users (and administrators) can’t override your script execution policy.

    How scopes affect powershell scripts

    PowerShell execution policy overridden

    In theory, this should also protect you from any future PowerShell-centric malware that might try alter your script execution policy.

    As I mentioned before, this doesn’t prevent someone from copying and pasting the contents of a script into a PowerShell session, but at least they can’t simply run the script.

    This policy also doesn’t have any effect on cmdlets, like Invoke-Command, that have a parameter to specify a script path. The machine running Invoke-Command should have a policy to allow script execution. But any remote machines can have restricted execution policies. When you specify a script from your machine, the script itself doesn’t run remotely, only the contents. Think of this as the equivalent of copying and pasting the script commands into a PowerShell session. The end result is that servers can be locked down yet you can still take advantage of PowerShell scripts.

    One thing to be aware of is that if you are using a Group Policy to define a PowerShell logon, logoff or computer script, that script will disregard any execution policy set locally or through a GPO. These scripts execute with a Bypass execution policy. The assumption is that you have created the script, know what it does and have taken precautions to secure it.

    I encourage you to test these Group Policy settings in a non-production environment so that you can fully understand the implications and how it might affect your PowerShell management practices.

    #Cloud #Devops #Automation #SRE

    This blog post covers some of most untalked rules about variable scope in PowerShell. It is untalked because most users never come across it. But it is useful to those who are trying to write a module or new cmdlets in the PowerShell or writing some product support in PowerShell.

    It is to be noted that the rules don’t just applies to the variables, but they are also applicable to the aliases, functions, and Windows PowerShell drives (PSDrives). Essentially it controls where they can be read and changed.

    Types of Scope in PowerShell

    Global: The scope that is in effect when Windows PowerShell starts. Variables and functions that are present when Windows PowerShell starts have been created in the global scope. This includes automatic variables and preference variables. This also includes the variables, aliases, and functions that are in your Windows PowerShell profiles.

    Local: The current scope. The local scope can be the global scope or any other scope.

    Script: The scope that is created while a script file runs. Only the commands in the script run in the script scope. To the commands in a script, the script scope is the local scope.

    Private: Items in private scope cannot be seen outside of the current scope. You can use private scope to create a private version of an item with the same name in another scope.

    Numbered Scopes: You can refer to scopes by name or by a number that describes the relative position of one scope to another. Scope 0 represents the current, or local, scope. Scope 1 indicates the immediate parent scope. Scope 2 indicates the parent of the parent scope, and so on. Numbered scopes are useful if you have created many recursive scopes.

    Basic Rules of Scoping

    • An item you include in a scope is visible in the scope in which it was created and in any child scope, unless you explicitly make it private. You can place variables, aliases, functions, or Windows PowerShell drives in one or more scopes.
    • An item that you created within a scope can be changed only in the scope in which it was created, unless you explicitly specify a different scope.
    • If you create an item in a scope, and the item shares its name with an item in a different scope, the original item might be hidden under the new item. But, it is not overridden or changed.

    Let’s try to comprehend that. It will take some time to sink that all in. So everything starts with a globle scope in PowerShell. It is created when you run an instance of PowerShell session. It may also be called as Parent Scope. Now when you run a function or variable directly inside the session, you create child scopes. Each function/variable has its own child scope. If you create variables inside the function, you create further child scopes, for whom the function is a parent scope and the session is grand-parent scope. You can create as many child scopes inside each child scope as you would like to. Do note that the items in a parent scope are always available to be used by a child scope unless they are explicitly restricted. However, items in the parent scope are not inherited by the child scope. They are their to be seen/view and to be changed/modified if required but not more than that. They never become part of the child scope.

    Let’s break it down further. We can start a new PowerShell ISE session and see the output of following 2 commands:

    You can see the output of these 2 and it is exactly same. Why? It is because the variables in the parent scope are always available to the child scope. Let’s create a local scope variable by using following command:

    Now compare the output of below 2 commands again:

    You can see the difference in the output of the 2nd command as below:

    How scopes affect powershell scriptsIdentifying variables in the local scope

    If we want to declare a global variable, we would need to declare it like this:

    And again see the output from Get-Variable -Scope global:

    How scopes affect powershell scriptsIdentifying variables in the global scope

    You can explicitly define the scope of the variables using below syntax:

    where permissible values to replace [] are Global, Local, Private, and Script. So:

    You can also use a scope modifier in functions. The following function definition creates a function in the global scope:

    Consider following PowerShell code:

    Above output should be clear now. We can do modify the variables in the parent scope but for that we need to explicitly use their respective scope modifier:

    Same goes for other scopes. Consider below code:

    If you have guessed, there is no output generated for $x as it does not exist in the parent scope.

    If you have guessed correctly, the output is Y as shown. Let’s take another example. Declare a local variable $testvar in the current session:

    Now, create a script called scope.ps1 and use below commands in the script:

    $testvar = 20
    Write-Output “The value of `$testvar is $testvar.”

    Now, execute the script in the same session. You should be able to see output like this:

    The value of $testvar is 20.

    Now, again get the value of $testvar and you should be able to see the value is unchanged to 10. Again, this is because the scope of variables inside a script are always limited to the script. Same goes for functions, etc.

    A sad thing about PowerShell is that function and scriptblocks are dynamically scoped.

    But there is another thing that surprised me is that variables behave as a copy-on-write within an inner scope.

    Which makes dynamic scoping a little bit less painful. But how do I avoid the copy-on-write?

    How scopes affect powershell scripts

    How scopes affect powershell scripts

    4 Answers 4

    You can use scope modifiers or the *-Variable cmdlets.

    The scope modifiers are:

    • global used to access/modify at the outermost scope (eg. the interactive shell)
    • script used on access/modify at the scope of the running script ( .ps1 file). If not running a script then operates as global .

    (For the -Scope parameter of the *-Variable cmdlets see the help.)

    Eg. in your second example, to directly modify the global $array :

    For more details see the help topic about_scopes.

    The PowerShell scopes article (about_Scopes) is nice, but too verbose, so this is quotation from my article:

    In general, PowerShell scopes are like .NET scopes. They are:

    • Global is public
    • Script is internal
    • Private is private
    • Local is current stack level
    • Numbered scopes are from 0..N where each step is up to stack level (and 0 is Local)

    Here is simple example, which describes usage and effects of scopes:

    As you can see, you can use $Global:test like syntax only with named scopes, $0:test will be always $null.

    Not just varibles. When this says “item” it means variables, functions, aliases, and psdrives. All of those have scope.

    The copy on write issue you’re seeing is because of the way Powershell handles arrays. Adding to that array actually destroys the original array and creates a new one. Since it was created in that scope, it is destroyed when the function or script block exits and the scope is disposed of.

    You can explicitly scope varibles when you update them, or you can use [ref] objects to do your updates, or write your script so that you’re updating a property of an object or a hash table key of an object or hash table in a parent scope. This does not create a new object in the local scope, it modifies the object in the parent scope.

    How scopes affect powershell scripts

    While other posts give lots of useful information they seem only to save you from RTFM.
    The answer not mentioned is the one I find most useful!

    This modifies the value of $var no matter what scope it happens to be in. You need not know its scope; only that it does in fact already exist. To use the OP’s example:

    Explanation:
    ([ref]$var) gets you a pointer to the variable. Since this is a read operation it resolves to the most recent scope that actually did create that name. It also explains the error if the variable doesn’t exist because [ref] can’t create anything, it can only return a reference to something that already exists.

    .value then takes you to the property holding the variable’s definition; which you can then set.

    You may be tempted to do something like this because it sometimes looks like it works.

    DON’T.
    The instances where it looks like it works is an illusion because PowerShell is doing something that it only does under some very narrow circumstances such as on the command line. You can’t count on it. In fact it doesn’t work in the OP example.

    I’ve got the script below, from internet:

    It prints 2. No problem. If I delete “private”, like below:

    Still it prints “2”. Seems no difference. Could you provide an quick sample of how “private” scope affects the result?

    3 Answers 3

    Private scope can be useful when writing a function that invokes a user-supplied callback. Consider this simple example:

    Then, if someone calls it like this:

    they’ll expect to see only the First2 Last2 row, but actually this will print all three rows. This is because of a collision on the $FirstName variable. To prevent such collisions, you can declare variables in Where-Name as private:

    Now $FirstName in Where-Name does not hide $FirstName in the outer scope when referenced from the $Condition script block.

    Note:
    * This answer explains why the OP’s code behaves the way it does (and that it behaves as designed); additionally, it provides some general information about variable scopes in PowerShell.
    * For an important real-world use of scope private , see PetSerAl’s helpful answer.

    Your first snippet prints:

    Your second snippet prints:

    In the first snippet, using scope private causes the parent (script) scope’s a variable to be hidden from the child (function) scope, as designed, so the first output line shows that $a has no value
    (an undefined variable has value $null , which evaluates to the empty string in a string context).

    In the second snippet, by contrast, without the private scope modifier, variable a from the parent scope is visible to the child scope.

    In PowerShell, functions execute in child scopes by default.

    Therefore, in both snippets above, assigning to variable a inside the function implicitly creates a local a variable there, whose scope is limited to the enclosing function.

    • Assigning to $a in the function creates a function-local variable named $a , which then shadows (hides) the script-level $a variable (if it wasn’t already hidden by having been declared as $private:a ) – though note that local in PowerShell means that child scopes do see its value; see next section.
    • On leaving the function, $a again has its original, script-level value.

    General information about variable scopes in PowerShell

    Unless a variable is explicitly hidden with scope private , descendant scopes can see that variable and read its value using the variable name without a scope qualifier (e.g., $a ) or the need for Get-Variable -Scope .

    • Note that while descendant scopes do not see the values of variables created with $private: by default, they can still refer to them with relative cross-scope access, using Get-Variable -Scope or Set-Variable -Scope .
      Non-relative scope modifiers ( $script , $global , $local ) generally do not work – except if the reference happens in the same scope in which the private variable was created and the scope modifier happens to effectively target that same scope, which is always true for $local:privateVarName , for instance.

    Assigning to an unqualified variable, however, implicitly creates a new variable in the current ( local ) scope, which can shadow a variable of the same name in an ancestral scope.

    • That is, $a = 2 is implicitly the same as $local:a = 2 .

    To explicitly get / modify a variable in an ancestral scope, use Get-Variable / Set-Variable -Scope , where represents the scope level, with 0 representing the current scope, 1 the parent scope, and so on.
    Note that Get-Variable returns a [System.Management.Automation.PSVariable] instance by default, so in order to get only the value, access its .Value property, or use the -ValueOnly switch, which only returns the value to begin with.

    In functions and trap handlers, before creating a local copy of a variable, you can alternatively modify a variable in the most immediate ancestral scope where it is defined as follows:

    • ([ref] $var).Value = .
    • (If and once a local variable by the same name is created, the above will modify only the local variable, however.)

    Variables in the script scope and the global scope can also be accessed – and modified – by using the $script: and $global: scope modifiers; e.g., $script:a and $global:a .
    Note that $script: refers to the (immediately) enclosing script file’s top-level scope.

    Modules each have their own scope domain linked only to the global scope. That is, modules see outside variables only from the global scope, not from a caller in any other scope, such as from a script; this can cause unexpected behavior with preference variables, as discussed in this GitHub issue.

    Declaring a variable with Set-Variable -Option AllScope allows it to be read and modified in any descendant scope without needing to qualify the name; to put it differently: only a single variable by that name exists then, which any scope can directly read and write using the unqualified variable name.

    Without a separate -Scope parameter, -Option AllScope is applied to the variable in the current scope (e.g., the script scope at the script’s top level, a function’s local scope inside a function). Thus, to safely create a script-global variable that you can access unqualified for reading and writing, use Set-Variable -Scope Script -Option AllScope .

    -Scope Global is distinct from -Option AllScope : while -Scope Global creates a globally accessible variable, reading it may, and modifying it does, require the $global: scope modifier. Also note that a global variable is session-global, so it persists even after the script that defined it has terminated.

    By combining -Scope Global with -Option AllScope you effectively create a session-global singleton variable that can be read and written from any scope without qualifier; as stated, however, such a variable lives on even after your script exits.

    Office365, Azure and SharePoint

    Using MFA enabled accounts in PowerShell scripts

    The use of multi-factor authentication (MFA) is growing by the day. More and more customers are enabling MFA for administrator accounts to protect their cloud environment a little bit more. But that also might affect your PowerShell scripts. In this post I want to point out how to deal with MFA enabled accounts in your PowerShell script.

    Connecting to services

    Before you can use cmdlets from modules like Azure PowerShell, Azure Active Directory and SharePoint Online you need to connect to these services first. Each of these services has its own “login” cmdlet. For example:

    These cmdlets also have a parameter Credential and you can pass a PSCredential object. This is handy when you need to connect to multple services with the same credentials. One way to achieve this is to ask the username and password from the script.

    How scopes affect powershell scripts

    And of course, you can also use

    to get the Windows PowerShell login popup:

    How scopes affect powershell scripts

    Windows Credential Manager

    If you don’t way to ask for credentials in the script, you can also use the Windows Credential Manager. You need to install the module CredentialManager for this.

    ( I am not going to describe how to set this all up. Plenty of articles available, such as https://www.petri.com/managing-usernames-passwords-powershell-sharepoint-online )

    Once set up, your script can look like this:

    What about MFA enabled accounts?

    In spite of all the advanced scripting above, it simply does not work with MFA enabled accounts. You will see errors like this

    The only solution available today (that I know of) is to use the cmdlets to connect to your service without the parameter Credential. This will show you the Sign in to your account popup of that service and this login window has the support for MFA. For example:

    How scopes affect powershell scripts

    How scopes affect powershell scripts

    How scopes affect powershell scripts

    Does this mean you have to throw all your scripts into the recycle bin? No, you are going to accept and embrace this.

    It is what it is.

    You can add a switch parameter to your own script to indicate MFA should be used. With that you can have a if-then-else construction.

    Summary

    As more and more customers are enabling multi-factor authentication (MFA) for administrator accounts, your PowerShell scripts may not work anymore. In case of MFA enabled accounts, you have to use the cmdlets to connect to your service without the parameter Credential. The Sign-In window has support for MFA.

    • ← Create Azure AD App Registration with PowerShell–Part 2
    • Configuring Azure AD App for AppOnly permissions with PowerShell →

    7 thoughts on “ Using MFA enabled accounts in PowerShell scripts ”

    Thanks for the tips! I was running into this issue while running to run power shell scripts against an Office 365 tenant with MFA enabled. Leaving out the credential parameter and letting it prompt for authentication worked great.

    I have a scheduled task (at night) executes a PS script that copies 400+ databases from prod to dev and creates a number of dev-specific accounts(AAD and Groups) and their permissions after the dev databases have copied. The copy step can be done with a non-AD account but the application of the dev-specific accounts must be done using an AAD account. MFA looks to break this template by requiring me to be there to provide and account and password information interactively. Am I missing something? Thanks for helping me see clearly.

    Do I understand it correctly, that your daily scheduled script is running under the credentials (yours?) that has MFA enabled?
    Looks to me that this should be better some service account without MFA enabled and having sufficient permissions (read: not necessarily the Global Admin role) to run this process.

    Thanks for the reply and confirmation. The service account without MFA is the only approach that came to mind to address my situation.

    Thanks for the information. However, I am not too sure about this statement

    “Does this mean you have to throw all your scripts into the recycle bin? No, you are going to accept and embrace this.

    It is what it is.”

    Not to be blunt or anything, but embrace it and then what? We have Powershell scripts that run nightly to sync content to cloud. Basically, all of them have now become unusable because of MFA. Any insight into how those scripts can be enabled for MFA without user intervention?

    Hi Michael, afaik you should not use an account with MFA for unattended processes like you describe. So, create and use a dedicated service account for your purpose and do not use MFA.

    Leave a Reply Cancel reply

    I work at

    Portiva Make

    How scopes affect powershell scripts

    The best solution for creating and provisioning SharePoint Online sites, Office 365 Groups and Teams using templates!

    Portiva GuestHub

    How scopes affect powershell scripts

    Manage all your guest accounts in your tenant.

    Mavention Screen

    How scopes affect powershell scripts

    Flexible narrow casting solution based on SharePoint

    バッチスクリプトでは、環境変数を変更すると、デフォルトで現在のセッションにグローバルな影響があります。 PowerShellでは、スコープがスクリプトの変更を分離するために使用されるため、正反対のことが言えます。 ここでは、スコープがPowerShellスクリプトにどのように影響するか、およびその中やその周辺で作業する方法を探ります。

    スコープとは

    PowerShellでは、「スコープ」とはスクリプトまたはコマンドシェルが動作している現在の環境を指します。 スコープは、スクリプトや関数によって意図せずに変更されることから環境内の特定のオブジェクトを保護するために使用されます。 特に、以下の事項は、別のスコープから実行されたコマンドによる変更から保護されています。

    • 変数
    • エイリアス
    • 関数
    • PowerShellドライブ(PSDrive)

    スクリプトまたは関数を実行したとき、またはPowerShellの新しいセッションまたはインスタンスを作成したときに、新しいスコープが作成されます。 スクリプトと関数を実行することによって作成されたスコープは、それらが作成されたスコープと「親/子」関係を持ちます。 特に特別な意味を持ち、名前でアクセスできるスコープがいくつかあります。

    • グローバルスコープは、PowerShellの起動時に作成されるスコープです。 これには、PowerShellに組み込まれている変数、エイリアス、関数、およびPSDriveだけでなく、PowerShellプロファイルによって作成されたものも含まれます。
    • ローカルスコープは、現在のスコープが何であれを指します。 PowerShellを起動すると、グローバルスコープを参照し、スクリプト内ではスクリプトスコープなどになります。
    • スクリプトスコープは、スクリプトが実行されたときに作成されます。 この範囲内で動作する唯一のコマンドは、スクリプト内にあるものです。
    • プライベートスコープは現在のスコープ内で定義することができ、他のスコープ内のコマンドが他の方法ではアクセス権を持っている可能性があるアイテムを読み取りまたは変更できないようにします。

    スコープは、特定のコマンドでは番号で参照することもできます。この場合、現在のスコープはゼロとして参照され、その先祖は増加する整数によって参照されます。 たとえば、Globalスコープから実行されたスクリプト内では、Scriptスコープは0になり、Globalスコープは1になります。関数など、Scriptスコープ内でさらにネストされたスコープは、Globalスコープを2として参照します。負の数は子スコープを参照するためには機能しません – その理由は間もなく明らかになります。

    スコープがコマンドに与える影響

    前述のように、あるスコープ内で実行されたコマンドは、特に指示がない限り、別のスコープ内のものには影響しません。 たとえば、$ MyVarがGlobalスコープに存在し、スクリプトが$ MyVarを別の値に設定するコマンドを実行した場合、$ MyVarのグローバルバージョンは変更されずにそのまま残り、$ MyVarのコピーは新しいスコープでScriptスコープに配置されます。値。 $ MyVarが存在しない場合、スクリプトはデフォルトでScriptスコープ内でそれを作成します – Globalスコープ内ではありません。 スコープ間の実際の親子関係について学習するときに、これを覚えておくことが重要です。

    PowerShellのスコープの親子関係は一方向です。 コマンドは、現在のスコープ、その親、およびそれより上の任意のスコープを調べ、必要に応じて変更することができます。 しかし、彼らは現在の範囲のどの子にも物事を見たり修正したりすることはできません。 これは主に、いったん親スコープに移動した後、その子スコープがその目的を果たしたためにすでに破棄されているためです。 たとえば、スクリプトが終了した後に、GlobalスコープからScriptスコープ内の変数を表示または変更する必要があるのはなぜですか。 スクリプトや関数の変更を完了後も持続させる必要がある場合は多くありますが、スクリプトの実行前または実行後にスクリプトや関数のスコープ内のオブジェクトに変更を加える必要がある場合はそれほど多くありません。 (通常、このようなことはスクリプトまたは関数自体の一部として処理されます。)

    もちろん、例外のないルールは何ですか? 上記の例外の1つは、Privateスコープです。 Privateスコープ内のオブジェクトは、それらが作成されたスコープ内で実行されるコマンドにのみアクセス可能です。 もう1つの重要な例外は、AllScopeプロパティを持つアイテムです。 これらは特別な変数とエイリアスで、スコープの変更はすべてのスコープに影響します。 次のコマンドは、どの変数とエイリアスにAllScopeプロパティがあるかを示します。

    活動中のスコープ

    実際にスコープを見てみるために、コマンドラインから変数$ MyVarが「私はグローバル変数です」という文字列に設定されているPowerShellセッションから始めましょう。 その後、Scope-Demo.ps1というファイルから次のスクリプトが実行されます。

    PowerShellスクリプトがバッチスクリプトと同じように機能する場合、$ MyVar(またはバッチ構文では%MyVar%)の値が、 ‘私はグローバル変数です!’から ‘スクリプトで設定された!’に変わると予想されます。そして最後に「関数によって設定されました」に。 明示的に再度変更されるか、セッションが終了するまで待機します。 しかし、ここで実際に何が起こるのかを見てみましょう。特に、FunctionScope関数が作業を完了し、Scriptから変数をチェックした後、さらにGlobalスコープをチェックした後です。

    How scopes affect powershell scripts

    FunctionScope関数が完成するまで、最後に変更されたのと同じスコープ内から変数をチェックしていたため、スクリプトを移動するにつれて変数が変化したように見えます。 ただし、FunctionScopeが実行された後は、$ MyVarが関数の影響を受けないままになっているScriptスコープに戻りました。 その後、スクリプトが終了したときに、私たちはグローバルスコープに戻りました。

    ローカルスコープの外側に到達する

    そのため、スクリプトや機能以外にも誤って環境に変更を加えないようにするのに役立ちますが、実際にそのような変更を加えたい場合はどうしますか。 Localスコープを超えてオブジェクトを作成および変更するための、特殊でかなり単純な構文があります。 スコープ名を変数名の先頭に置き、スコープと変数名の間にコロンを入れるだけです。 このような:

    How scopes affect powershell scripts

    ここでは、FunctionScopeがScriptスコープ内の変数を変更し、変更が完了した後もその変更を保持できることがわかります。 また、Globalスコープ内の変数への変更は、スクリプトが終了した後も持続しました。 これは、同じコードを使用して、スクリプト内またはGlobalスコープ内で変数を繰り返し変更する必要がある場合に特に役立ちます。変数を変更する場所と方法を変更するために書かれた関数またはスクリプトを定義するだけです。それらの変更が必要なときはいつでもそれを要求してください。

    この記事に収まる以上に、スコープを使ってできることはまだたくさんあります。 スコープは単なる変数以上のものに影響を与えますが、PrivateスコープとAllScope変数について学ぶべきことはまだたくさんあります。 より有用な情報を得るために、PowerShell内から次のコマンドを実行できます。

    Although PowerShell is popular, for malicious intruders it represents a very attractive attack vector into your system. The obvious way of preventing this type of penetration is to detect when a script is altered. Not only must any script that is used for system or data administration be properly secured, but also any script that is used to maintain a PowerShell profile.

    The series so far:

    1. Automating Day-to-Day PowerShell Admin Tasks – Part 1: Jobs and Workflow
    2. PowerShell Day-to-Day Admin Tasks – Part 2: WMI, CIM and PSWA
    3. PowerShell Day-to-Day Admin Tasks – Part 3: Monitoring Performance
    4. PowerShell Day-to-Day Admin Tasks – Part 4: Securing Scripts
    5. PowerShell Day-to-Day Admin Tasks – Part 5: Events and Monitoring
    6. PowerShell Day-to-Day Admin Tasks – Part 6: Real Time IT Dashboard

    Scripting shells can be one of the most effective attack points of an operating system. They offer an advantage to an attacker by providing a layer of abstraction that anti-virus applications have no idea how to interpret. PowerShell is a scripting shell, but it uses ‘defense in depth’ meaning that it takes a number of approaches to preventing malicious use of PowerShell or your scripts. One of the most effective measures is to use Digital Signatures to determine whether a script is to be ‘trusted’.

    PowerShell, by default, has an execution policy that prevents scripts being run at all and only allows interactive use of PowerShell. Many admins switch to an unrestricted policy, which is leaves the possibility of malicious attack wide-open, thereby risking harm for them and the firm for which they work. In this article, I will argue that using signed scripts a via digital Signatures (allsigned) is the best execution policy.

    PowerShell is unique amongst shells in that it executes scripts on start-up. There are user or system scripts that are run unnoticed when you start up PowerShell in order to provide the PowerShell ‘environment’, and to maintain profiles for users. An attacker needs only add code to a profile is then executed using the ID of the user, along with all its permissions. It could then execute an external binary, or more covertly, load shellcode or a malicious DLL. When the user executes the script, he inadvertently executes the added code. The attacker has avoided having to load an external malicious script and the modification could easily remain unnoticed. Obviously, the PowerShell profile scripts are stored within a windows users’ directory so they may not seem to be easy for an intruder to alter, but any malware is likely to run under the users ID anyway so this, by itself, offers little protection. Digital signatures provide a measure of security wherever the PowerShell scripts are stored, and allow admins to safely share common libraries of scripts.

    In case you are unfamiliar with profiles, I’ll show how you can obtain PowerShell’s environment when personalising your profile. Then I shall build on this to show how PowerShell handles this security aspect. Most important, I’d like to demonstrate that ease-of-use does not mean lack of security.

    The customisation of your work environment

    The PowerShell is capable of saving time with your tasks and assisting with your work. A profile can, among other things, help with the administrator’s task. Anyone, for example, who does administrative tasks will find that many actions are performed several times per day, or each time they connect to their machine. Let’s take a simple example: you launch a remote connection on a server to update a component. How many times will you ask yourself the following question: ‘Am I really connected to server “SRV01”?’ To reassure ourselves, we open a PowerShell console and type ‘$env:COMPUTERNAME’.It is certainly wise to double-check but, in doing so, you have just lost a few seconds. You’d probably have other information you’d want to check, such as the User_AD you are logged-in as, and the up-time of the machine you’re logged into. There are, of course, tools available to provide this sort of information such as BGInfo (download: https://technet.microsoft.com/en-us/sysinternals/bginfo.aspx). Sadly it is not always possible to execute this type of tool on critical servers. It is here that PowerShell can help you if we create a profile.

    To determine if you already possess a PowerShell profile, type in the following on your machine: