【问题标题】:rundeck unable to execute powershell script with import-modulerundeck 无法使用 import-module 执行 powershell 脚本
【发布时间】:2018-06-25 15:16:43
【问题描述】:

我正在 Windows 2012 目标节点上通过 rundeck(内联)运行几个 python 脚本。这些脚本曾经在本地运行,但现在正在转移到 rundeck。

其中一个 python 脚本打开子进程以调用 powershell 脚本并读取输出。

import subprocess
CMD = [r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe ', '-File', r'C:\Users\Osman\Code\mop.ps1']
cmd = CMD[:]
cmd.append('arg1')
cmd.append('arg2')
cmd.append('arg3')
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
r = p.communicate()
print(r)

mop.ps1是

Import-Module MSOnline
$domain = $args[0]
$login = $args[1]
$pass = $args[2]
$EPass = $pass | ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PsCredential($login, $EPass)
Connect-MsolService -Credential $Cred
$TenantId = Get-MsolPartnerContract -Domain $domain | Select-Object -ExpandProperty TenantId
Get-MsolAccountSKU -TenantId $TenantId | select SkuPartNumber,ActiveUnits,ConsumedUnits | ConvertTo-Csv -NoTypeInformation

这部分代码总是无法执行,如果我检查 stderr 它会说:

        Connect-MsolService : Exception of type 'Microsoft.Online.Administration.Automation.MicrosoftOnlineException' was thrown.
At C:\Users\Osman\Code\mop.ps1:7 char:1 
+ Connect-MsolService -Credential $Cred

我不确定它为什么会失败。我试过了

Import-Module MSOnline -Verbose

我可以看到 Cmdlet 正在加载。我尝试在C:\WINDOWS\system32\WindowsPowerShell\v1.0\ 位置创建 profile.ps1 文件。

如果我在本地执行代码,一切正常。我尝试运行常规测试 .ps1 文件“disk.ps1”而不是我的代码,这很好,因为它不加载任何模块:

get-WmiObject win32_logicaldisk -Computername $env:computername

有什么解决方法可以让带有模块的脚本正常运行?标准输出始终为空。

节点注册为 64 位,所以我尝试将 cmd 更改为

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe 

我尝试在那里复制 profile.ps1,在那里复制了模块,但仍然无法通过 rundeck 工作。

【问题讨论】:

    标签: python powershell rundeck


    【解决方案1】:

    根据您的描述,由于您在直接从服务器运行脚本时能够获得有效输出,因此您的错误可能与您用于登录 MS 的“Second Hop”有关-在线的。目前Rundeck Python-Winrm plugin支持Basic、ntlm或CredSSP认证,CredSSP认证可以让你成功执行第二跳。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 2019-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多