首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell语法$using

PowerShell语法$using
EN

Stack Overflow用户
提问于 2020-10-31 19:32:48
回答 1查看 186关注 0票数 0

我找到一个类似如下的函数:

代码语言:javascript
复制
function New-ActiveDirectoryForest {
    param(
        [Parameter(Mandatory)]
        [string]$Name,

        [Parameter(Mandatory)]
        [pscredential]$Credential,

        [Parameter(Mandatory)]
        [string]$SafeModePassword,

        [Parameter(Mandatory)]
        [string]$ComputerName
    )

    Invoke-Command -ComputerName $ComputerName -Credential $Credential -ScriptBlock {
        Install-windowsfeature -Name AD-Domain-Services     
        $forestParams = @{
            DomainName                    = $using:Name
            InstallDns                    = $true
            Confirm                       = $false
            SafeModeAdministratorPassword = (ConvertTo-SecureString -AsPlainText -String $using:SafeModePassword -Force)
            WarningAction                 = 'Ignore'
        }
        $null = Install-ADDSForest @forestParams
    }
}

$using:Name$using:SafeModePassword代表什么?

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2020-11-01 12:27:30

正如其中一条评论中提到的,您可以在about_scopes Microsoft Docs站点上看到$using的用法。

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7#scope-modifiers

我认为这是文档中最中肯的解释。

对于在会话外执行的任何脚本或命令,您需要使用作用域修饰符来嵌入来自调用会话作用域的变量值,以便会话外代码可以访问它们。在以下上下文中支持Using scope修饰符:

远程执行的命令,通过使用ComputerName、HostName、SSHConnection或会话参数(远程会话)的Invoke命令启动后台作业,通过Start-Job (进程外会话)线程作业启动,通过Start-ThreadJob或ForEach-Object -Parallel (单独的线程会话)启动

因此,对于包含的脚本,它在被调用时从参数中收集SafeModePassword变量。它使用$Using作用域定义该变量,以便将该变量正确地传递给远程调用命令。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64621240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档