You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell调用SQLPLUS返回PL/SQL多行结果被合并为单行如何解决

问题原因

sqlplus执行完成后返回的$sqlOutput为字符串数组,每一行输出对应数组的一个元素。你使用双引号包裹$($sqlOutput)时,PowerShell会默认使用空格作为分隔符将数组合并为单个字符串,原有换行被替换为空格,最终表现为多行内容被拼接成单行。

修复方案

修改PSObject的Query属性赋值逻辑,保留换行结构即可,两种可选写法:

  • 直接将$sqlOutput数组赋值给属性,不套双引号,PowerShell输出对象时会自动保留多行格式:
Query = $sqlOutput
  • 若需要提前合并为单个字符串,手动指定换行符拼接数组:
Query = $sqlOutput -join "`n"
修改后的核心代码片段
foreach ($proc in $process_id) {
    foreach ($id in $service) {
        if ($id.ProcessId -eq $proc.RawValue) {
            foreach ($time in $processor_time) {
                $path = $proc.Path.Replace("\id process","\% processor time")
                if ($time.Path -eq $path -and $time.CookedValue -gt 0) {
                    $cpu = [Math]::Round($time.CookedValue / $env:NUMBER_OF_PROCESSORS, 1)
                    #if cpu usage > 1%
                    if ($cpu -gt 1) {
                        $klant = $id.Name.Substring(13)
                        $sqlQuery = @"
set serveroutput on
set feedback off
set pagesize 100
set heading off
SELECT SQL_TEXT
  FROM SYS.V_`$SQL S, SYS.ALL_USERS U, V`$SESSION SN
 WHERE S.PARSING_USER_ID = U.USER_ID
   AND SN.sql_hash_value = S.hash_value
   AND SN.sql_address = S.address
 ORDER BY S.LAST_LOAD_TIME;
"@
                        $sqlOutput = $sqlQuery | sqlplus -silent "sys/d20K27hD@$($klant) as sysdba"
                        $item += New-Object -TypeName psobject -Property @{
                            Klant = $klant
                            'CPU %' = $cpu
                            Query = $sqlOutput # 如需单字符串则替换为 $sqlOutput -join "`n"
                        }
                        $item
                    }
                }
            }      
        }
    }
}

内容的提问来源于stack exchange,提问作者Tim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 00:51:00