public static async Task PutFile(string path, bool forceSafety) { using (var client = new PSClient(PSConnectionInfo.CreateLocalConnection())) { client.FileSystem.ForceSafety = forceSafety; if (client.FileSystem.PathExists(path, true, false)) { await client.FileSystem.DeleteFileAsync(path); } await client.FileSystem.PutFileAsync(path, Encoding.UTF8.GetBytes("HELLO WORLD!"), true); Assert.IsTrue(await client.FileSystem.PathExistsAsync(path, true, false)); Assert.IsTrue(File.Exists(path)); var txt = await client.FileSystem.GetFileHashAsync(path, "MD5+Length"); Assert.AreEqual("B59BC37D6441D96785BDA7AB2AE98F75::12", txt); await client.FileSystem.DeleteFileAsync(path); Assert.IsFalse(await client.FileSystem.PathExistsAsync(path, true, false)); Assert.IsFalse(File.Exists(path)); } }
public async Task StringResult_Tests() { using (var client = new PSClient()) { var value = "Hello World -- " + Guid.NewGuid().ToString(); await client.OpenAsync(PSConnectionInfo.CreateLocalConnection()); var result = await client.InvokeScriptAsync<string>(PSUtils.EscapeString(value)); Assert.AreEqual(value, result.Single()); } }
public async Task ReadLine_Exception3_Tests() { using (var client = new PSClient()) { await client.OpenAsync(PSConnectionInfo.CreateLocalConnection()); await Task.Delay(TimeSpan.FromMilliseconds(100)); client.ConfigureNonInteractiveSilentHost(); await Assert.ThrowsExceptionAsync <CmdletInvocationException>(async() => await client.InvokeScriptAsync <string>("Read-Host")); } }
public async Task ReadLine_Test() { using (var client = new PSClient()) { await client.OpenAsync(PSConnectionInfo.CreateLocalConnection()); await Task.Delay(TimeSpan.FromMilliseconds(100)); client.ConfigureNonInteractiveConsoleHost(); client.HostUI.ReadLineCallback = () => "Hello World"; Assert.AreEqual("Hello World", (await client.InvokeScriptAsync <string>("Read-Host")).Single()); } }
public async Task ReadLine_Exception1_Tests() { using (var client = new PSClient()) { await client.OpenAsync(PSConnectionInfo.CreateLocalConnection()); await Task.Delay(TimeSpan.FromMilliseconds(100)); try { var result = await client.InvokeScriptAsync <string>("Read-Host"); Assert.Fail("Read-Host did not fail!"); } catch (Exception ex) { if (!(ex is InvalidOperationException) && !(ex is CmdletInvocationException)) { throw; } } } }
public async Task TestWriteHost2() { using (var client = new PSClient()) { await client.OpenAsync(PSConnectionInfo.CreateLocalConnection()); client.ConfigureNonInteractiveConsoleHost(); string v = string.Empty; var oldCallback = client.HostUI.WriteCallback; client.HostUI.WriteCallback = (s, fg, bg, c) => { if (s == PSOutputStream.Default) { v += c; } oldCallback?.Invoke(s, fg, bg, c); }; await client.InvokeScriptAsync("Write-Host \"Hello World\";"); Assert.AreEqual("Hello World", v.Trim()); Assert.IsTrue(v.EndsWith("\n")); } }
public async Task TestWriteHost() { using (var client = new PSClient()) { await client.OpenAsync(PSConnectionInfo.CreateLocalConnection()); string v = string.Empty; client.HostUI.WriteCallback = (s, fg, bg, c) => { if (s == PSOutputStream.Default) { v += c; } }; await client.InvokeScriptAsync("Write-Host \"Hello World\";"); Assert.AreEqual("Hello World", v.Trim()); Assert.IsTrue(v.EndsWith("\n")); // Also test multi-close await client.CloseAsync(); await client.CloseAsync(); await client.CloseAsync(); } }
public async Task TestConnection_Tests() { using (var client = new PSClient()) { Assert.ThrowsException<ArgumentNullException>(() => client.TestConnection(null)); await Assert.ThrowsExceptionAsync<ArgumentNullException>(async () => await client.TestConnectionAsync(null)); Assert.IsTrue(await client.TestConnectionAsync(PSConnectionInfo.CreateLocalConnection())); var rnd = new Random(Guid.NewGuid().GetHashCode()); var badRemoteCxnNfo = PSConnectionInfo.CreateRemoteConnection ( "localhost", "notarealusername", "notarealpassword".ToSecureString(), (ushort)rnd.Next(3000, 4000) ); badRemoteCxnNfo.ConnectionTimeout = TimeSpan.FromSeconds(1); badRemoteCxnNfo.OperationTimeout = TimeSpan.FromSeconds(1); Assert.IsFalse(await client.TestConnectionAsync(badRemoteCxnNfo)); } }