如何使用PowerShell将选择查询的输出从DataSet复制到日志文件?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用PowerShell将选择查询的输出从DataSet复制到日志文件?相关的知识,希望对你有一定的参考价值。
我使用下面的代码从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-SRSTEPMSSQLSERVER12" #use ServerInstance 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
虽然我尝试使用它并将输出复制到文件但删除了日志文件中的其他先前输出。
$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] | format-list >> 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-SRSTEPMSSQLSERVER12" #use ServerInstance 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();
以上是关于如何使用PowerShell将选择查询的输出从DataSet复制到日志文件?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 powershell 变量中捕获 PL SQL 函数的输出?
从任务计划程序运行时如何重定向 PowerShell 输出?
您将如何使用带有 FastAPI 的 asyncpg 将选择查询的返回值映射到 pydantic 模型以进行输出和验证?