我正在编写一个函数并观察输出的一些异常行为。
这是代码:
K = repmat({K}, 8,1);
C = spblkdiag(K{:}) * spblkdiag(A{:}).' * spblkdiag(B{:});
C = reshape(nonzeros(C), 18,[]);
C = mat2cell(C, 18,18 * ones(8,1))';
从pipline传递参数时:
“karuma”,“localhost”|得到-CompInfo
Function Get-CompInfo
{
[Cmdletbinding()]
PARAM
(
[Parameter(ValueFromPipeline=$true)]
[Alias('Comp', 'Host')]
[string[]]$computername
)
Begin
{
}
Process
{
if ($computername -eq $Null) {
$computername=$env:computername
$VerboseOut="No computer specified. Running against local computer $computername :"
}
Else {
$VerboseOut="Getting information for computer $computername :"
}
Write-Verbose $VerboseOut
$CompInfo=Get-WmiObject Win32_Computersystem -computername $computername
$OSInfo=Get-WmiObject Win32_OperatingSystem -computername $computername
$Properties = [ordered]@{
'Input'=$computername
'SystemName'=$CompInfo.Name
'Manufacturer'=$CompInfo.Manufacturer
'Model'=$CompInfo.Model
'PhysicalMemory'=$CompInfo.TotalPhysicalMemory
'LogicalProcessors'=$CompInfo.NumberOfLogicalProcessors
'OSCaption'=$OSInfo.Caption
'OSArchitecture'=$OSInfo.OSArchitecture
'ServicePackMajorVersion'=$OSInfo.ServicePackMajorVersion}
# Output Information
$obj=New-Object -TypeName PSobject -Property $Properties
write-output $obj
}
End
{
}
}
当我传递带有计算机名称列表的文本文件时,我得到了相同类型的输出。
当指定多个不同的主机名时:
Get-CompInfo -computername localhost,karuma
Input : {karuma}
SystemName : KARUMA
Manufacturer : Hewlett-Packard
Model : h9-1400a
PhysicalMemory : 17115000832
LogicalProcessors : 8
OSCaption : Microsoft Windows 10 Pro
OSArchitecture : 64-bit
ServicePackMajorVersion : 0
Input : {localhost}
SystemName : KARUMA
Manufacturer : Hewlett-Packard
Model : h9-1400a
PhysicalMemory : 17115000832
LogicalProcessors : 8
OSCaption : Microsoft Windows 10 Pro
OSArchitecture : 64-bit
ServicePackMajorVersion : 0
我希望在传递多个值时看到表输出,就像管道到format-table一样。
对于我需要更改以获得所需输出的任何帮助将不胜感激。
答案 0 :(得分:0)
这是你设置函数的方式......当使用管道时,PROCESS块将为管道中的每个对象运行一次 - $ computername param在这种情况下一次只包含一个对象。 / p>
当你在$ computername param中指定两台计算机时,你正在改变整个函数的运行方式,因为它包含两个对象。
通过将函数包装在Foreach中可以很容易地修复:
Function Get-CompInfo
{
[Cmdletbinding()]
PARAM
(
[Parameter(ValueFromPipeline=$true)]
[Alias('Comp', 'Host')]
[string[]]$computername
)
Begin {}
Process
{
Foreach ($computer in $computername) {
if ($computer -eq $Null) {
$computer=$env:computername
$VerboseOut="No computer specified. Running against local computer $computer :"
}
Else {
$VerboseOut="Getting information for computer $computer :"
}
Write-Verbose $VerboseOut
$CompInfo=Get-WmiObject Win32_Computersystem -computername $computer
$OSInfo=Get-WmiObject Win32_OperatingSystem -computername $computer
$Properties = [ordered]@{
'Input'=$computer
'SystemName'=$CompInfo.Name
'Manufacturer'=$CompInfo.Manufacturer
'Model'=$CompInfo.Model
'PhysicalMemory'=$CompInfo.TotalPhysicalMemory
'LogicalProcessors'=$CompInfo.NumberOfLogicalProcessors
'OSCaption'=$OSInfo.Caption
'OSArchitecture'=$OSInfo.OSArchitecture
'ServicePackMajorVersion'=$OSInfo.ServicePackMajorVersion}
# Output Information
$obj=New-Object -TypeName PSobject -Property $Properties
write-output $obj
}
}
End {}
}