troubleshooting Question

write line in csv file if connection failed

Avatar of bibi92
bibi92Flag for France asked on
Powershell
1 Comment1 Solution12 ViewsLast Modified:
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

How to resolve ?

Thank you

Best regards
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros