代码之家  ›  专栏  ›  技术社区  ›  SRP

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

  •  0
  • SRP  · 技术社区  · 7 年前

    我使用下面的代码从SQLServer中的表中选择一个值,代码成功执行并在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
    
    4 回复  |  直到 7 年前
        1
  •  1
  •   Clijsters    7 年前

    虽然我尝试使用它,但它会将输出复制到文件中 但删除之前的其他输出 从日志文件。

    $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.
    

    [...]

    也就是说,只要更换 > 双精度大于 >> 你的方法应该奏效。

        2
  •  0
  •   Jesus A. Sanchez    7 年前

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

    $MsgBody | Add-Content $logFilePath
    

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

    我想这就是你要找的。

        3
  •  0
  •   jaismeensandhu    7 年前

    你可以试试

    $数据集。表[0]|格式列表>&燃气轮机;美国广播公司。txt文件

    这对我有用

        4
  •  0
  •   SRP    7 年前

    我发现,上述问题的答案是,虽然有点冗长,但对我来说效果很好。

    $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();