Powershell范围处理v2 / v3的未记录更改? [英] Undocumented changes to Powershell Scope handling v2/v3?

查看:147
本文介绍了Powershell范围处理v2 / v3的未记录更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我一直在撰写一个powershell脚本,用于将Windows Server '08(使用Powershell 2.x)上的Sharpoint 2010实例的文件迁移到Windows Server '12上的Sharepoint 2013实例(与Powershell 3.x)。我有这个工作,但我注意到了如何处理范围的变化。



问题:我有以下代码可以在两个PSSession上运行( $ param 是参数值的散列表)

  Invoke-Command -session $ Session -argumentlist $ params -scriptblock`
{
Param($ in)
$ params = $在#存储参数在远程会话

#需要使用提升的权限运行以访问sharepoint farm
#drops cli stdout support(no echo to screen ...)
[Microsoft.SharePoint.SPSecurity] :: RunWithElevatedPrivileges(
{
#开始获取网站和web对象
$ site = get-spsite($ params [SiteURL])
})
}
我注意到,在分配给 $ site 的PS 2.x远程会话中,也分配给在 Invoke-Command 的范围内的相同变量,即任一范围获得通过或者共享sa我的范围分配给 $ site 的PS 3.x远程会话中但是不更改 Invoke-Command (true child scope)。



我的解决方案:我写了一个函数来计算在每个服务器上调用它的正确范围,然后使用返回值作为 Get-Variable Set-Variable 的 -Scope 选项。这解决了我的问题,并允许赋值和访问变量。

 函数GetCorrectScope 
{
#scoping在版本3中的powershell
#的版本2和3之间更改,我们需要在
#parent和本地范围之间传输变量。
if($ psversiontable.psversion.major -gt 2)
{
$ ParentScope = 1#上一级,powershell版本> = 3
} else
{
$ ParentScope = 0#当前级别,powershell版本< 3
}

$ ParentScope
}

问题:微软所记录的哪里有哪些地方? (我在TechNet上的 about_scope 找不到它,它说它适用于2.x和3.x,是我在其他问题中看到的标准参考。)



另外,是否有更好/正确的方式这个?

解决方案

WMF 3发行说明在更改WINDOWS POWERSHELL LANGUAGE一节中有记录。 p>


作为委托人执行的脚本块在自己的范围内运行




  Add-Type @
public class Invoker
{
public static void Invoke(System.Action< int> func)
{
func(1);
}
}
@
$ a = 0
[Invoker] :: Invoke({$ a = 1 $)
$ a

Windows PowerShell 2.0中返回1
Windows PowerShell 3.0中返回0


Background: I've been writing a powershell script to migrate files from a Sharpoint 2010 instance on Windows Server '08 (with Powershell 2.x) to a Sharepoint 2013 instance on Windows Server '12 (with Powershell 3.x). I have that working but I noticed a change in how scope is handled.

Issue: I have the following code that gets run on both PSSessions ($param is a hashtable of parameter values)

Invoke-Command -session $Session -argumentlist $params -scriptblock `
{
    Param ($in)
    $params = $in # store parameters in remote session

    # need to run with elevated privileges to access sharepoint farm
    # drops cli stdout support (no echo to screen...)
    [Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
    {
        # start getting the site and web objects
        $site = get-spsite($params["SiteURL"])
    })
}

I noticed that in the PS 2.x remote session that assigning to $site also assigned to the same variable in Invoke-Command's scope, i.e. either scope gets passed or they share the same scope. BUT in the PS 3.x remote session assigning to $site does not change the value in Invoke-Command (true child scope).

My Solution: I wrote a function to compute the correct scope on each server which it calls and then uses the return value as an input to Get-Variable and Set-Variable's -Scope option. This solved my problem and allows assignment and access of the variables.

Function GetCorrectScope
{
    # scoping changed between version 2 and 3 of powershell
    # in version 3 we need to transfer variables between the
    # parent and local scope.
    if ($psversiontable.psversion.major -gt 2)
    {
        $ParentScope = 1 # up one level, powershell version >= 3
    }else
    {
        $ParentScope = 0 # current level, powershell version < 3
    }

    $ParentScope
}

The Question: Where, if anywhere, is this documented by Microsoft? (I couldn't find it in about_scope on TechNet, which says it applies to both 2.x and 3.x and is the standard reference I've seen in other questions).

Also, is there a better/proper way to do this?

解决方案

It is documented in the WMF 3 Release Notes in the section "CHANGES TO THE WINDOWS POWERSHELL LANGUAGE".

Script blocks executed as delegates run in their own scope

Add-Type @"
public class Invoker
{
    public static void Invoke(System.Action<int> func)
    {
        func(1);
    }
}
"@
$a = 0
[Invoker]::Invoke({$a = 1})
$a

Returns 1 in Windows PowerShell 2.0 
Returns 0 in Windows PowerShell 3.0

这篇关于Powershell范围处理v2 / v3的未记录更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆