Categories
PowerShell

Setting Environment Variables in Developer PowerShell in Visual Studio

While writing UI automated test scenarios using Gherkin SpecFlow framework and C# Steps, I needed to set an environment variable to run a single test case from the command line. In the Windows Command Prompt, I would set the environment variable like this:

Set ENV=envName

For example:

Set ENV=autotests-rc

Then, I would run the test scenario using:

dotnet test --filter FullyQualifiedName=project_api_testing.Features.Admin.Structure.VerifyAdminPayItemsFunctionalityFeature.VerifyUserCanGetPayItemOptions

However, setting the environment variable like this doesn’t work in the Developer PowerShell in Visual Studio. Instead, you need to use the following syntax:

$env:ENV = "autotest-rc"

To run all the test cases from the Developer PowerShell in Visual Studio, I use the following command:

To run the all the test case from the developer power shell in visual studio.

dotnet test ; py .\SanitizeJsonReport.py ; livingdoc test-assembly ./bin/Debug/net6.0/project_api_testing.dll -t ./TestExecution.json

Happy Coding 🙂

Categories
PowerShell

Code sign using PowerShell

Code signing is important before releasing our software to the client. In the previous post, we use a PowerShell script to publish our code. After publishing the code, we need to sign the code before release. We must have code cert for signing. Install the code sign in your machine. Certificate installs process is simple using certificate import wizard.

Windows Certificate installation wizard.

Once the certificate is install, we can procced to code signing process.

cd src
$paths = Get-ChildItem -include *.csproj -Recurse
foreach($pathobject in $paths)
{
     cd $pathobject.directory.fullName
     dotnet publish -o ..\..\__DEPLOY__\Program
}
cd ..\..

This part of the code from previous post this script publish all the project in the solution.

$cert = Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert

Get-ChildItem -recurse -path '__DEPLOY__\' -Include *.exe, *.dll | ForEach-Object {	
	$signingParameters = @{
		FilePath      =  $_.FullName
		Certificate   = $cert
		HashAlgorithm = 'SHA256'
		TimestampServer = 'http://timestamp.digicert.com'
	}
	Set-AuthenticodeSignature @signingParameters
	
}

The first line gets the signing cert path.

Next line, navigate to the code that needed to sign, in this example path is ‘__DEPLOY__’, sign will only the files with exe and dll extension. Subsequent lines shows different params for signing. You can find more about the code signing params from PowerShell documentation.

If you want to exclude any folder and files from signing, you can define like this

[string[]]$Exclude = @('ExcudeFolderName')

Get-ChildItem -recurse -path '__DEPLOY__\Program\' -Include *.exe, *.dll | Where-Object  { $_.DirectoryName -notmatch $Exclude } | ForEach-Object
cd src
$paths = Get-ChildItem -include *.csproj -Recurse
foreach($pathobject in $paths)
{
     cd $pathobject.directory.fullName
     dotnet publish -o ..\..\__DEPLOY__\Program
}
cd ..\..

[string[]]$Exclude = @('ExcudeFolderName')

$cert = Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert

Get-ChildItem -recurse -path '__DEPLOY__\Program\' -Include *.exe, *.dll | Where-Object  { $_.DirectoryName -notmatch $Exclude } | ForEach-Object {	
	$signingParameters = @{
		FilePath      =  $_.FullName
		Certificate   = $cert
		HashAlgorithm = 'SHA256'
		TimestampServer = 'http://timestamp.digicert.com'
	}
	Set-AuthenticodeSignature @signingParameters
	
}

Above snippet is the entire script.

Happy coding 🙂

Categories
PowerShell

.Net Publish Using PowerShell

Sometimes we want to publish project using PowerShell rather then using Visual Studio Publish feature. I wanted to published all projects of the solutions in one folder. Below PowerShell command will published our projects in __DEPLOY__ folder as an example.

cd src
$paths = Get-ChildItem -include *.csproj -Recurse
foreach($pathobject in $paths)
{
     cd $pathobject.directory.fullName
     dotnet publish -o ..\..\__DEPLOY__\Program
}
cd ..\..

Save this code as PowerShell script with extension of ps1. Run this script from VS, we need to open VS in elevated permission, open “Developer PowerShell” navigate to the script folder and run the script.
You may face an error with error message
“FullyQualifiedErrorId : UnauthorizedAccess”

To resolve this error run this command to by pass security exception

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

Run the PowerShell Script again, it should run it without any issue. And all you published files and folder should be in “__DEPLOY__” folder.

Happy Hacking 🙂