【问题标题】:Cluster, Node from Python集群,来自 Python 的节点
【发布时间】:2017-11-03 06:28:41
【问题描述】:

我有一个集群的 DNS 名称,该集群上运行服务的节点很少。 我首先编写了 powershell 脚本,它为我提供了正在运行所提供服务的节点及其状态。脚本使用 Get-ClusterResource 命令 然后我写了执行这个powershell的python脚本。 当我运行 powershell 脚本时,我正在获取节点及其状态。但是,当我从 python 运行 powershell 脚本时,我收到以下错误:

Get-ClusterResource : The term 'Get-ClusterResource' 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.

我尝试使用import-module failovercluster,但没有成功。

请让我知道我错在哪里,如果不可能,那么我正在寻找 python lib 来实现同样的目标。

我对集群的一些理解: - 我们不能create PSSession 用于集群。

我正在以这种方式运行 powershell 脚本:

subprocess.check_output(["powershell.exe", '-ExecutionPolicy', 'Unrestricted', self.win_cluster_handling() + " -Cluster_Name " + self.cluster_name + " -ClusterNode_Action Stop-Cluster" + " -Service_Name " + self.service_name + " -Username " + self.username + " -Password " + self.password])

【问题讨论】:

  • 您从 Python 运行的 PowerShell“脚本”是否只是单个命令:powershell.exe -Command "Get-ClusterResource"(或类似命令)?
  • 是的。这是一个命令,powershell.exe,然后将一些参数传递给它,ps1 接受并给出结果。 Insode ps1 我有命令 Get-CLusterResource
  • 啊,好的,所以你调用的是一个实际的脚本,而不仅仅是一个命令。请编辑您的原始帖子并将 Python 中的代码行放在那里(而不是在下面答案的 cmets 中)。
  • 您是否尝试使用 python 脚本远程查询集群?您是在运行 python 脚本的同一台机器上测试 PowerShell 脚本吗?
  • 完成。谢谢你的建议。

标签: python-2.7 python-3.x powershell powershell-3.0 powershell-remoting


【解决方案1】:

您是否也在脚本中包含了Import-module FailoverClusters

看看这个question

一个建议是更新到 PowerShell v3.0 或更高版本。

【讨论】:

  • 模块不包含在 python 脚本中。如何将其包含在 python 脚本中。我当前的 powershell 版本是 5.1.14409.1005。我从 python 中检查了这个命令:os.system(r"powershell get-module -listAvailable"),发现 Failovercluster 模块丢失了。我们如何添加它以使其上市。
  • 当我执行命令时: output = 'Get-ChildItem Env: from python 它不会打印任何变量,但会形成 powershell。
  • 如果将output =放在 PowerShell 脚本中,则环境变量列表将写入变量而不发送到输出流;因此 powershell.exe 不会向调用者 (python) 返回任何输出。
  • 如果您还没有这样做,将以下行添加到您的 PowerShell 脚本中:Import-Module FailoverClusters。这将显式加载模块 in powershell 会话;至少我们会看看这一行是否会引发错误,这似乎证明加载模块存在问题。
  • 如果Get-Module -ListAvailable 命令不包含ClusterFailovers 模块,那么这听起来不太好。如果您在直接从 PowerShell 运行时可以看到该模块,请检查它的位置:Get-Module -ListAvailable | Sort Name | Select Name,Path
【解决方案2】:

重新阅读您的描述,您从 Python 调用的 Powershell 脚本中出现问题。在您的 Powershell 脚本中,您是否可以编写一些行来确定脚本是否使用与正常运行 Powershell 时相同的环境变量、PATH 等运行。

例如,如果您从 Powershell 运行:“Get-ChildItem Env:”,它会为您提供环境变量列表。您可以在您的 powershell 脚本中包含相同的内容,并检查它们是否相同,或者您的脚本运行的脚本中是否缺少某些内容。

【讨论】:

  • 当我单独运行 powershell 时,它可以工作。但是从python它失败了。当我执行这个命令 os.system(r"powershell get-module -listAvailable") 我可以看到故障转移集群模块丢失。但是当我从 powershell 运行它时,它就可以工作了。
  • 当我执行命令时: output = 'Get-ChildItem Env: from python 它不会打印任何变量,而是形成 powershell
猜你喜欢
  • 2022-07-07
  • 1970-01-01
  • 2013-04-24
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 1970-01-01
  • 2016-08-14
相关资源
最近更新 更多