我正在尝试使用$PSDefaultParameterValues来简化我的生活。我遇到了这样一个问题:当调用通过隐式远程处理加载的cmdlet时,我在$PSDefaultParameterValues中设置的任何内容都会被忽略。
下面是我目前遇到的一个简化示例:
$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
}我得到的异常如下:
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属性只将Alias、PrimarySmtpAddress和Identity显示为绑定参数。看起来没有为DomainController传入任何东西。
我知道我可以重构并显式定义DomainController参数,或者使用脚本范围的哈希表和splatting,但这需要进行大量的更改,我真的很好奇为什么这种行为是这样的。
发布于 2013-01-05 02:19:31
当您对这些Exchange会话使用隐式远程处理时,Exchange cmdlet(实际上是代理函数)将被传递到远程会话执行,然后将结果传递回您的本地会话。该哈希表在远程会话中不存在。
https://stackoverflow.com/questions/14162488
复制相似问题