gpt4 book ai didi

powershell - 在变量 SaltStack 中使用 PS 命令

转载 作者:行者123 更新时间:2023-12-04 07:32:55 24 4
gpt4 key购买 nike

我有一个状态,如果文件夹存在与否,它应该给我一个输出。
我不知道如何转义文件夹 Programs 文件的名称,所以 PS 知道我的意思。

{% set label = salt['cmd.run']('powershell.exe "test-path \"$env:SystemDrive\\Program Files (x86)\\Folder\""') %}

{% if label == 'True' %}
First:
cmd.run:
- name: Write-Host "Exist"
- shell: powershell

{% else %}
Secondary:
cmd.run:
- name: Write-Host " NOT "
- shell: powershell
{% endif %}
我每次尝试都没有得到,但文件夹存在
你能告诉我如何使这个工作。我在哪里可以找到有关如何在 YAML 中转义行的信息

最佳答案

我建议使用 Saltstack file module使用 directory_exists 检查目录是否存在功能。
这允许我们使用 requisite system触发进一步的状态。
例子:

check-dir-exists:
module.run:
- name: file.directory_exists
- path: 'C:\Program Files (x86)\SomeDirectory'

show-dir-exists:
cmd.run:
- name: Write-Host "Dir exists"
- shell: powershell
- require:
- module: check-dir-exists

show-dir-not-exist:
cmd.run:
- name: Write-Host "Dir not exists"
- shell: powershell
- onfail:
- module: check-dir-exists
或者存储返回值 True/Falseif/else 中的变量和匹配条件中陈述。
例子:
{% set label = salt['file.directory_exists']('C:\Program Files (x86)\SomeDirectory') %}

{% if label %}
show-dir-exists:
cmd.run:
- name: Write-Host "Dir exists"
- shell: powershell
{% else %}
show-dir-not-exist:
cmd.run:
- name: Write-Host "Dir not exists"
- shell: powershell
{% endif %}

关于powershell - 在变量 SaltStack 中使用 PS 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67867881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com