我正在尝试运行一个 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": "对象引用未设置到对象的实例。\r\n在第 2 行 char:1\r\n+ $database = $windowsInstaller.OpenDatabase($msiFilePath, 0)\r\n+ ~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~\r\n + CategoryInfo : 操作停止: (:) [], NullReferenceException\r\n + ExcellentQualifiedErrorId : System.NullReferenceException\r\n \r\n",
“pipeline_iteration_info”:[],
"script_stack_trace": "在 , : 第 2 行",
“目标对象”:空
},
{
“类别信息”:{
“活动”: ””,
"category": "无效操作",
“类别id”:7,
“原因”:“运行时异常”,
“目标名称”:“”,
“目标类型”:“”
},
“错误详细信息”:空,
“例外”: {
“帮助链接”:空,
“结果”:-2146233087,
“内部异常”:空,
"message": "您不能对空值表达式调用方法。",
"source": "匿名托管的 DynamicMethods 程序集",
“类型”:“System.Management.Automation.RuntimeException”
},
"full_qualified_error_id": "InvokeMethodOnNull",
"output": "您不能在空值表达式上调用方法。\r\n在第 4 行 char:1\r\n+ $view = $database.OpenView($query)\r\n+ ~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n + CategoryInfo : InvalidOperation: (:) [], RuntimeException\r\n + FullQualifiedErrorId : InvokeMethodOnNull\r\n \r\n",
“pipeline_iteration_info”:[],
"script_stack_trace": "在 , : 第 4 行",
“目标对象”:空
},
-
多变的$msiFilePath
未在您的内联 PowerShell 脚本中设置,这使得它的计算结果为$null
.
- Passing
$null
作为 MSI 文件路径.OpenDatabase() method https://learn.microsoft.com/en-us/windows/win32/msi/installer-opendatabase结果是您看到的(模糊的)错误消息。
-
虽然你确实通过了msiFilePath
Ansible 端的参数,该值将成为PowerShell变量有了这个名字,你必须使用参数声明您的 PowerShell 脚本, 以键值对中的键命名parameters:
YAML 部分, 用一个param(...)
块,如下图:
...
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>'
...
Note:
-
上述技术显示在文档中的示例ansible.windows.win_powershell module https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_powershell_module.html#examples.
-
有关声明 PowerShell 脚本和函数参数的更多信息,请参阅的相关部分about_Functions帮助主题 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Functions#functions-with-parameters.
-
中的键值对的方式parameters:
YAML 部分被转换为 PowerShell 参数,类似于基于哈希表的方式飞溅 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Splatting在 PowerShell 内部工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)