如何使用 PowerShell 将 Select 查询的输出从数据集复制到日志文件?

How to copy output of a Select Query from a DataSet to a log file using PowerShell?

我正在使用下面的代码 select SQLServer 中存在的 table 的值,代码成功执行并在 PowerShell 的命令提示符上显示输出 我已将其分配给一个变量,但是当我尝试将分配给 $MsgBody 的输出添加到日志文件时,它会将 System.Data.DataRow 复制到日志文件。

如何将输出添加到日志文件中,我们将不胜感激。

$scriptPath = $PSScriptRoot
$logFilePath = Join-Path $scriptPath "DemoResults.log"

# If log file exists, then clear its contents 
if (Test-Path $logFilePath) {
    Clear-Content -Path $logFilePath
} 

# It displays the date and time of execution of powershell script in log file.
$log = "Date Of Testing: {0} " -f (Get-Date)
$logString = "Process Started." 
Add-Content -Path $logFilePath -Value $log -Force 
Add-Content -Path $logFilePath -Value $logString -Force
$SQLServer = "AP-PON-SRSTEP\MSSQLSERVER12" #use Server\Instance for named SQL instances! 
$SQLDBName = "SystemDB"

$SqlQuery2 = "Select * from SystemDB.dbo.Version_Solution WHERE Notes ='9.2.7'"
$sa = "srp"
$pass = "Stayout"

$connectionString = "Data Source=$SQLServer;Initial Catalog=$SQLDBName;User ID=$sa;Password=$pass";

$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString);
$command = New-Object System.Data.SqlClient.SqlCommand($SqlQuery2, $connection);
$connection.Open();
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$MsgBody = $DataSet.Tables[0] 
#Displays output in Command shell
$MsgBody

$MsgBody | Add-Content $logFilePath
Get-Content $logFilePath

$connection.Close();

尽管我尝试使用它并将输出复制到文件,但从日志文件中删除了其他先前的输出。

$MsgBody > $logFilePath 

编辑部分:- 使用这个之后>>

$MsgBody >> $logFilePath 

它将输出水平复制到这种格式的日志文件中,

S o l u t i o n             :   i n t e l l   C o m p o n e n t           :   S y s t e m D B  M a j o r                   :   9  M i n o r                   :  2 S e r v i c e P a c k       :   1  H o t f i x                 :   0  I n s t a l l e d D a t e   :   1 2 / 1 2 / 2 0 1 7   6 : 5 7 : 4 8   P M  N o t e s                   :   9 . 2 . 1  R o l l U p R e l e a s e   :   0

看起来很难看,我想这样垂直复制-

Solution      : intell
Component     : SystemDB
Major         : 9
Minor         : 2
ServicePack   : 1
Hotfix        : 0
InstalledDate : 12/12/2017 6:57:48 PM
Notes         : 9.2.1
RollUpRelease : 0

Though I tried using this and it copies output to the file but deletes the other previous output from the log file.

$MsgBody > $logFilePath 

看看 Get-Help about_redirection 告诉我们:

Operator  Description               Example
--------  ----------------------    ------------------------------
>         Sends output to the       Get-Process > Process.txt
          specified file.

>>        Appends the output to     dir *.ps1 >> Scripts.txt
          the contents of the
          specified file.

[...]

这意味着,只需将您的 > 运算符替换为大于 >> 的两倍,您的方法就应该有效。

您可以尝试以下非常简单的修改:

$MsgBody | Add-Content $logFilePath

$MsgBody | Out-File -Path $logFilePath -Append -Force

我想这就是您要找的。

你可以试试

$DataSet.Tables[0] |格式列表 >> abc.txt

对我有用

我找到的上述问题的答案是这个,虽然有点冗长,但对我来说很好用。

$scriptPath = $PSScriptRoot
$logFilePath = Join-Path $scriptPath "DemoResults.log"

# If log file exists, then clear its contents 
if (Test-Path $logFilePath) {
    Clear-Content -Path $logFilePath
} 

# It displays the date and time of execution of powershell script in log file.
$log = "Date Of Testing: {0} " -f (Get-Date)
$logString = "Process Started." 
Add-Content -Path $logFilePath -Value $log -Force 
Add-Content -Path $logFilePath -Value $logString -Force
$SQLServer = "AP-PON-SRSTEP\MSSQLSERVER12" #use Server\Instance for named SQL instances! 
$SQLDBName = "SystemDB"

$SqlQuery2 = "Select * from SystemDB.dbo.Version_Solution WHERE Notes ='9.2.1'"
$sa = "srp"
$pass = "Stayout"

$connectionString = "Data Source=$SQLServer;Initial Catalog=$SQLDBName;User ID=$sa;Password=$pass";

$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString);
$command = New-Object System.Data.SqlClient.SqlCommand($SqlQuery2, $connection);
$connection.Open();
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$MsgBody = $DataSet.Tables[0] 
#Displays output in Command shell
$MsgBody

#Instead of this code, I have used the below code to copy output in Vertical Format in log file. 
<#
$MsgBody | Add-Content $logFilePath
Get-Content $logFilePath
#>

#Code to copy the output of select statement to log file.
    $logString="Version_Solution Table in SystemDB"
    add-content -Path $logFilePath -Value $logString -Force
    add-content -Path $logFilePath -Value "`n" -Force

    $MsgBody = $MsgBody | Select-Object Solution, Component, Major, Minor ,ServicePack,Hotfix,InstalledDate,Notes,RollUpRelease
    $Solution=$MsgBody.Solution
    $Component=$MsgBody.Component
    $Major=$MsgBody.Major
    $Minor=$MsgBody.Minor
    $ServicePack=$MsgBody.ServicePack
    $Hotfix=$MsgBody.Hotfix
    $InstalledDate=$MsgBody.InstalledDate
    $Notes=$MsgBody.Notes
    $RollUpRelease=$MsgBody.RollUpRelease

    add-content -Path $LogFilePath -Value "Solution: $Solution" -Force   
    add-content -Path $LogFilePath -Value "Component: $Component" -Force  
    add-content -Path $LogFilePath -Value "Major: $Major" -Force  
    add-content -Path $LogFilePath -Value "Minor: $Minor" -Force  
    add-content -Path $LogFilePath -Value "ServicePack: $ServicePack" -Force  
    add-content -Path $LogFilePath -Value "Hotfix: $Hotfix" -Force  
    add-content -Path $LogFilePath -Value "InstalledDate: $InstalledDate" -Force  
    add-content -Path $LogFilePath -Value "Notes: $Notes" -Force  
    add-content -Path $LogFilePath -Value "RollUpRelease: $RollUpRelease" -Force  
    add-content -Path $logFilePath -Value "`n" -Force

$connection.Close();