Authored by Gunjan Jain
PowerShell ps = PowerShell.Create();
var script = GetScriptContents();
ps.AddScript(script);
To execute the script, call the Invoke method on the PowerShell session
ps.Invoke();
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Management.Automation;
namespace PS.Example
{
/// <summary>
/// Summary description for SDCard_Test_Win32
/// </summary>
[CodedUITest]
public class Example
{
PowerShell ps;
public Example()
{
ps = PowerShell.Create();
}
[TestInitialize]
public void Initialize()
{
var script = GetScriptContents();
ps.AddScript(script);
}
[TestMethod]
public void TestPowershell()
{
var objects = ps.Invoke();
if (objects.Count <= 0)
{
Assert.Fail("The Test has Failed.");
}
}
private string GetScriptContents()
{
var location = System.Reflection.Assembly.GetCallingAssembly().Location;
var scriptLocation = Path.GetDirectoryName(location) + "\\PowerShell_Test.ps1";
var script = File.ReadAllText(scriptLocation);
return script;
}
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
private TestContext testContextInstance;
}
}
PowerShell Script Used:
Get-WmiObject -query "Select * from win32_logicaldisk where DriveType=2"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.