Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell: Assigning a variable, while surpressing console output of dotnet user-secrets

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

Redirect the error stream to $null:

$settingsFile = dotnet user-secrets list -p "..\$project" 2>$null
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading