0

아래 코드를 사용하여 SQLServer에있는 테이블의 값을 선택하면 코드가 성공적으로 실행되고 변수에 할당 된 PowerShell 의 명령 프롬프트에 출력이 표시되지만 $MsgBody에 할당 된 출력을 추가하려고하면 로그 파일에 System.Data.DataRow을 로그 파일에 복사합니다.PowerShell을 사용하여 Select Query의 출력을 DataSet에서 로그 파일로 복사하는 방법은 무엇입니까?

출력을 로그 파일에 추가하면 어떤 도움을 얻을 수 있습니까?

$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 
+0

출력을 표시하려는 _how_을 지정하지 않았으므로 질문의 두 번째 부분에만 답했습니다. – Clijsters

답변

0

일했다.

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

비록 이것을 사용해 보았지만 출력을 파일로 복사하지만 다른 이전 출력은 로그 파일에서을 삭제합니다. Get-Help about_redirection을 살펴보면

$MsgBody > $logFilePath 

알려줍니다

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. 

[...]

단지를 두 번 이상으로 > 연산자를 대체하여, 즉, >> 접근 방식이 효과가 있습니다.

+0

@ Clijsters, 출력을 가로로 복사하고 있는데 어떻게 테이블 형식이나 이와 비슷한 정렬 된 순서로 복사 할 수 있습니까? 또한 Add-Content를 사용하여 로그 파일에 복사 할 수 없습니까? – SRP

+0

@ Clijsters, 내가 이것을 사용한다면> 나는 원하는 방식으로 출력을 복사하지만 다른 출력을 덮어 씁니다. 그러나 >> 이것을 사용하면 출력을 가로로 복사하기 때문에 더러워 보이고보기가 흉하게 보입니다. 출력을 세로 형식과 같은 올바른 형식으로 복사하면 쉽게 읽을 수 있습니다. >> – SRP

+1

이미 주석을 달았으므로 실제로 보이는 모양과 원하는 모양에 몇 가지 예제 출력을 추가하십시오 보기. – Clijsters

0

는 다음과 같은 매우 간단한 수정을 시도 할 수 있습니다 :

$MsgBody | Add-Content $logFilePath 

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

에 I가 당신을 위해 무엇을 찾고있는 것 같아요.

+0

아니, 나는 $ MsgBody | Add-Content $ logFilePath,이 파일을 System.Data.DataRow 로그 파일에 복사하고이 $ MsgBody | Out-File -Path $ logFilePath -Append - 출력을 복사하지만 로그 파일의 다른 출력을 겹쳐 씁니다. – SRP

+0

먼저 형식을 지정해 보셨습니까? ---> $ MsgBody | 포맷 - 테이블 - 자동 축소 | Out-File -Path $ logFilePath -Append –

+0

아니요,이 방법을 시도하지 않았으므로 시도해 본 결과를 확인합니다. – SRP

0

시도해 볼 수 있습니다.

$ DataSet.Tables |

abc.txt 형식 목록은 >> 그것은 조금 긴하지만 나를 위해 잘 작동하지만 내가 발견 한 위의 질문에 대한 대답이 나를

+0

@ jaismeensandhu, 심지어 이것은 내게 원하는 출력을주지 못한다. 또한 질문에서 물어 본 것과 마찬가지로 로그 파일에 출력을 가로로 복사한다. 지금은 출력을 세로 형식으로 로그 파일에 복사하는 데 오랜 시간이 걸리는 코드를 사용하고 있습니다. – SRP