Skip to content

Commit 682df22

Browse files
author
Bob Pokorny
committed
Updating logging
1 parent 952c330 commit 682df22

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

IISU/PSHelper.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ private void InitializeRemoteSession()
273273
{
274274
_logger.LogInformation("Attempting to create a temporary key file");
275275
tempKeyFilePath = createPrivateKeyFile();
276-
_logger.LogTrace($"Temporary KeyFilePath created at: {tempKeyFilePath}");
277276
}
278277
catch (Exception ex)
279278
{
@@ -287,6 +286,10 @@ private void InitializeRemoteSession()
287286
{ "UserKnownHostsFile", "/dev/null" },
288287
};
289288

289+
_logger.LogTrace($"Temporary KeyFilePath created at: {tempKeyFilePath}");
290+
_logger.LogTrace($"HostName: {ClientMachineName}");
291+
_logger.LogTrace($"Username: {serverUserName}");
292+
290293
PS.AddCommand("New-PSSession")
291294
.AddParameter("HostName", ClientMachineName)
292295
.AddParameter("UserName", serverUserName)
@@ -348,7 +351,21 @@ private void InitializeRemoteSession()
348351
$"Could not establish a remote PowerShell session to '{machineName}:{port}' within {timeoutSeconds} seconds. " +
349352
"Verify WinRM is reachable and the firewall allows the connection.");
350353
}
351-
_PSSession = new Collection<PSObject>(PS.EndInvoke(asyncResult));
354+
355+
try
356+
{
357+
_PSSession = new Collection<PSObject>(PS.EndInvoke(asyncResult));
358+
}
359+
catch (Exception ex)
360+
{
361+
_logger.LogError(ex.ToString());
362+
if (ex.InnerException != null)
363+
{
364+
_logger.LogError($"Inner Exception: {ex.InnerException.Message}");
365+
}
366+
367+
throw;
368+
}
352369

353370
if (_PSSession.Count > 0)
354371
{
@@ -425,8 +442,9 @@ private void InitializeRemoteSession()
425442
$"Failed to create the remote PowerShell session to '{machineName}'. {errorSummary}");
426443
}
427444
}
428-
catch
445+
catch (Exception ex)
429446
{
447+
_logger.LogError($"An error occurred while initializing the remote session: {ex.Message}");
430448
CleanupTempKeyFile();
431449
throw;
432450
}
@@ -1045,7 +1063,7 @@ private string createPrivateKeyFile()
10451063

10461064
if (chmodProcess.ExitCode == 0)
10471065
{
1048-
_logger.LogInformation("SSH private key permissions set to 600.");
1066+
_logger.LogInformation($"SSH private key permissions set to 600 for file: {tmpFile}");
10491067
}
10501068
else
10511069
{

0 commit comments

Comments
 (0)