0

I am trying to run the following command inside a PowerShell script:

$settingsFile = dotnet user-secrets list -p "..\$project"

where $project is a string variable describing a .csproj-file.

When running the above command fails (as not every project will have user secrets) the following gets written to the output window: Could not find the global property 'UserSecretsId' in MSBuild project '<PATH_TO_CSPROJ>.csproj'. Ensure this property is set in the project or use the '--id' command line option.

I want to suppress this output, while still being able to set the $settingsFile variable. How do I do this? I have tried the following modifications, which all did not work for me:

Try-catch

try { $settingsFile = dotnet user-secrets list -p "..\$project"} catch{ }

This was taken from this SO answer. However, this did not do anything.

Redirect error stream with Out-Null

$settingsFile = dotnet user-secrets list -p "..\$project" | Out-Null

This was taken from this SO answer. However, this also did not change anything to the output.

Redirect error stream with Out-Null and 2>&1

$settingsFile = dotnet user-secrets list -p "..\$project" 2>&1 | Out-Null

This did prevent the command from writing to console, but also caused the variable $settingsFile not to be set.


So, does anyone have any other ideas as how I might be able to achieve this goal? Let me know if there are any further questions regarding this issue.

Rich_Rich
  • 427
  • 3
  • 15
  • 1
    `$settingsFile = dotnet user-secrets list -p "..\$project" 2>$null` <- you want to redirect _only_ the error stream to `$null` – Mathias R. Jessen Oct 18 '22 at 11:31
  • Remove the "-p". It is not a parameter. See : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.secretmanagement/get-secret?force_isolation=true&view=ps-modules – jdweng Oct 18 '22 at 11:33
  • @jdweng, I am using `user-secrets` from the ASP.NET-Core package. I do understand the confusion however. – Rich_Rich Oct 18 '22 at 11:38

1 Answers1

1

Redirect the error stream to $null:

$settingsFile = dotnet user-secrets list -p "..\$project" 2>$null
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206