How can I an Object's Property to a String in Powershell
I'm pretty familiar with PHP, Javascript, etc but I'm new to Powershell. I'm try开发者_JS百科ing to get IP addresses and Mac addresses from all the computer's in an active directory. I came across a script that gets an object with all the computer names a lists them through a foreach loop. Inside that loop, I'm trying to run the following:
$colItems = GWMI -cl "Win32_NetworkAdapterConfiguration" -name "root\CimV2" `
comp $objComputer.name -filter "IpEnabled = TRUE"
Obviously, this isn't working since it can't take data from the pipeline. Is there away to take the name property and convert it into a string variable to use within the Get-WmiObject? I've tried declaring a string variable with that $objComputer.name as the value and tried $($objComputer.name), but neither did it for me. Any help would be greatly appreciated.
i think you need a hyphen in front of comp...
$colItems = GWMI -cl "Win32_NetworkAdapterConfiguration" -name "root\CimV2" -comp $objComputer.name -filter "IpEnabled = TRUE"
that could be a copy and paste error...
This works for me though (using a dot):
$colItems =GWMI -cl "Win32_NetworkAdapterConfiguration" -name "root\CimV2" -comp . -filter "IpEnabled = TRUE"
using $objComputer though you could do this
$objComputer = @{ "name" = "." }
Matt
Unfortunately Get-WmiObject doesn't accept pipeline input. Altough passing $objComputer.name is perfectly valid (as long as $objComputer is an object that has a name member) you can assign the name value to a vraible and pass the variable to the ComputerName property.
foreach($objComputer $computers)
{
$name = $objComputer.name
$colItems = GWMI -cl Win32_NetworkAdapterConfiguration -comp $name -filter "IpEnabled = TRUE"
$colItems
...
}
精彩评论