简体   繁体   English

这个迭代 List 的 powershell 脚本有什么问题? Foreach-Object -Process { 设置别名 ... }

[英]What is wrong with this powershell script that iterates a List? Foreach-Object -Process { Set-Alias ... }

I have got a module that returns a List of LinuxAlias (class I made)我有一个返回 LinuxAlias 列表的模块(我创建的类)

When I try to iterate them to Set-Alias in my session I am having troubles.当我尝试在会话中将它们迭代到 Set-Alias 时,我遇到了麻烦。

Data: MyModule has the Get-LinuxAliases command.数据:MyModule 有 Get-LinuxAliases 命令。 Get-LinuxAliases command is a binary module that returns a List of LinuxAlias. Get-LinuxAliases 命令是一个二进制模块,它返回一个 LinuxAlias 列表。

Output definition:输出定义:

[OutputType(typeof(List<LinuxAlias>))]

LinuxAlias definition: Linux别名定义:

public class LinuxAlias
    {
        public String Name { get; set; }
        public String RawCommand { get; set; }

        public String Value { get; set; }
        public String TranslatedPowershellCommand { get; set; }
        public class COMMENT_BLOCK_EXCEPTION : Exception
        {
        }
        public class EMPTY_LINE_EXCEPTION : Exception
        {
        }
        public class UNRECOGNIZED_INPUT : Exception
        {
        }
    }

I do:我愿意:

if ( Get-Module "MyModule" ) {
    Get-LinuxAliases | ForEach-Object -Process { 
        try {
            [string]$name = $_.Name;
            [string]$val = $_.Value;
                Set-Alias -Name $name -Value $val -Force;
        }
        catch {
            Write-Warning $_.Exception.GetType().FullName;
        }

    }
}

What is the expected behaviour?什么是预期的行为? Since I noticed, for example.自从我注意到,例如。 If I just Write-Host $_.Name and Write-Host $_.Value for each iteration.如果我每次迭代只Write-Host $_.Name和写主机 $_.Value。 It first will write names and then values, having something like:它首先将写入名称,然后写入值,例如:

name1 name2 ... value1 value2 ... name1 name2 ... value1 value2 ...

What is not the expected behaviour in a For-Each iterator I guess.我猜在 For-Each 迭代器中什么不是预期的行为。 It should be:它应该是:

name1 value1 name2 value2名称1 值1 名称2 值2

(Get-LinuxAliases) [0] returned : (Get-LinuxAliases) [0] 返回:

Name RawCommand Value TranslatedPowershellCommand
---- ---------- ----- ---------------------------
cls  'clear'    clear

Someone could bring me light to this scenario?有人可以让我了解这种情况吗?

Thanks in advance.提前致谢。

that returns a List of LinuxAlias.返回一个 LinuxAlias 列表。

Generally, except in special circumstances, cmdlets :通常,除特殊情况外, cmdlet

  • should not output instances of a collection type (such as List in your case).不应输出集合类型的实例(例如您的情况下的List )。
  • instead should output multiple objects to the pipeline one by one .相反,应该将多个对象一一输出到管道

The reason is that other cmdlets - such as ForEach-Object - expect other commands to produce output object by object .原因是其他 cmdlet(例如ForEach-Object )期望其他命令按对象生成输出对象

Get-LinuxAliases | ForEach-Object -Process { ...

With your current design, ForEach-Object receives a single input object that is the list as a whole , which is not your intent.对于您当前的设计, ForEach-Object接收一个作为整体列表单个输入对象,这不是您的意图。

Therefore:所以:

  • Make your cmdlet-implementing class indicate that it returns LinuxAlias , not a list of it;让你的 cmdlet 实现类表明它返回LinuxAlias ,而不是它的列表 that multiple instances of that type may be returned is invariably implied in PowerShell. PowerShell总是暗示可能会返回该类型的多个实例。

     [OutputType(typeof(LinuxAlias)]
  • To produce output from your cmdlet, use WriteObject(list, true) , ie, use the WriteObject overload that enumerates collections ;要从 cmdlet 生成输出,请使用WriteObject(list, true) ,即使用枚举集合WriteObject重载 that is, make it send the LinuxAlias instances stored in your List instance to the pipeline one by one .也就是说,令其发送LinuxAlias存储在您的实例List的一个实例的管道之一

处理列表中的项目<object>通过一组线程 - 线程池<div id="text_translate"><pre>public void multithread() { List&lt;Bbject&gt; objectlist= new List&lt;Object&gt;{ //consider it has around 10 objects }; foreach (var obj in objectlist) { Process(obj); } } private void process(Object obj) { //Implementation }</pre><p> 我正在尝试实现多线程,其中每个线程采用 object 并对其进行处理...我尝试创建 5 个单独的线程,其中每个线程处理 2 个对象...但它更像是硬配置...有什么方法可以使用它来实现它线程池...</p></div></object> - Process items in list<Object> by a set of threads - threadpooling

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#中的Powershell管道和Foreach对象 - Powershell Pipe and Foreach-Object in c# C#Powershell管道foreach-object - C# Powershell Pipeline foreach-object Foreach-Object项目到数组,而不是子数组 - Foreach-Object items to an array, not a sub-array 处理列表中的项目<object>通过一组线程 - 线程池<div id="text_translate"><pre>public void multithread() { List&lt;Bbject&gt; objectlist= new List&lt;Object&gt;{ //consider it has around 10 objects }; foreach (var obj in objectlist) { Process(obj); } } private void process(Object obj) { //Implementation }</pre><p> 我正在尝试实现多线程,其中每个线程采用 object 并对其进行处理...我尝试创建 5 个单独的线程,其中每个线程处理 2 个对象...但它更像是硬配置...有什么方法可以使用它来实现它线程池...</p></div></object> - Process items in list<Object> by a set of threads - threadpooling 此ForEach循环有什么问题? - What's wrong with this ForEach loop? C#在数组列表上使用foreach时,即使列表长度为86,foreach也仅迭代5次 - C# When using foreach on a list of arrays, the foreach only iterates 5 times even though the list has length 86 IndexOf 在列表中的 foreach object 中返回 -1 - IndexOf returns -1 in foreach object in list 将并行 ForEach 转换为 PLINQ 以处理任务列表 - Converting Parallel ForEach into PLINQ to process list of Tasks 按类型处理对象列表 - Process list of object by type 使用 Process.Start 启动 PowerShell 脚本会关闭 PowerShell 且没有错误 - Starting PowerShell Script with Process.Start closes PowerShell with no error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM