string - 从Powershell中的字符串添加哈希表中的条目
问题描述
我有一个这样的字符串,从 txt 文件中检索:
$txtindex = 5
$PRINTERNAME = get-content $txtPrinterfile | select -Index $txtindex
$PRINTERNAME = $PRINTERNAME.Trim()
$PRINTERNAME = $PRINTERNAME.Replace(" ","")
$Printername
NAME="W018"
所以我想我可以轻松地将其添加到哈希表中:
$HashPrinter = @{}
$HashPrinter.Add($PRINTERNAME)
但这不起作用。
要么:
$HashPrinter = @{$PRINTERNAME}
如果我手动输入:
$HashPrinter = @{NAME="W018"}
它按预期工作。我做错了什么?
解决方案
PS C:\Users\alHaos> $HashTable = @{}
PS C:\Users\alHaos> $HashTable.Add("Name", "Pr001")
PS C:\Users\alHaos> $HashTable.Add("Manufacture", "Epson")
PS C:\Users\alHaos> $HashTable
Name Value
---- -----
Name Pr001
Manufacture Epson
推荐阅读
- ios - 过滤字典
- python - ValueError:无法将字符串转换为浮点数:'4,6'
- wordpress - Wordpress 插件故障转移 sftp (WinSCP)
- android - Resources$NotFoundException -- 天气应用
- javascript - React-Native/Redux 调度多次触发
- powershell - 使用 PowerShell CIM 重新启动 2008 Server
- java - 将特定短信从默认短信应用程序导入我的应用程序
- python - 多折线图功能
- render - wkhtmltopdf 和 MathJax:方程渲染得太小
- python - 打印出由类创建的列表对象