新对象不能包含短划线 (-),但"needs to"



我正在创建一个新对象,用于将值导出到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创建后对其进行-替换,但这似乎很脏。有没有办法让我在这里用破折号?

我的错误消息是:

Missing '=' operator after key in hash literal.
At Z:ScriptsTestscriptsScanServers_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" }

相关内容

最新更新