PowerShell版本2中的DPM命令

3

我正在运行一个需要连接到DPM服务器的PowerShell脚本。

当我在DPM管理Shell中运行Connect-DPMServer <DPM服务器名称> cmdlet时,命令成功并且我能够连接到服务器。

然而,当我将相同的命令放在脚本中,并通过DPM管理Shell调用该脚本时,会出现以下错误:

The term 'Connect-DPMServer' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
+ CategoryInfo          : ObjectNotFound: (Connect-DPMServer:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

类似的情况也发生在其他DPM cmdlet上,例如Get-DPMProtectionGroup。
我在Windows Server 2008 R2上运行Powershell版本2.0。
这种特殊行为的原因是什么,如何避免?
编辑
我有一些观察结果。我的脚本有两个部分:一个包装脚本和一个作为独立作业由包装脚本调用的辅助脚本。
所有的DPM命令都在包装脚本中标识,但当它作为作业运行时,在辅助脚本中没有被标识。
为什么会出现这种情况,有什么建议来解决这个问题?

您可以尝试在未正常工作的 shell 中通过 Get-command 列出已安装的命令。 - S.Spieker
我建议在你的辅助脚本中打印出 Get-Command 并使用 $PSVersionTable 输出 Powershell 版本以缩小范围。 - S.Spieker
1个回答

1
我想到了解决方案,以下是: 发生了什么 包装脚本在DPM PowerShell中运行,然后作为单独的作业或线程调用辅助脚本。此辅助脚本运行的环境是Windows本机PowerShell而不是DPM PowerShell。因此,在那里无法识别DPM命令。 解决方案 需要在调用辅助脚本时立即导入DPM特定模块。步骤如下:
  1. 右键单击DPM管理Shell图标,查看属性。

  2. 选择目标的值。对我来说,它看起来像这样:C:\Windows\system32\windowspowershell\v1.0\powershell.exe -noexit -File "D:\DPM\DPM\bin\dpmcliinitscript.ps1"

  3. 参数-File的值是" D:\ DPM \ DPM \ bin \ dpmcliinitscript.ps1",将其导入Windows Powershell后,可以将其转换为DPM Management Console。这意味着它会加载带有DPM命令的shell。

  4. 通过点操作符引入此文件到辅助脚本中。这意味着辅助脚本的第一行应该如下所示:."D:\DPM\DPM\bin\dpmcliinitscript.ps1"

    这将帮助调用的shell识别DPM特定的命令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接