Tag: powershell

从.NET Core运行PowerShell

有没有办法从.net-core运行PowerShell脚本? 我正在尝试在新的.net核心’website \ api’中运行PowerShell脚本。 据我所知,为了在.net上运行PowerShell,我们需要添加 System.Management.Automation命名空间。 这对于.net核心是不可能的(或者我没有找到合适的方法来添加它)。 还有一些NuGet包也旨在将这个DLL添加到项目中,但这些包与.net核心也不兼容。 有没有办法在.net核心上执行此操作? 以下是我尝试过的一些链接,但没有一个是.net核心特定的: Writing a PowerShell module in C#, Part 1: The basics 在Visual Studio中引用system.management.automation.dll https://www.nuget.org/packages/System.Management.Automation/

如何检索程序集的限定类型名称?

如何生成程序集限定类型名称? 例如,在配置成员资格提供程序时,我必须在“type”属性中为“SqlMembershipProvider”(在此示例中,我已从某处复制以下配置)提供程序集限定类型名称。 如何生成该程序集限定类型名称? 是否必须通过检查assembly类型每次手动输入? [更新] : 更简单的PowerShell版本 PS>([System.String]).AssemblyQualifiedName System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

使用PowerShell调用Visual Studio方法

我正在尝试编写一个powershell脚本来调用位于我的Visual Studio(2010)解决方案中的方法。 调用的语法是什么,例如,类似的方法 public void CreateData(string s, int i) ?

在C#代码中运行PSCmdLets(Citrix XenDesktop)

我是PowerShell的新手,在C#中运行PowerShell cmd-lets。 具体来说,我正在尝试使用Citrix的XenDesktop SDK编写Web应用程序来管理我们的XenDesktop环境。 就像快速测试一样,我提到了Citrix BrokerSnapIn.dll,看起来它给了我很好的C#类。 但是,当我使用此错误消息命中.Invoke时:“无法直接调用从PSCmdlet派生的Cmdlet。” 我搜索并尝试了一堆东西,但不知道如何调用PSCmdlets。 我有点想到我必须使用字符串和运行空间/管道等来做到这一点。 谢谢高级,NB using System; using System.Management.Automation; using System.Management.Automation.Runspaces; using Citrix.Broker.Admin.SDK; namespace CitrixPowerShellSpike { class Program { static void Main(string[] args) { var c = new GetBrokerCatalogCommand {AdminAddress = “xendesktop.domain.com”}; var results = c.Invoke(); Console.WriteLine(“all done”); Console.ReadLine(); } } }

PowerShell,读写/写入SSH.NET流

我想通过SSH连接到服务器,使用PowerShell, 然后更改为其他用户 。 为了通过SSH实现与PowerShell的服务器连接,我使用SSH.NET库中的 SSH和如何通过SSH连接 PowerShell来使用 SSH 。 这很棒,并为我提供了简单的命令行开关,例如new-sshsession和invoke- sshcommand 。 接下来,我想su给不同的用户。 当我尝试通过命令行开关这样做时,我得到了错误: 标准必须是tty 。 我知道我可以编辑服务器上的安全设置,让我在没有处于交互模式的情况下使用su 。 但是,我无法在我想要连接的所有服务器上更改该设置。 使用我在https://sshnet.codeplex.com/discussions/285853上找到的C#示例,我把它放在一起: $server = “server1” $port = 22 $username = “user1” $password = “password1” $ssh = new-object Renci.SshNet.SshClient($server, $port, $username, $password) $ssh.Connect() $inputstream = new-object System.IO.MemoryStream $streamwriter = new-object System.IO.StreamWriter($inputstream) $outputstream = new-object System.IO.MemoryStream $streamreader = new-object System.IO.StreamReader($outputstream) […]

复制文件夹/文件没有修改属性?

是否可以将文件或文件夹从一个位置复制到另一个位置而无需修改其属性数据? 例如,如果我在网络驱动器上有一个文件夹,它是在2007年2月3日创建的,我想将其复制到我的c:驱动器..但是将日期/时间戳保留为2/3/2007 …那可能吗?

在C#错误中运行powershell命令,“无法调用此函数,因为当前主机未实现它”

我有一个应用程序,用于在C#代码中运行Exchange Powershell命令,如下所示。 这是我用来运行powershell命令的相关行的示例。 RunspaceConfiguration rsConfig = RunspaceConfiguration.Create(); PSSnapInException snapInException = null; //load Exchange shell rsConfig.AddPSSnapIn(“Microsoft.Exchange.Management.PowerShell.E2010”, out snapInException); Runspace runSpace = RunspaceFactory.CreateRunspace(rsConfig); //open runspace runSpace.Open(); //setup pipeline Pipeline pipeLine = runSpace.CreatePipeline(); String sScript = “get-mailbox -identity ‘rj'”; //add script to pipeline pipeLine.Commands.AddScript(sScript); //run the script pipeLine.Invoke(); pipeLine.Dispose(); 到目前为止,此代码在所有情况下都是完美的 我试图运行的脚本而不是上面的脚本是为邮箱设置RetentionPolicy。 我试图运行的脚本如下所示: Set-Mailbox -Identity’rj’-RetentionPolicy’主要校园保留政策’ 当我在powershell中运行它时,它运行得很好但是当我尝试使用下面的代码运行它时,我得到错误,“无法调用此函数,因为当前主机没有实现它。” 从这个错误来看,几乎看起来在C#中运行的命令无法运行RetentionPolicy命令,但这没有多大意义。 我用谷歌搜索了所有的建议,但没有运气。 如果有人知道为什么会这样,那将非常有帮助。

在PowerShell中,如何确定当前驱动器是否为联网驱动器?

我需要知道,在Powershell中,当前驱动器是否是映射驱动器。 不幸的是,Get-PSDrive没有像预期的那样工作: PS:24 H:\temp >get-psdrive h Name Provider Root CurrentLocation —- ——– —- ————— H FileSystem H:\ temp 但在MS-Dos中,“net use”表明H:实际上是一个映射的网络驱动器: New connections will be remembered. Status Local Remote Network ——————————————————————————- OK H: \\spma1fp1\JARAVJ$ Microsoft Windows Network The command completed successfully. 我想要做的是获取驱动器的根目录并在提示符中显示它(请参阅: 自定义PowerShell提示符 – 相当于CMD的$ M $ P $ _ $ + $ G? )

托管的PowerShell无法在同一个程序集中看到Cmdlet

我正在尝试从我的C#代码运行PowerShell脚本,它将使用运行它们的程序集中的自定义Cmdlet。 这是代码: using System; using System.Management.Automation; [Cmdlet(VerbsCommon.Get,”Hello”)] public class GetHelloCommand:Cmdlet { protected override void EndProcessing() { WriteObject(“Hello”,true); } } class MainClass { public static void Main(string[] args) { PowerShell powerShell=PowerShell.Create(); powerShell.AddCommand(“Get-Hello”); foreach(string str in powerShell.AddCommand(“Out-String”).Invoke()) Console.WriteLine(str); } } 当我尝试运行它时,我得到一个CommandNotFoundException。 我写错了Cmdlet吗? 有什么我需要做的事情来在PowerShell或Runspace中注册我的Cmdlet吗?

我如何使用ConvertTo-SecureString

假设我需要在Powershell中执行此操作: $SecurePass = Get-Content $CredPath | ConvertTo-SecureString -Key (1..16) [String]$CleartextPass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($CredPass)); $ CredPath的内容是一个包含ConvertFrom-SecureString -Key(1..16)输出的文件。 如何在C#/ .NET中完成ConvertTo-SecureString -key (1..16)部分? 我知道如何创建SecureString ,但我不确定应该如何处理加密。 我是否使用AES加密每个字符,或者解密字符串然后为每个字符创建一个安全字符串? 我对密码学几乎一无所知,但从我收集的内容中我可能只想用C#调用Powershell命令。 作为参考,我在这里发现了一篇关于AES加密/解密的类似post: 在C#中使用AES加密 UPDATE 我已经回顾了Keith发布的链接 ,但我还面临其他未知数。 DecryptStringFromBytes_Aes有三个参数: static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV) 第一个参数是字节数组表示加密文本。 这里的问题是,如何在字节数组中表示字符串? 它应该用或不用编码来表示? byte[] ciphertext = Encoding.ASCII.GetBytes(encrypted_text); byte[] ciphertext = Encoding.UTF8.GetBytes(encrypted_text); byte[] ciphertext = Encoding.Unicode.GetBytes(encrypted_text); byte[] ciphertext = […]