Tag: powershell

PowerShell操纵XML文件问题

假设我有以下XML文件,并且我想使用PowerShell(版本1.0)来操作XML文件以获取Foo的值(在此示例中,我想得到的值是“Foo Value”)和Goo(在此样本,我想得到的价值是“Goo Value”),任何想法如何实现? $FooConfig = [xml](get-content .\Foo.exe.config -ErrorAction:stop) 乔治,提前谢谢

使用C#在Exchange 2010上创建邮箱

我试图通过我的C#windows应用程序在Exchange 2010上创建用户邮箱。 这是我的代码 PSCredential creds = new PSCredential(“user id”, StringToSecureString(“password”)); System.Uri uri = new Uri(“http://servername/powershell?serializationLevel=Full”); Runspace runspace = RunspaceFactory.CreateRunspace(); PowerShell powershell = PowerShell.Create(); PSCommand command = new PSCommand(); command.AddCommand(“New-PSSession”); command.AddParameter(“ConfigurationName”, “Microsoft.Exchange”); command.AddParameter(“ConnectionUri”, uri); command.AddParameter(“Credential”, creds); command.AddParameter(“Authentication”, “Default”); powershell.Commands = command; runspace.Open(); powershell.Runspace = runspace; Collection result = powershell.Invoke(); powershell = PowerShell.Create(); command = new PSCommand(); […]

从PowerShell管理单元引用外部程序集

我正在开发一个自定义PowerShell管理单元,它引用了解决方案中的另一个项目。 当我尝试调试管理单元时(遵循[这些说明] [1]),程序集无法加载,我的cmdlet失败并显示消息“无法加载文件或程序集……” 如何指示PowerShell如何定位程序集,或者如何指定管理单元所需的程序集位置? 我宁愿避免在GAC中注册程序集,至少在开发期间是这样。

使用C#/ Powershell设置Active Directory“安全身份映射”/“名称映射”不会添加到正确的“存储”

我已按照本指南操作: https : //blogs.msdn.microsoft.com/adpowershell/2009/04/26/working-with-certificates-in-active-directory-powershell/ 证书已成功添加到用户的已发布证书中。 但这不是我真正想要的。 相反,我希望将证书添加到AD用户的X509证书(Active Directory中的名称映射/安全身份映射) 安全身份映射 有没有办法在C#或Powershell中执行此操作?

在C#上执行PowerShell命令行

我正在尝试在C#应用程序中执行此命令(列出Firefox扩展)。 Get-ChildItem -Path $env:USERPROFILE”\AppData\Roaming\Mozilla\Firefox\Profiles\*\extensions.json” | Get-Content 从MSDN文档中我发现代码看起来应该像 PowerShell ps = PowerShell.Create(); ps.AddCommand(“Get-ChildItem”); ps.AddParameter(“Path”, “\”$env:USERPROFILE\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\*\\extensions.json\””); ps.AddCommand(“Get-Content”); Collection results = ps.Invoke(); 但是,“结果”为null (当PS线不是时)。 我在SO上发现了一些类似的问题,但我真的无法回答这个问题。 有谁知道如何修复我的代码?

PSCmdlet动态自动完成一个参数(如Get-Process)

在powershell中,某些参数具有动态自动完成行为。 例如,Get-Process参数Name。 我可以使用TAB迭代我的所有进程。 我想在我的PSCmdlet中使用此行为。 但问题是,我只知道如何使用静态自动完成值来做到这一点。 看例子: public class TableDynamicParameters { [Parameter] [ValidateSet(“Table1”, “Table2”)] public string[] Tables { get; set; } } 这是一个如何使用原生powershell http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/21/use-dynamic-parameters-to-populate-list-of-printer-names完成此操作的示例。 ASPX 它适用于@bouvierr public string[] Tables { get; set; } public object GetDynamicParameters() { if (!File.Exists(Path)) return null; var tableNames = new List(); if (TablesCache.ContainsKey(Path)) { tableNames = TablesCache[Path]; } else { try […]

如何使用c#中的“format-list”和“out-file”管道调用powershell命令?

嗨,我正在开发一个C#程序来调用远程运行空间中的Exchange 2010 powershell cmdlet。 ps命令是: “Get-MailboxDatabase -Server EX2010SVR1 -Status | Format-List Identity,Guid,mounted,CircularLoggingEnabled,Recovery | Out-File’C:\ db.txt’-Encoding UTF8 -Width 8192”。 我的代码类似于: static int Main(string[] args) { const string SHELL_URI = “http://schemas.microsoft.com/powershell/Microsoft.Exchange”; const string COMMAND = “Get-MailboxDatabase -Server EX2010SVR1 -Status | Format-List Identity,Guid,mounted,CircularLoggingEnabled,Recovery | Out-File ‘C:\db.txt’ -Encoding UTF8 -Width 8192”; System.Uri serverUri = new Uri(“http://EX2010SVR1/powershell?serializationLevel=Full”); PSCredential creds = […]

从C#远程连接到PowerShell

我想用Windows远程连接从Windows 7到Windows Server 2008 R2(安装在VMware中)的PowerShell。 这是我的代码: string shell = “http://schemas.microsoft.com/powershell/Microsoft.PowerShell”; var target = new Uri(“http://win-qkheb9s51i8/wsman”); Pipeline p = runSpace.CreatePipeline(); SecureString passing = new SecureString(); string password = “A123456a”; foreach (char c in password) { passing.AppendChar(c); } passing.MakeReadOnly(); var cred = new PSCredential(@”win-qkheb9s51i8\Administrator”, passing); var connectionInfo = new WSManConnectionInfo(target, shell, cred); connectionInfo.OperationTimeout = 4 * 60 […]

从事件处理程序报告Powershell进度

我在C#中编写了一个cmdlet,它充当了重/长时间运行的同步操作的包装器。 该方法(其他人的代码)通过事件处理程序报告在此长时间运行操作期间的百分比进度,并且我想将这些连接到PowerShell的标准WriteProgress方法以获得漂亮打印的进度条。 但是,我收到以下错误消息: The WriteObject and WriteError methods cannot be called from outside the overrides of the BeginProcessing, ProcessRecord, and EndProcessing methods, and they can only be called from within the same thread. 这是我的代码: overrride void ProcessRecord() { LongRunningOperation op = new LongRunningOperation(); op.ProgressChanged += ProgressUpdate; op.Execute(); op.ProgressChanged -= ProgressUpdate; } void ProgressUpdate(object sender, ProgressChangeEventArgs […]

从c#应用程序调用azure powershell cmdlet失败

我正在自动化部署到azure云的过程。 我的powershell脚本就是这样做的,当从azure powershell命令行执行它时,它就像一个魅力。 当我尝试从c#应用程序调用相同的脚本时,它失败了。 这是我的代码: internal void RunPowerShellScript(string scriptPath, Dictionary arguments) { RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); runspace.Open(); RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace); Pipeline pipeline = runspace.CreatePipeline(); //Here’s how you add a new script with arguments Command myCommand = new Command(scriptPath, true); foreach (var argument in arguments) { myCommand.Parameters.Add(new CommandParameter(argument.Key, argument.Value)); } […]