我有一个名为“conf.d”的配置文件,我需要使用powershell自动化进行更新。以下是conf.d文件的1个块:
- name: NameHere
url: www.example.com
http_response_status_code: (1|2|3)\d\d
collect_response_time: true
disable_ssl_validation: false
ignore_ssl_warning: true
check_certificate_expiration: true
days_warning: 30
days_critical: 10
tags:
- Name:Tag
文件的其余部分就是那个配置块一遍又一遍地重复。是否有一个我可以编写的Powershell脚本,它会在文件的末尾添加另一个这样的块?
理想情况下,我会使用变量以及名称,网址和标记插槽来执行此操作。
是的你可以。为此,您可能希望将块准备为模板,并包含可由其值替换的变量。如评论中所述,您可以使用Add-Content
附加到文件中,或者您可以将Out-File
与-append
一起使用。
$name = "test"
$content = "Name:$name"
$content | Out-File test.txt