bibi92
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 :
"","","","","","","","","",""
Thank you
Best regards
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
How to resolve ?Thank you
Best regards
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.