[英]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 :
List
in your case).不应输出集合类型的实例(例如您的情况下的List
)。 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
的一个实例的管道之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.