我创建了一个dataTable,并从SQL查询中填充了PowerShell。
我需要遍历每个datatable行,在(space/space)上找到一个匹配项,并将其替换为(/) (根据客户的请求)
我需要替换每一行的匹配项。
我已经做了无数次搜索,并尝试了以下方法:
$dt | where {$_.l4 -cmatch " / "} | foreach {$_.l4 = "/"}
但这会将整个值替换为(/)
我尝试过使用replace、creplace和许多其他ForEach循环、IF语句等,..
这基本上就是我想要做的:
#loop through the datatable and replace " / " with "/"
foreach ($Row in $dt.Rows) {
if($Row -cmatch " - "){
$Row -creplace " - ","/"
}
}请告诉我我明目张胆地漏掉了什么。
在我进一步了解PowerShell的过程中,我感谢您的帮助和指导!
发布于 2019-09-20 01:56:43
您基本上已经得到了,但是当您执行-creplace时,您需要将这个新值赋给$dt中该行的现有值。
我假设您想要在第一个代码示例中的特定列中进行替换。如果是这样的话,这将会起作用:
foreach ($Row in $dt) {
$Columns = $Row | Get-Member -MemberType Property
foreach ($Column in $Columns) {
$Row.($Column.Name) = $Row.($Column.Name) -creplace " / ", "/"
}
}https://stackoverflow.com/questions/58015485
复制相似问题