PowerShell PSObject -pass 和 -passthru 之间的混淆
PowerShell PSObject confusion between -pass & -passthru
function getinfo {
$strComputer = "localhost"
$colItems = GWMI -cl "Win32_NetworkAdapterConfiguration" -name "root\CimV2" -comp $strComputer -filter "IpEnabled = TRUE"
$Items1 = $colItems | Select DHCPServer, Caption, DNSHostName, IPAddress
$Items2 = $colItems | Select ServiceName, MacAddress, IPSubnet, InterfaceIndex
}
$objects = (New-Object PSObject |
add-member -pass NoteProperty "DHCP Server" $Items1.DHCPServer |
add-member -pass NoteProperty "IP Address" $Items1.IPAddress |
add-member -passthru NoteProperty "Mac Address" $Items2.MacAddress |
add-member -passthru NoteProperty "IP Subnet" $Items2.IPSubnet
)
$objects | ConvertTo-Json
我对 -pass
和 -passthru
键感到困惑。有什么区别,为什么当 -passthru
用于 $Items1
时什么也没有填充?
问题不在于 -pass
或 -passthru
。问题是在函数内部创建的变量通常仅在该函数仍然 运行 时才可用。来自 about_Scopes 的帮助:
Windows PowerShell protects access to variables, aliases, functions,
and Windows PowerShell drives (PSDrives) by limiting where they can be
read and changed.
如果您通过 using dot-source 调用该函数,那么您可以保留变量以供在 New-Object
命令中使用。
function getinfo {
$strComputer = "localhost"
$colItems = GWMI -cl "Win32_NetworkAdapterConfiguration" -name "root\CimV2" -comp $strComputer -filter "IpEnabled = TRUE"
$Items1 = $colItems | Select DHCPServer, Caption, DNSHostName, IPAddress
$Items2 = $colItems | Select ServiceName, MacAddress, IPSubnet, InterfaceIndex
}
. getinfo
$objects = (New-Object PSObject |
add-member -pass NoteProperty "DHCP Server" $Items1.DHCPServer |
add-member -pass NoteProperty "IP Address" $Items1.IPAddress |
add-member -passthru NoteProperty "Mac Address" $Items2.MacAddress |
add-member -passthru NoteProperty "IP Subnet" $Items2.IPSubnet
)
$objects | ConvertTo-Json
function getinfo {
$strComputer = "localhost"
$colItems = GWMI -cl "Win32_NetworkAdapterConfiguration" -name "root\CimV2" -comp $strComputer -filter "IpEnabled = TRUE"
$Items1 = $colItems | Select DHCPServer, Caption, DNSHostName, IPAddress
$Items2 = $colItems | Select ServiceName, MacAddress, IPSubnet, InterfaceIndex
}
$objects = (New-Object PSObject |
add-member -pass NoteProperty "DHCP Server" $Items1.DHCPServer |
add-member -pass NoteProperty "IP Address" $Items1.IPAddress |
add-member -passthru NoteProperty "Mac Address" $Items2.MacAddress |
add-member -passthru NoteProperty "IP Subnet" $Items2.IPSubnet
)
$objects | ConvertTo-Json
我对 -pass
和 -passthru
键感到困惑。有什么区别,为什么当 -passthru
用于 $Items1
时什么也没有填充?
问题不在于 -pass
或 -passthru
。问题是在函数内部创建的变量通常仅在该函数仍然 运行 时才可用。来自 about_Scopes 的帮助:
Windows PowerShell protects access to variables, aliases, functions, and Windows PowerShell drives (PSDrives) by limiting where they can be read and changed.
如果您通过 using dot-source 调用该函数,那么您可以保留变量以供在 New-Object
命令中使用。
function getinfo {
$strComputer = "localhost"
$colItems = GWMI -cl "Win32_NetworkAdapterConfiguration" -name "root\CimV2" -comp $strComputer -filter "IpEnabled = TRUE"
$Items1 = $colItems | Select DHCPServer, Caption, DNSHostName, IPAddress
$Items2 = $colItems | Select ServiceName, MacAddress, IPSubnet, InterfaceIndex
}
. getinfo
$objects = (New-Object PSObject |
add-member -pass NoteProperty "DHCP Server" $Items1.DHCPServer |
add-member -pass NoteProperty "IP Address" $Items1.IPAddress |
add-member -passthru NoteProperty "Mac Address" $Items2.MacAddress |
add-member -passthru NoteProperty "IP Subnet" $Items2.IPSubnet
)
$objects | ConvertTo-Json