【发布时间】:2017-04-17 20:56:25
【问题描述】:
我是 Powershell 脚本的新手,我正在处理用户管理 Powershell 脚本,但我遇到了一个奇怪的错误。
以下代码在我从 shell 运行时有效,但在从脚本运行时无效:
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://servername/Powershell/ -Authentication Kerberos -Credential $UserCredential -AllowRedirection
Import-PSSession $Session
当我在第一行带有Param() 块的脚本中运行它时,它会失败并出现以下错误:
Import-PSSession: Cannot bind argument to parameter 'Path' becuase it is an empty string.
如果我删除我的Param() 块,我可以让Import-PSSession 工作,但我不确定如何接受我的脚本的命令行参数。如何保留Param() 块(或更一般地说,接受命令行参数)并且仍然能够导入 PS 会话?
我在尝试连接到 2010 Exchange 服务器的 Windows 2008 R2 服务器上使用 Powershell v2。
更新:
我有一个名为 ManageUser.ps1 的脚本,我从类似的 Powershell 提示符运行该脚本
.\ManageUser.ps1 -disableUser -username someuser
脚本开始如下:
Param(
[switch]$disableUser,
[string]$username
)
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://servername/Powershell/ -Authentication Kerberos -Credential $UserCredential -AllowRedirection
Import-PSSession $Session
#more stuff past here...
Import-PSSession 命令失败。但是,如果我删除整个 Param(...),会话导入将起作用。我希望这足以让你理解它!
【问题讨论】:
-
您希望人们在不显示您的代码的情况下解决您的代码问题?你是如何编写
param()块的?你是怎么调用脚本的? -
@TessellatingHeckler 对不起,我以为我已经提供了足够的信息。我现在更新了我的问题。请让我知道是否还有其他有助于了解的信息!谢谢。
-
只是想知道您是否尝试将 -AllowClobber 选项添加到 Import-PSSession?也许Powershell在后台发生了一些默认参数设置?
-
@SamuelWarren 我已经有一段时间没有使用这个脚本了,但我很确定我确实尝试过添加 -AllowClobber。感谢您的建议。
标签: powershell powershell-2.0 windows-server-2008-r2 exchange-server-2010