powershell - 新对象不能包含破折号(-),但可以包含 “needs to”

标签 powershell export-to-csv

我正在创建一个用于将值导出到CSV的新对象:

New-Object -TypeName PSObject -Property @{
            host_name = ($server.name).ToLower()
            address = $IPAddress
            host_is_collector = "no"
            host-preset = "windows-server"
        } | Select-Object host_name,address,host-preset | Export-Csv -Path $nConf_import_host_file

问题是其中一行包含短划线(主机预设)。我当然会简单地将其更改为下划线,但是我的CSV需要将此值设为破折号。创建完整个csv之后,我还可以在整个csv上执行-replace,但这看起来很脏。
有没有办法在这里使用破折号?

我的错误讯息是:
Missing '=' operator after key in hash literal.
At Z:\Scripts\Testscripts\ScanServers_and_check_nagiosV7.ps1:336 char:16
+                 host-preset <<<<  = "windows-server"
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingEqualsInHashLiteral

最佳答案

您只需要通过将括在引号中将host-preset属性名称视为字符串即可:

New-Object -TypeName PSObject -Property @{ "host-preset" = "windows-server" }

关于powershell - 新对象不能包含破折号(-),但可以包含 “needs to”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8910257/

相关文章:

powershell - 在 powershell 中读取具有可变列数的 CSV

javascript - csv 中的 HTML 表格数据

php - 从 MySQL 数据库导出数据到 PHP 中的 excel

linux - 如何将多个 html 文件中的表格提取到一个 csv 文件中?

php - 如何使 fputcsv "echo"数据

windows - 如何获取每个文件的创建时间并格式化

azure - 在 PowerShell Runbook 中使用 Azure 文件共享

mysql - Powershell 中的 last_insert_id

powershell - PowerShell 的 -Version 参数到底有什么作用?

json - 输出分号分隔的字符串