powershell - Powershell - 在 Here-String 内替换
问题描述
我需要在if
里面使用语句here-string
。但是当我查看变量内部时,我看到true
而不是$true
表达式$($row.'Secondary Network Adapter' -eq 'Y')
将被替换为$true
or $false
。
$row = @{
SecondaryNetworkAdapter = "Y"
}
$code1 = @'
if($($row.SecondaryNetworkAdapter -eq 'Y')){
Set-NetIPInterface -InterfaceAlias '$nic1Name' -AutomaticMetric disabled -InterfaceMetric 5
}
'@
然后,我正在运行以下行:
$ExecutionContext.InvokeCommand.ExpandString($code1)
输出:
if(True){ Set-NetIPInterface -InterfaceAlias 'LAN' -AutomaticMetric disabled -InterfaceMetric 5 }
亲切的问候,
解决方案
$
在表达式前添加转义文字:
if(`$$($row.SecondaryNetworkAdapter -eq 'Y')){
推荐阅读
- sql - 查找参加 SQL 课程最多的学生
- amazon-web-services - 使用 AWS 云形成堆栈的 SNS 事件目的地不起作用
- clojure - 按值属性对地图进行排序
- php - 图像中的firebase存储不会在laravel中生成访问令牌
- python - 如何清除嵌入 PyQt5 的绘图?
- reactjs - 在 create-react-app 中的 public/ 目录中链接到 pdf
- c# - $unset 二维数组 MongoDB (C#)
- javascript - 克隆 HTMLCollection
- php - PHP 代码有时会在带有 isset($_POST 的 html 文件中运行或不运行
- python - 为什么导入后图像大小会发生变化