首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell隐式远程处理不使用$PSDefaultParameterValues

PowerShell隐式远程处理不使用$PSDefaultParameterValues
EN

Stack Overflow用户
提问于 2013-01-05 02:07:58
回答 1查看 909关注 0票数 0

我正在尝试使用$PSDefaultParameterValues来简化我的生活。我遇到了这样一个问题:当调用通过隐式远程处理加载的cmdlet时,我在$PSDefaultParameterValues中设置的任何内容都会被忽略。

下面是我目前遇到的一个简化示例:

代码语言:javascript
复制
$domainController = 'dc1.ptloma.edu' 
Import-Module ActiveDirectory

foreach($command in (Get-Command -Module ActiveDirectory -ParameterName Server))
{
    $PSDefaultParameterValues["$($command.Name):Server"] = $domainController
}        

$exchangeSessionParameters = @{
    Name = "ExchangeInterop"
    ConfigurationName = "Microsoft.Exchange"
    Authentication = "Kerberos"
    Credential = $ExchangeCredential
    ConnectionUri = "http://server.domain.com/PowerShell/"
}

$exchangePsSession = New-PSSession @exchangeSessionParameters
$remoteModule = Import-Session -Session $exchangePsSession

foreach($command in (Get-Command -Module $remoteModule -ParameterName DomainController))
{
    $PSDefaultParameterValues["$($command.Name):DomainController"] = $domainController
}

try
{
    New-AdGroup -Path "OU=Automated,OU=Groups,DC=domain,DC=com" -Name "GroupA"
    Enable-DistributionGroup -Identity "CN=GroupA,OU=Automated,OU=Groups,DC=domain,DC=com" -Alias "GroupA" -PrimarySmtpAddress "GroupA@domain.com"
}
catch
{
    # Breakpoint here
    Write-Host $_.Exception.SerializedRemoteInvocationInfo
    throw
}

我得到的异常如下:

代码语言:javascript
复制
The operation couldn't be performed because object 'domain.com/Groups/Automated/GroupA' couldn't be found on 'dc2.domain.com'.

请注意,它讨论的是dc2,而不是我在DomainController属性上设置为默认值的dc1。

更深入地查看错误记录,我看到exceptions SerializedRemoteInvocationInfo属性只将AliasPrimarySmtpAddressIdentity显示为绑定参数。看起来没有为DomainController传入任何东西。

我知道我可以重构并显式定义DomainController参数,或者使用脚本范围的哈希表和splatting,但这需要进行大量的更改,我真的很好奇为什么这种行为是这样的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-05 02:19:31

当您对这些Exchange会话使用隐式远程处理时,Exchange cmdlet(实际上是代理函数)将被传递到远程会话执行,然后将结果传递回您的本地会话。该哈希表在远程会话中不存在。

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

https://stackoverflow.com/questions/14162488

复制
相关文章

相似问题

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