locked
Incorrect behaviour of Get-ChildItem cmdlet in case IIS locations RRS feed

  • Question

  • User1278457954 posted

    My environment:

    OS Windows 7 enterprise: Service Pack 1
    .NET Framework: 4.0.30319.18052
    Powershell version: 3.0
    IIS: 7.5.7600.16385


    I try to execute following scripts from my c# code:

    script1:

    import-module webadministration
    Get-ChildItem iis:\apppools | % { $_.PSObject.TypeNames.Insert(0, "MyCustomName"); $_ }

    script2:

    import-module webadministration
    Get-ChildItem iis:\sites | %{ $_.PSObject.TypeNames.Insert(0, "MyCustomName"); $_ }

    After execution, on C# side, TypeNames property for each result object will be unchanged(without "MyCustomName" name)
    if i execute one of the scripts twice, number of items of TypeNames property for each result object will be incorrect.

    If i use powershell 2.0 the result will be right.

    Could you please provide  a workaround. It's very critical for me

    C# code:

    using System.Collections.Generic;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;


    namespace ConsoleApplication1
    {

    public class PSCommandProcessor
    {
    private readonly Runspace _runspace;

    public PSCommandProcessor()
    {
    var runspaceConfiguration = RunspaceConfiguration.Create();
    _runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
    _runspace.Open();
    }

    public IEnumerable<PSObject> Execute(string command, params KeyValuePair<string, object>[] arguments)
    {
    var pipeline = _runspace.CreatePipeline();
    var myCommand = new Command(command, true);
    foreach (var argument in arguments)
    {
    myCommand.Parameters.Add(new CommandParameter(argument.Key, argument.Value));
    }
    pipeline.Commands.Add(myCommand);

    return pipeline.Invoke();
    }

    public void Close()
    {
    _runspace.Close();
    }
    }

    class Program
    {
    static void Main()
    {
    var psCmdProcessor = new PSCommandProcessor();
    var result = psCmdProcessor.Execute(Properties.Resources.TextFile1);

    psCmdProcessor.Close();
    }
    }
    }

    Wednesday, October 2, 2013 10:49 AM

Answers

  • User-823319154 posted

    Hi Sergey

    Sergey.terentyev.v

    if i execute one of the scripts twice, number of items of TypeNames property for each result object will be incorrect.    If i use powershell 2.0 the result will be right.

    I'm not sure what the root of this issue is,  I think you could give a feedback to Microsoft connect site(http://connect.microsoft.com/ ), Microsoft engineers will evaluate this seriously, and check whether they have good workarounds for you.

    Regards

     

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Monday, October 7, 2013 2:59 AM

All replies

  • User-823319154 posted

    Hi Sergey

    Sergey.terentyev.v

    if i execute one of the scripts twice, number of items of TypeNames property for each result object will be incorrect.    If i use powershell 2.0 the result will be right.

    I'm not sure what the root of this issue is,  I think you could give a feedback to Microsoft connect site(http://connect.microsoft.com/ ), Microsoft engineers will evaluate this seriously, and check whether they have good workarounds for you.

    Regards

     

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Monday, October 7, 2013 2:59 AM
  • User1278457954 posted

    Hi. Thanks for your reply.

    Thursday, October 10, 2013 9:58 AM