我正在创建一个用于将值导出到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/