Link to home
Start Free TrialLog in
Avatar of bibi92
bibi92Flag for France

asked on

write line in csv file if connection failed

Hello,
If connection to a server is not possible I try to write the line in try but the result is :
"","","","","","","","","",""

$InvProd = @"
SET NOCOUNT ON
SELECT [HOSTNAME]  
FROM [Inventory]
"@
$Serv = sqlcmd -S SRVDBA -d DataRepo  -Q $InvProd -h -1 -W
$RFile = "E:\extract\extract.csv"
$CFile = "E:\extract\chk.csv"
$Date = get-date -format "yyyy-MM-dd HH:mm:ss"
$Rpt = @()
$Chck = @()
Foreach($I in $Serv) {
 Try {
 $Rpt + = Invoke-Sqlcmd -ServerInstance $I -inputfile $sql  -ErrorAction 'continue'
 $Chck += $I
 }
 Catch {
  $Rpt += 'fr' + ',' + "" + ',' + "$I.split(',')[0].split('')" + ',' + "" + ',' + "" + ',' + "" + ','+ "" + ',' + "" + ','+ 'server not accessible' + ',' + "$Date"
 }
}


$Rpt  |
 ConvertTo-Csv -NoTypeInformation  |
 ForEach-Object { $_ -replace '"' } |
 Out-File $RFile -Encoding Unicode
$Chck |
 ConvertTo-Csv -NoTypeInformation  |
 ForEach-Object { $_ -replace '"' } |
 Out-File $CFile -Encoding Unicode

Open in new window

How to resolve ?

Thank you

Best regards
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial