0
我在嘗試使用PowerShell腳本以靜默方式安裝SQL Server 2008 R2。 安裝後,我需要驗證安裝是否成功。 有沒有什麼辦法可以檢查sqlsetup.log
文件中退出代碼是否成功安裝?如何驗證SQL Server 2008 R2的成功安裝
以下是代碼,我目前使用: -
# Read current directory
$ScriptPath = $MyInvocation.MyCommand.Path
$ParentDir = Split-Path $scriptpath
#Read Config file from current directory
$ConfigFile = $ParentDir + '\ConfigurationFile.ini'
Write-Host $ConfigFile
#Holds base setup from current directory
$SetupFile=$ParentDir + '\setup.exe'
Write-Host $SetupFile
$SP3SetupFile=$ParentDir + '\SQLServer2008R2_SP3\Setup.exe'
Write-Host $SP3SetupFile
#Start of the log file
$Start="========================================================================================"
$Start | Out-File $env:temp\InstallSQL2008R2_log.txt -append
#Printsd the current date and time in log file
$Date=Get-Date
$Date | Out-File $env:temp\InstallSQL2008R2_log.txt -append
#Starting with the SQL base installation
$InstallBaseSQL="Installing SQL Server 2008 R2"
$InstallBaseSQL | Out-File $env:temp\InstallSQL2008R2_log.txt -append
#Installs SQL Server 2008 R2 Standard Edition
& "$SetupFile" /ConfigurationFile="$ConfigFile"
#Checks whether properly installed
$ExistString = Select-String -Path "$env:temp\SqlSetup*.log" -Pattern "Setup closed with exit code: 0x00000000"
if($ExistString)
{
$SuccessStatus = "Installation of SQL Server 2008 R2 completed"
$SuccessStatus | Out-File $env:temp\InstallSQL2008R2_log.txt -append
#write-host "Installation of SQL Server 2008 R2 completed"
#out-file $env:temp\InstallSQL2008R2_log.txt -append
# $log.name | out-file $C:\FileContainingString.txt -append
}
else
{
$failStatus = "Installation couldn't complete, for more details please check sqlsetup.txt in temp folder"
$FailStatus | Out-File $env:temp\InstallSQL2008R2_log.txt -append
}
#Installing sql patch
$InstallSQLSP3="Installing SQL Server 2008 R2 SP3"
$InstallSQLSP3 | Out-File $env:temp\InstallSQL2008R2_log.txt -append
#Install SQL Server 2008 R2 SP3 Patch
& "$SP3SetupFile" /instancename=MSSQLSERVER /quiet
#Completion Message
$PatchComplete="Installation of SQL Server 2008 R2 SP3 completed3"
$PatchComplete | Out-File $env:temp\InstallSQL2008R2_log.txt -append
$End="========================================================================================"
$End | Out-File $env:temp\InstallSQL2008R2_log.txt -append
不能說我見過任何人保存每個輸出到一個新的變量,然後將其保存到一個文件。爲什麼不直接將字符串輸入到「Out-File」?無論如何,而不是檢查日誌文件,你可以檢查[這篇文章](http://stackoverflow.com/questions/3257328/detecting-if-sql-server-2008-is-installed?rq=1)的註冊表標誌,但錯誤代碼「0x00000000」(日誌的第二行)意味着沒有錯誤。 –