从ansible运行powershell脚本并注册输出

问题描述 投票:0回答:1

我正在尝试运行一个 Powershell 脚本,该脚本在远程 Windows PC 上使用 ansible 从 MSI 文件读取 ProductGUID。 powershell 在本地工作台上作为 ps1 文件运行。但从 ansible 中它会抛出一个错误,指出对象引用未设置为对象的实例。

这是我的ansible代码

- name: Run PowerShell script to find out Product Code
  ansible.windows.win_powershell:
    script: |
           $windowsInstaller = New-Object -ComObject WindowsInstaller.Installer
           $database = $windowsInstaller.OpenDatabase($msiFilePath, 0)
           $query = 'SELECT Value FROM Property WHERE Property = ''ProductCode'''
           $view = $database.OpenView($query)
           $view.Execute()
           $record = $view.Fetch()

           if ($record) {
                $productGUID = $record.StringData(1)
                echo "Product GUID: $productGUID"
           } else {
                echo "Product GUID not found in the MSI file."
           }

           if ($view) {
                $view.Close()
           }
    parameters:
      msiFilePath: '<Path_to_MSI>'
  register: PS_output
  tags:
    - read_msi

- name: Show PS_output
  debug:
    var: PS_output
  tags:
    - read_msi 

输出显示:

更改:[mb018-windows] => { “改变”:真实, “调试”:[], “错误”: [ { “类别信息”:{ “活动”: ””, "category": "操作已停止", “类别id”:14, "原因": "NullReferenceException", “目标名称”:“”, “目标类型”:“” }, “错误详细信息”:空, “例外”: { “帮助链接”:空, “结果”:-2147467261, “内部异常”:空, "message": "对象引用未设置到对象的实例。", "source": "系统.管理.自动化", “类型”:“System.NullReferenceException” }, "full_qualified_error_id": "System.NullReferenceException", “output”:“未将对象引用设置为对象的实例。 行:2 字符:1 + $database = $windowsInstaller.OpenDatabase($msiFilePath, 0) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~ + 类别信息:操作停止:(:) [],NullReferenceException + FullQualifiedErrorId:System.NullReferenceException “, “pipeline_iteration_info”:[], "script_stack_trace": "在 , : 第 2 行", “目标对象”:空 }, { “类别信息”:{ “活动”: ””, "category": "无效操作", “类别id”:7, “原因”:“运行时异常”, “目标名称”:“”, “目标类型”:“” }, “错误详细信息”:空, “例外”: { “帮助链接”:空, “结果”:-2146233087, “内部异常”:空, "message": "您不能对空值表达式调用方法。", "source": "匿名托管的 DynamicMethods 程序集", “类型”:“System.Management.Automation.RuntimeException” }, "full_qualified_error_id": "InvokeMethodOnNull", "output": "您不能对空值表达式调用方法。 行:4 字符:1 + $view = $database.OpenView($query) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException +FullyQualifiedErrorId:InvokeMethodOnNull “, “pipeline_iteration_info”:[], "script_stack_trace": "在 , : 第 4 行", “目标对象”:空 },

windows powershell ansible windows-installer
1个回答
0
投票
    您的脚本中未设置
  • $msiFilePath
    ,这使得它的计算结果为
    $null

    • $null
      作为 MSI 路径传递给
      .OpenDatabase()
      方法会导致您看到的(模糊的)错误消息。
  • 虽然您确实在 Ansible 端传递了

    msiFilePath
    参数,但要使该值成为具有该名称的 PowerShell 变量,您必须使用 param(...) 块声明您的 PowerShell 脚本
    带参数
    ,如下所示如下图:

...

    script: |
           # DECLARE YOUR PARAMETER(S) HERE.
           # The name(s) must match the ones in the parameters: YAML section.
           param([string] $msiFilePath)

           $windowsInstaller = New-Object -ComObject WindowsInstaller.Installer
           $database = $windowsInstaller.OpenDatabase($msiFilePath, 0)
           $query = 'SELECT Value FROM Property WHERE Property = ''ProductCode'''
           $view = $database.OpenView($query)
           $view.Execute()
           $record = $view.Fetch()

           if ($record) {
                $productGUID = $record.StringData(1)
                echo "Product GUID: $productGUID"
           } else {
                echo "Product GUID not found in the MSI file."
           }

           if ($view) {
                $view.Close()
           }
    parameters:
      msiFilePath: '<Path_to_MSI>'

...

上述技术在 ansible.windows.win_powershell

 模块
文档中的 示例中有所展示。

© www.soinside.com 2019 - 2024. All rights reserved.