将对象转换为字符串

时间:2018-11-16 08:42:15

标签: powershell

我设置了一个参数,使我可以显示工具的输出:

Function LogWrite
{
    #This function takes the passed text and adds it to the on screen log and the external log file

    Param ([string]$logstring)
    #Get current date and time
    $now = Get-Date -Format g
    #Write to screeen
    $txb_logs.Appendtext("------------------------------------------------------------------------------ `r`n")
    $txb_logs.Appendtext("$now - $logstring `r`n")

}

我想将以下内容转换为字符串吗?

$StartUp =  Get-WmiObject Win32_StartupCommand -ComputerName $ComputerName | Select Caption

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

  

我只是将标题列和每一项返回到新行的内容

您可以将其转换为CSV,这样就可以实现。第一行中的列名,然后是新行中的每个项目。这将适应任何输入,因此您可以自由选择更多列或完全选择其他对象。

Get-WmiObject Win32_StartupCommand -ComputerName $ComputerName | Select Caption | ConvertTo-CSV -NoTypeInformation

稍作修改,记录功能就可以容纳多行输入:

$logstring | ForEach-Object { $txb_logs.Appendtext("$now - $_`r`n") }

您也可以使用Format-Table代替ConvertTo-CSV

答案 1 :(得分:0)

$ StartUp = Get-WmiObject Win32_StartupCommand |选择标题|字符串

$ Startup.gettype()#这将为您提供变量类型。

相关问题