我的目标是将一些信息保存到稍后将被写出的变量中($Server
)。我创建了一个从 CPU 获取一些信息的 cmdlet,它为我提供了我想要添加到我的服务器变量中的结果。当我添加它时,我得到一个$null
值。
PS C:\> $CPU = Get-CPUInfo -Computername .
PS C:\> $CPU
CPUSpecification CPUCurrentClockSpeed CPUMaxClockSpeed Socket
---------------- -------------------- ---------------- ------
Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz 2300 2400 {@{SocketDesignation=U3E1; NumberOfCores=2; NumberOfLogicalProcessors=4}}
PS C:\> $Server = New-Object PSObject
PS C:\> Add-Member -InputObject $Server -MemberType NoteProperty -Name "CPU" -Value $CPU
PS C:\> $Server
CPU
---
{$null, @{CPUSpecification=Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz; CPUCurrentClockSpeed=2300; CPUMaxClockSpeed=2400; Socket=System.Object[]}}
PS C:\>
我想知道为什么我在里面得到这个 $null ,如果它是一些有用的信息,我该如何改变它?
稍后我将创建一个 .json 文件来存储我的 $Server 变量,它还将空值保存在那里。
我真的不喜欢我的用户收到预期没有的空值的想法。
编辑:我已经制作了我使用的这个自定义 Get-CPUInfo cmdlet,但是它给了我我需要的东西并且没有 $null 值:
function Get-CPUInfo
{
[CmdletBinding()]
Param
(
# Name of the computer to get version from
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[Alias("CN", "Host", "HostName")]
[string]$Computername
)
$MySocket = Get-WmiObject -Class Win32_Processor -ComputerName $ComputerName
$IsSocketArray = $MySocket -is [system.array]
$SocketInfo
$SocketInfoArray = @()
if(-not $IsSocketarray)
{
$SocketInfo = New-Object PSObject
Add-Member -InputObject $SocketInfo -MemberType NoteProperty -Name "SocketDesignation" -Value $MySocket.SocketDesignation
Add-Member -InputObject $SocketInfo -MemberType NoteProperty -Name "NumberOfCores" -Value $MySocket.NumberOfCores
Add-Member -InputObject $SocketInfo -MemberType NoteProperty -Name "NumberOfLogicalProcessors" -Value $MySocket.NumberOfLogicalProcessors
$SocketInfoArray += $SocketInfo
}
else
{
foreach ($s in $MySocket)
{
$SocketInfo = New-Object PSObject
Add-Member -InputObject $SocketInfo -MemberType NoteProperty -Name "SocketDesignation" -Value $s.SocketDesignation
Add-Member -InputObject $SocketInfo -MemberType NoteProperty -Name "NumberOfCores" -Value $s.NumberOfCores
Add-Member -InputObject $SocketInfo -MemberType NoteProperty -Name "NumberOfLogicalProcessors" -Value $s.NumberOfLogicalProcessors
$SocketInfoArray += $SocketInfo
}
}
$CPUSpecification = if($IsSocketarray){ $MySocket[0].Name} else{ $MySocket.Name}
$CPUCurrentClockSpeed = if($IsSocketarray){ $MySocket[0].CurrentClockSpeed} else{ $MySocket.CurrentClockSpeed}
$CPUMaxClockSpeed = if($IsSocketarray){ $MySocket[0].MaxClockSpeed} else{ $MySocket.MaxClockSpeed}
$CPUInfo = New-Object PSObject
Add-Member -InputObject $CPUInfo -MemberType NoteProperty -Name "CPUSpecification" -Value $CPUSpecification
Add-Member -InputObject $CPUInfo -MemberType NoteProperty -Name "CPUCurrentClockSpeed" -Value $CPUCurrentClockSpeed
Add-Member -InputObject $CPUInfo -MemberType NoteProperty -Name "CPUMaxClockSpeed" -Value $CPUMaxClockSpeed
Add-Member -InputObject $CPUInfo -MemberType NoteProperty -Name "Socket" -Value $SocketInfoArray
return $CPUInfo
}