导入 PSSession 在脚本中失败,在 shell 中工作 [英] Import-PSSession fails in script, works in shell

查看:36
本文介绍了导入 PSSession 在脚本中失败,在 shell 中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Powershell 脚本的新手,我正在处理用户管理 Powershell 脚本,但我遇到了一个奇怪的错误.

I'm new to Powershell scripting and I'm working on a user management Powershell script, but I have run into a strange error.

以下代码在我从 shell 运行时有效,但在从脚本运行时无效:

The following code works when I run it from the shell, but not when it is run from a script:

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://servername/Powershell/ -Authentication Kerberos -Credential $UserCredential -AllowRedirection
Import-PSSession $Session

当我在第一行带有 Param() 块的脚本中运行它时,它失败并显示以下错误:

When I run it in a script with a Param() block on the first line, it fails with the following error:

Import-PSSession: Cannot bind argument to parameter 'Path' becuase it is an empty string.

如果我删除我的 Param() 块,我可以让 Import-PSSession 工作,但我不确定如何接受我的命令行参数否则脚本.我怎样才能保持 Param() 块(或更一般地说,接受命令行参数)并且仍然能够导入 PS 会话?

I can get the Import-PSSession to work if I remove my Param() block, but I'm not sure how to accept command-line arguments for my script otherwise. How can I keep the Param() block (or more generally, accept command-line arguments) and still be able to import the PS session?

我在尝试连接到 2010 Exchange 服务器的 Windows 2008 R2 服务器上使用 Powershell v2.

I'm using Powershell v2 on a Windows 2008 R2 server trying to connect to a 2010 Exchange server.

更新:

我有一个名为 ManageUser.ps1 的脚本,我从类似的 Powershell 提示符处运行该脚本

I have a script named ManageUser.ps1 that I run from a Powershell prompt like

.\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(...),会话导入工作.我希望这足以让你理解它!

It is failing on the Import-PSSession command. But, if I remove the whole Param(...), the session import works. I hope this is enough for you to understand it!

推荐答案

您的脚本可以单独运行,也可以从另一个脚本中调用:

Your script works fine on it's own and also when called from within another script:

& 'C:\Scripts\ManageUser.ps1' -disableUser -username "foo"
Get-Mailbox -resultsize 5 | ConvertTo-Csv -NoTypeInformation | Out-File C:\Scripts\mytest.csv

由于没有@SamuelWarren 提到的 -allowclobber,后续运行将收到导入错误/警告...

Subsequent runs will get the import error/warning due to not having -allowclobber as mentioned by @SamuelWarren...

在您的情况下(至少在很久以前最初编写问题时),错误是由于您在此处未提及的另一个变量造成的.您很可能在第一次运行后修复了这个问题,随后的测试显示了 AllowClobber 错误.

In your case (at least when initially writing the question long ago) the error was due to another variable that you haven't mentioned here. It's likely you fixed that after the first run, and then subsequent tests were showing the AllowClobber error.

还值得注意(对于将来遇到此问题的任何人)检查您正在调用的文件的路径.仅仅因为您尝试使用相对路径 .\myfile.ps1 并不意味着 PowerShell 当前正在寻找正确的目录.

Also worth noting (for anybody who comes across this in the future) to check the path of the file you are calling. Just because you try to use a relative path .\myfile.ps1 doesn't mean that PowerShell is currently looking in the right directory.

检查:$psscriptroot

Split-Path -Path $($global:MyInvocation.MyCommand.Path)

这篇关于导入 PSSession 在脚本中失败,在 shell 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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