아래 코드를 사용하여 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
출력을 표시하려는 _how_을 지정하지 않았으므로 질문의 두 번째 부분에만 답했습니다. – Clijsters