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.
CodePudding user response:
Redirect the error stream to $null
:
$settingsFile = dotnet user-secrets list -p "..\$project" 2>$null