Tag: powershell

将powershell变量值返回给c#应用程序

我从c#运行powershell脚本。 string scriptPath = “/script/myscript.ps1”; Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript(scriptPath); Collection results = pipeline.Invoke(); 例如,如果我的myscript.ps1文件在下面; $test=4 $test++ $test 如何在执行脚本后获取变量test值。 我需要为我的c#程序获得这个价值。

使用C#生成Powershell脚本时转义字符

我使用VS2010,C#,.NET 3.5生成Powershell脚本(ps1文件)。 然后,它是Powershell的必需转义字符。 有关它的任何建议,以开发逃避字符的好方法吗? public static partial class StringExtensions { /* PowerShell Special Escape Sequences Escape Sequence Special Character `n New line `r Carriage Return `t Tab `a Alert `b Backspace `” Double Quote `’ Single Quote “ Back Quote `0 Null */ public static string FormatStringValueForPS(this string value) { if (value == null) return […]

用C#读取Powershell进度条输出

我有一个程序从事件处理程序调用powershell脚本。 powershell脚本由第三方提供,我对它没有任何控制权。 powershell脚本使用powershell进度条。 我需要阅读powershell脚本的进度,但是由于进度条,System.Management.Automation命名空间不会将其视为输出。 是否可以从外部程序中读取powershell进度条的值? Process process = new Process(); process.StartInfo.FileName = “powershell.exe”; process.StartInfo.Arguments = String.Format(“-noexit -file \”{0}\””, scriptFilePath); process.Start();

Raspberry Pi CPU Temp

我有一个运行Windows 10 IoT核心的Raspberry Pi 3。 我的问题是; 是否有可能,或者有没有办法通过远程PowerShell连接监控设备的CPU温度,在命令行控制台的设备上本地监控设备的CPU温度,或者是否所有其他设备都使用设备上的C#部署应用程序失败? 我承认自己是C#noob,因此这是我的第三选择。 为什么?…我打算用Lego为一堆多个设备制作一个定制shell,并想评估我是否需要购买一堆散热器和散热风扇等。 非常感谢您的时间和帮助。

将powershell变量传递给PS脚本中的C#代码

我阅读了很多关于将vars从C#传递到powershell脚本的内容,但我却反过来说。 这里我有这个代码在我的powershell脚本中创建一个Type: Add-Type @’ public class Node { public string Type; public string VM_Name; public string VM_IP; public string Hostname; } ‘@ $vm1 = New-Object Node $vm2 = New-Object Node $vm3 = New-Object Node $vm4 = New-Object Node 在这段代码之后我有C#代码: $sourceCode = @’ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private […]

通过C#交换PowerShell命令

我正在使用C#发送与Exchange交互的PowerShell命令。 我有一个名为initconnection的方法,它设置我与Exchange的连接。 当我单击一个按钮时,我会调用另一种方法,该按钮将在建立连接后向powershell发送命令。 但是我无法继续创建的连接。 当我尝试运行命令时,它说找不到命令。 很可能是因为它没有交换cmdlet。 Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); runspace.Open(); Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript(“Set-ExecutionPolicy Unrestricted -Scope process -Force;$password = ConvertTo-SecureString -AsPlainText -Force ” + password + “;$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist ” + username + “,$password;$LiveCred = Get-Credential -Credential $mycred; $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic –AllowRedirection; […]

Powershell命令通过C#代码

我想通过C#代码添加Powershell命令或脚本(什么是正确的?)变量声明,默认值存储在C#变量中。 例如,在Powershell中我输入以下行 $user = ‘Admin’ 我想在C#代码中添加这一行。 powershell.AddScript(String.Format(“$user = \”{0}\””, userName)); 要么 powershell.AddCommand(String.Format(“$user = \”{0}\””, userName)); 我尝试使用AddCommand()但它抛出exception。 我用的是PS 2.0。

加载System.Management.Automation程序集时出错

我正在开发一个运行PowerShell脚本的WPF应用程序。 string scriptPath = “/script/myscript.ps1”; Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript(scriptPath); Collection results = pipeline.Invoke(); 我添加了以下程序集。 C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll 但是当程序运行后出现exception; 无法加载文件或程序集’System.Management.Automation,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35’或其依赖项之一。 强名称validation失败。 (来自HRESULT的exception:0x8013141A)“:”System.Management.Automation,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35“} ** 当我搜索这个错误时,我可以找到一些答案。 但那些对我来说并不清楚。 请指教。

创建一个显示更新通知的NuGet包

我正在创建一个NuGet包,我希望每当包的更新存在于存储库(这是一个私有存储库,而不是官方的NuGet存储库)时,包都会显示通知。 请注意,我不希望程序包自动更新(如果新版本可能会引入一些问题),而只是通知用户。 为此,我在包中的init.ps1文件中添加了这个: param($installPath, $toolsPath, $package, $project) $PackageName = “MyPackage” $update = Get-Package -Updates | Where-Object { $_.Id -eq $PackageName } if ($update -ne $null -and $update.Version -gt $package.Version) { [System.Windows.Forms.MessageBox]::Show(“New version $($update.Version) available for $($PackageName)”) | Out-Null } 需要检查$update.Version -gt $package.Version以避免在安装较新的软件包时显示通知。 我想知道是否 这个解决方案是可以接受的,或者如果有更好的“标准”方法(而不是酿造我自己的解决方案)。 有一种更好的方式来显示通知,因为MessageBox相当烦人:当我打开项目时,它隐藏在“准备解决方案”对话框后面,直到我点击确定才完成操作。

在C#中连接到Microsoft Exchange PowerShell

我正在尝试从C#.NET WinForms应用程序连接到远程PowerShell。 我的目标是创建自己的Microsoft PowerShell ISE版本。 所以我需要一种从远程计算机上的应用程序执行PowerShell脚本的方法。 我已经创建了几种方法,并在我的应用程序中在本地计算机上进行了测试。 如果我不使用WSManConnectionInfo并使用(Runspace remoteRunspace = RunspaceFactory.CreateRunspace())我可以在本地执行脚本,就好像它是真的powershell(小脚本,变量的使用,输出数据使用ft , fl ,做了很多我通常使用powershell做的其他事情。当我添加WSManConnectionInfo并将其指向我的Exchange Server而不是使用本地连接时,问题就出现了。它似乎能够执行像“get-mailbox”这样的基本内容但是只要我尝试管道东西,使用一些脚本function,如$ variables,它打破说它不受支持。 同样,我必须禁用powershell.AddCommand(“out-string”); 什么时候不在本地使用它。 System.Management.Automation.dll中发生了未处理的“System.Management.Automation.RemoteException”类型exception。 附加信息:术语“Out-String”不被识别为cmdlet,函数,脚本文件或可操作程序的名称。 检查名称的拼写,或者如果包含路径,请validation路径是否正确,然后重试。 如果我不强制远程连接但只是在本地执行,则不会出现相同的错误。 似乎SchemaUri只对执行基本命令非常严格。 我看到了其他使用非常直接信息的人的例子: powershell.AddCommand(“Get-Users”); powershell.AddParameter(“ResultSize”, count); 但是通过这种方法,我必须定义许多可能的选项,我不想通过定义参数和其他东西。 我只想加载“脚本”并像在PowerShell窗口中一样执行它。这是我现在使用的一个例子。 public static WSManConnectionInfo PowerShellConnectionInformation(string serverUrl, PSCredential psCredentials) { var connectionInfo = new WSManConnectionInfo(new Uri(serverUrl), “http://schemas.microsoft.com/powershell/Microsoft.Exchange”, psCredentials); //var connectionInfo = new WSManConnectionInfo(new Uri(serverUrl), “http://schemas.microsoft.com/powershell”, psCredentials); connectionInfo.AuthenticationMechanism = […]