Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Powershell中将多个字符串连接并聚合在一起_Powershell - Fatal编程技术网

在Powershell中将多个字符串连接并聚合在一起

在Powershell中将多个字符串连接并聚合在一起,powershell,Powershell,我正在尝试使用PowerShell运行广告程序 $tpObject = Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution ` | Select-Object -Property PKG_Manufacturer, PKG_Name, PKG_MIFVersion 输出将为: PKG_Manufacturer PKG_Name PKG_MIFVersi

我正在尝试使用PowerShell运行广告程序

$tpObject = Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution `
    | Select-Object -Property PKG_Manufacturer, PKG_Name, PKG_MIFVersion
输出将为:

PKG_Manufacturer PKG_Name PKG_MIFVersion
---------------- -------- --------------
Microsoft        Word     v1234
Google           Chrome   v987
Microsoft        Excel    v987
etc
如何将其连接成字符串?我试过这个:

[string[]]$result = $tpObject.PKG_Manufacturer + $tpObject.PKG_Name + " - " + $tpObject.PKG_MIFVersion  
$result
但它会显示所有PKG_制造商,然后是PKG_名称,然后是PKG_版本
我希望它将此,Microsoft Word-v1234显示为字符串?
如有任何建议或意见,将不胜感激

tks

尝试一下:

$result=@()
Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution | %{
$result += "$($_.PKG_Manufacturer) $($_.PKG_Name) - $($_.PKG_MIFVersion)"}
$result
请参阅
-f

样本输出:

Microsoft Word - v1234
Google Chrome - v987
Microsoft Excel - v987

我猜你想要的是这样的东西

$list = New-Object 'System.Collections.Generic.List[string]'
Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution `
    | ForEach-Object $list.Add("$($_.PKG_Manufacturer) $($_.PKG_Name) - $($_.PKG_MIFVersion)")
要连接和聚合字符串数组中的值,有几个选项

首先,可以连接字符串:

  • 您可以使用
    +
    操作符(由您应用)在PowerShell中压缩字符串
  • 您可以使用
    -f
    格式化方法(如在C#中)
    “我的电脑{0}有{1}MB内存。”-f“L001”,“4096”
  • 或者,您可以将双引号字符串与变量一起使用
    $x=“Max”;编写输出“I'm$x”
    (Hind:有时您需要子表达式扩展,如上面的示例所示。请参见第34页)
  • 其次,您可以汇总您的结果:

  • 将动态类型的数组
    $testArray=@()
    $testArray+=$一起使用
  • 使用类型化数组
    [string[]]$testArray=[string[]]
    以及
    +=
    ,如下所示
  • 使用带有Add方法的generic List
    $List=New Object'System.Collections.generic.List[string]
    还有更多
  • 一般来说,我会尽可能长时间地使用PS对象。
    将内容聚合到字符串数组中以便以后处理,这是程序员太多的想法

    在您的代码中:
    1.不能使用
    =
    运算符添加新数组元素,请改用
    +=

    $testArray = @()
    $tempArray = "123", "321", "453"
    $tempArray | ForEach {$testArray += $item }
    $testArray
    

    这只显示了连接字符串的方法,而没有显示如何聚合字符串。这不就是
    $i+=Get WmiObject[…]}$i
    ?这是关于连接和聚合的,不是吗。否则,这将只是如何在PowerShell中连接字符串和变量的普通副本?不鼓励只使用代码的答案,因为它们没有解释如何解决问题中的问题。考虑更新你的答案来解释这个问题,以及它是如何解决问题的。请回顾@FluffyKitten,大体上我同意-但是在这个问题和我非常相似的答案的背景下,我认为我不需要表达非常明显的意思。谢谢更新你的答案。它被标记为低质量,所以我在审查过程中添加了此评论。
    $testArray = @()
    $tempArray = "123", "321", "453"
    $tempArray | ForEach {$testArray += $item }
    $testArray