替换对象属性中的一些文本? [英] Replace some text in an objects property?

查看:35
本文介绍了替换对象属性中的一些文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要请求一些以对象形式出现的信息.我需要在其中一个属性中替换一些测试,然后将对象列表写入 CSV.

I have a requirement to request some information that comes in the form of an object. I need to replace some test in one of the properties and then write the list of objects to CSV.

当我这样做

Get-Process | select * | %{ $_Path.Replace("chrome", "ie") }

我有两个问题

  1. 如果 $_.Path 为空,它会给我一个错误,你不能在空值表达式上调用方法
  2. 输出是一个字符串,代表被替换的文本(只是 Path 属性).我需要保留原始对象及其所有属性,但需要更新路径值.

当然,当我尝试这样做时

So of course when I try to do

Get-Process | select * | %{ $_Path.Replace("chrome", "ie") } | Export-Csv -Path "out.csv"

我得到的是单个属性 Length 因为上面的输出是一个只有 Length 属性的字符串.

What I get is a single property Length because the output of the above is a string with only the Length property.

推荐答案

Get-Process | select * | %{ $_.Path = $_.Path.Replace("chrome", "ie"); $_ } | Export-Csv -Path "out.csv" -NoTypeInformation

  • $_.Path 而不是 $_Path
  • 将替换后的文本分配回路径属性
  • 并在完成赋值后输出对象
  • 帮助链接(如果有):

    • Get-Process (in module Microsoft.PowerShell.Management)
    • select is an alias for Select-Object (in module Microsoft.PowerShell.Utility)
    • % is an alias for ForEach-Object
    • Export-Csv (in module Microsoft.PowerShell.Utility)

    这篇关于替换对象属性中的一些文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆