Tag: powershell

如何调试从C#执行的powershell脚本?

我通过在C#中使用System.Management.Automation命名空间运行PowerShell脚本的代码,类似于下面的代码。 using (mPowershell = PowerShell.Create()) { mPowershell.AddScript(GetScriptText(mStep), true); SetPowershellVariables(mPowershell); output = new PSDataCollection(); output.DataAdded += new EventHandler(Output_DataAdded); mPowershell.InvocationStateChanged += new EventHandler(Powershell_InvocationStateChanged); IAsyncResult asyncResult = mPowershell.BeginInvoke(null, output); } 正如所料,我遇到了错误,我想调试它们。 有没有办法,没有进入powershell脚本,每两行放一个Write-Host $somevariable ,一步一步调试这个脚本? 我应该提一下脚本本身不能独立运行,C#代码将变量添加到脚本的运行空间中。

从C#调用PowerShell脚本中的特定函数

有没有办法从C#代码中调用PowerShell脚本中的特定函数? 我已经启动并运行了一个基本示例,它使用PowerShell脚本执行字符串。 如果我有一个包含许多函数的文件,并且我只想将特定的一个加载到C#中并执行它,该怎么办? 如何才能做到这一点? 另外,有没有办法动态地将参数加载到此函数中,而不实际知道前面哪个是加载的函数? 谢谢!

PowerShell运行“dotnet run …”后没有返回命令行

我目前正在尝试构建一个PowerShell脚本,需要将我自己的C#应用​​程序作为服务启动,然后我需要检查事件日志中的错误或其他条目。 但是一旦dotnet运行任务完成,进程就不会让我运行另一个命令,它会等待启动进程停止。 这是PowerShell脚本的一个示例 CD C:\MyCSharpProject\Project\ dotnet run –debug notepad 如何启动我的服务然后再运行另一个命令?

post-build powershell脚本

我有以下post-build事件: powershell Set-ExecutionPolicy Unrestricted powershell -file “$(SolutionDir)Obfuscation\_obfuscate.ps1” “$(SolutionDir)” “$(ProjectDir)” powershell Set-ExecutionPolicy Restricted 和PS脚本开头: param ( [string]$slnDir, [string]$projectDir ) 当MSBuild尝试运行它时,我的第一个参数”$(SolutionDir)”被分成两个参数,因为解决方案路径包含一个空格字符: D:\Projects\Dion2 Mercurial\repo\Dion2Web\ 。 所以我的脚本接收D:\Projects\Dion2作为第一个参数, Mercurial\repo\Dion2Web\作为第二个参数。 将这些参数发送到脚本文件的正确方法是什么? 注意:当脚本只有一个参数时,这样的构建后脚本可以正常工作。

EntityFramework.6.1.2-beta1 \ tools \ init.ps1受限制,无法运行enable-migrations命令

Visual Studio 2013 C#ASP.NET MVC问题无法加载EntityFramework.6.1.2-beta1 \ tools \ init.ps1,因为其执行被软件限制策略阻止。 有关更多信息,请与系统管理员联系。 当我尝试使用Enable-Migrations时:我收到以下消息: “Enable-Migrations”一词不被识别为cmdlet,函数,脚本文件或可操作程序的名称。 检查名称的填充,或者如果包含路径,请validation路径是否正确,然后重试。 在行:1 char:18 + Enable-Migrations <<<< + CategoryInfo:ObjectNotFound 🙁 Enable-Migrations:String)[],CommandNotFoundException + FullyQualifiedErrorId:CommandNotFoundException 我有上述问题,我为它尝试了各种解决方案,但没有一个有帮助 我已经完成的事情: 删除所有nuget扩展并再次安装(没有工作) 删除了entity framework并重新安装(不起作用) 关闭我的视觉工作室2013并重新打开文件(没有工作) 使用管理员打开视觉工作室(没有工作) 从一个不同的项目中删除整个包带来的包(没有工作) 我甚至尝试了一些电源shell命令,仍然无法正常工作。

如何启动新的PowerShell实例并在其中运行命令?

这就是我想要完成的事情: 我需要用户在登录计算机时自动运行powershell脚本,让脚本启动Elevated Powershell Prompt(就像用户可以单击Run Powershell作为管理员一样)然后让它在新的中运行一些命令Powershell对象,然后关闭新的Powershell对象。 此函数当前将在Elevated模式下创建并运行新的Powershell对象。 function Set-Elevation { # Create a new process object that starts PowerShell $newProcess = New-Object System.Diagnostics.ProcessStartInfo “powershell”; # Indicate that the process should be elevated $newProcess.Verb = “runas”; # Start the new process [System.Diagnostics.Process]::Start($newProcess) | Out-Null } 但是,如何让它在那里运行新命令? 那之后我怎么关闭这个物体呢? 任何关于语法的提示都将不胜感激。

Powershell v4不会自动导入模块

我使用的是Microsoft PowerShell v4 : PS C:\> get-host Name : ConsoleHost Version : 4.0 InstanceId : 3b4b6b8d-70ec-46dd-942a-bfecf5fb6f31 UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : de-CH CurrentUICulture : en-US PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy IsRunspacePushed : False Runspace : System.Management.Automation.Runspaces.LocalRunspace 我在Visual Studio 2012中开发了一个针对.NET Framework 4的C#项目,其中包含一些Cmdlet和Snapin 。 我可以调试它们,一切正常。 我创建了路径C:\PowerShell\Modules\并将其添加到PSModulePath环境变量中。 我把r MySnapIn.dll放到路径C:\PowerShell\Modules\MySnapIn 。 我希望模块能够自动加载,因此我可以使用新的cmdlet,但它们不是:模块未加载。 我必须编写Import-Module MySnapin才能加载它。 如何自动加载模块?

如何从C#运行PowerShell脚本

我正在尝试使用C#运行PowerShell脚本,但我没有任何成功。 这是我的function: private void ExecutePowerShellCommand(string scriptfile) { RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); runspace.Open(); RunspaceInvoke scriptInvoker = new RunspaceInvoke(); scriptInvoker.Invoke(“Set-ExecutionPolicy Unrestricted”); Pipeline pipeline = runspace.CreatePipeline(); //Here’s how you add a new script with arguments Command myCommand = new Command(scriptfile); //CommandParameter testParam = new CommandParameter(“key”, “value”); //myCommand.Parameters.Add(testParam); pipeline.Commands.Add(myCommand); // Execute PowerShell script pipeline.Invoke(); } […]

C#Runspace Powershell(交互式)

我正在尝试使用C#在运行空间中执行带有参数的Powershell文件。 不幸的是我得到以下输出: A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as […]

检索具有CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6}的组件的COM类工厂失败

我正在创建停止IIS默认网站的应用程序。 我使用PowerShell脚本来停止网站,因为该脚本是从我的网站执行的。 这是我的脚本: Import-Module C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration Stop-Website ‘Default Web Site’ my copy code Start-Website ‘Default Web Site’ 这是我的C#代码: PowerShell _PowerShell = PowerShell.Create(); Runspace rs = RunspaceFactory.CreateRunspace(); rs.Open(); _PowerShell.Runspace = rs; _PowerShell.AddScript(@”Set-ExecutionPolicy RemoteSigned -scope LocalMachine”).Invoke(); _PowerShell.AddScript(@”E:\DE.TEST\Power_Shell\Scripts\StopIISDefaultSite.ps1″).Invoke(); if (_PowerShell.HadErrors) { Collection errors = _PowerShell.Streams.Error.ReadAll(); foreach (var item in errors) { Console.WriteLine(item.ToString()); } } Console.ReadLine(); 它显示以下错误 检索具有CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6}的组件的COM类工厂由于以下错误而失败:80040154未注册类(HRESULTexception:0x80040154(REGDB_E_CLASSNOTREG))。