Skip to content

Commit

Permalink
fix bugs in how I called pwsh.AddCommand()
Browse files Browse the repository at this point in the history
  • Loading branch information
anamnavi committed Aug 29, 2023
1 parent 9050054 commit 702fda6
Showing 1 changed file with 30 additions and 65 deletions.
95 changes: 30 additions & 65 deletions src/code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,12 @@ public static PSCredential GetRepositoryCredentialFromSecretManagement(
try
{
System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create();
var module = pwsh.AddCommand("Import-Module -Name Microsoft.PowerShell.SecretManagement -PassThru").Invoke<PSModuleInfo>();
var module = pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module").AddParameters(
new Hashtable() {
{ "Name", "Microsoft.PowerShell.SecretManagement"},
{ "PassThru", true}
}).Invoke<PSModuleInfo>();

if (module == null)
{
cmdletPassedIn.ThrowTerminatingError(
Expand Down Expand Up @@ -612,7 +617,12 @@ public static void SaveRepositoryCredentialToSecretManagementVault(
try
{
System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create();
var module = pwsh.AddCommand("Import-Module -Name Microsoft.PowerShell.SecretManagement -PassThru").Invoke<PSModuleInfo>();
var module = pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module").AddParameters(
new Hashtable() {
{ "Name", "Microsoft.PowerShell.SecretManagement"},
{ "PassThru", true}
}).Invoke<PSModuleInfo>();

if (module == null)
{
cmdletPassedIn.ThrowTerminatingError(
Expand Down Expand Up @@ -655,11 +665,21 @@ public static bool IsSecretManagementModuleAvailable(
try
{
System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create();
var module = pwsh.AddCommand("Get-Module -Name Microsoft.PowerShell.SecretManagement -ErrorAction Ignore").Invoke<PSModuleInfo>();
var module = pwsh.AddCommand("Microsoft.PowerShell.Core\\Get-Module").AddParameters(
new Hashtable() {
{ "Name", "Microsoft.PowerShell.SecretManagement"},
{ "ErrorAction", "Ignore"}
}).Invoke<PSModuleInfo>();

if (module == null)
{
pwsh.Commands.Clear();
module = pwsh.AddCommand("Import-Module -Name Microsoft.PowerShell.SecretManagement -PassThru -ErrorAction Ignore").Invoke<PSModuleInfo>();
module = pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module").AddParameters(
new Hashtable() {
{ "Name", "Microsoft.PowerShell.SecretManagement"},
{ "PassThru", true},
{ "ErrorAction", "Ignore"}
}).Invoke<PSModuleInfo>();
}

if (module == null)
Expand All @@ -680,36 +700,6 @@ public static bool IsSecretManagementModuleAvailable(
}

return true;

// var results = PowerShellInvoker.InvokeScriptWithHosst<int>(
// cmdlet: cmdletPassedIn,
// script: @"
// $module = Microsoft.PowerShell.Core\Get-Module -Name Microsoft.PowerShell.SecretManagement -ErrorAction Ignore
// if ($null -eq $module) {
// $module = Microsoft.PowerShell.Core\Import-Module -Name Microsoft.PowerShell.SecretManagement -PassThru -ErrorAction Ignore
// }
// if ($null -eq $module) {
// return 1
// }
// return 0
// ",
// args: new object[] {},
// out Exception terminatingError);

// if (terminatingError != null)
// {
// cmdletPassedIn.ThrowTerminatingError(
// new ErrorRecord(
// new PSInvalidOperationException(
// message: $"Cannot validate Microsoft.PowerShell.SecretManagement module setup for PSResourceRepository ({repositoryName}) authentication.",
// innerException: terminatingError),
// "RepositoryCredentialSecretManagementInvalidModule",
// ErrorCategory.InvalidOperation,
// cmdletPassedIn));
// }

// int result = (results.Count > 0) ? results[0] : 1;
// return result == 0;
}

public static bool IsSecretManagementVaultAccessible(
Expand All @@ -720,7 +710,12 @@ public static bool IsSecretManagementVaultAccessible(
try
{
System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create();
var module = pwsh.AddCommand("Import-Module -Name Microsoft.PowerShell.SecretManagement -PassThru").Invoke<PSModuleInfo>();
var module = pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module").AddParameters(
new Hashtable() {
{ "Name", "Microsoft.PowerShell.SecretManagement"},
{ "PassThru", true}
}).Invoke<PSModuleInfo>();

if (module == null)
{
return false; // TODO: i don't understand what original code was doing here.
Expand All @@ -747,36 +742,6 @@ public static bool IsSecretManagementVaultAccessible(

return false;
}

// var results = PowerShellInvoker.InvokeScriptWithHosst<bool>(
// cmdlet: cmdletPassedIn,
// script: @"
// param (
// [string] $VaultName
// )
// $module = Microsoft.PowerShell.Core\Import-Module -Name Microsoft.PowerShell.SecretManagement -PassThru
// if ($null -eq $module) {
// return
// }
// & $module ""Test-SecretVault"" -Name $VaultName
// ",
// args: new object[] { repositoryCredentialInfo.VaultName },
// out Exception terminatingError);

// if (terminatingError != null)
// {
// cmdletPassedIn.ThrowTerminatingError(
// new ErrorRecord(
// new PSInvalidOperationException(
// message: $"Microsoft.PowerShell.SecretManagement\\Test-SecretVault encountered an error while validating the vault \"{repositoryCredentialInfo.VaultName}\" for PSResourceRepository ({repositoryName}) authentication.",
// innerException: terminatingError),
// "RepositoryCredentialSecretManagementInvalidVault",
// ErrorCategory.InvalidOperation,
// cmdletPassedIn));
// }

// bool result = (results.Count > 0) ? results[0] : false;
// return result;
}

public static NetworkCredential SetNetworkCredential(
Expand Down

0 comments on commit 702fda6

Please sign in to comment.