Sorry but I am just getting into Powershell so my knowledge is limited.
I have the following script:
Add-PSSnapin Quest.ActiveRoles.ADManagement
$PCS = Get-QADComputer -ManagedBy (Get-QADUser swindmiller) | Select-Object Description
Which gives me an output for $PCS of:
Model / Serial / Building
Model / Serial / Building
Model / Serial / Building
I want to reference each Model, Serial, Building, with something like $PCS[0],$PCS[1],$PCS[2]. I assume I would have to use some sort of foreach command for each line but I am getting tripped up on the split command.
I keep getting:
Method invocation failed because [Selected.Quest.ActiveRoles.ArsPowerShellSnapIn.Data.ArsComputerObject] does not contain a method named ‘Split’.
Can someone give me a hand and possibly an example?
Thanks,
Scott
Are there literal spaces on either side of the slashes?
It would be better if you could post real world data (sanitized as necessary) rather than the made up data above.
Also itls not clear from above what "$PCS[0],$PCS[1],$PCS[2]:" means.
And is there a reason why you are using the Quest cmdlets instead of the native AD cmdlets?
Scott, I do not have access to the Quest CMDLETs, but I have an idea for you. Pipe your output to Get-Member. If the TYPENAME is System.String, the you can try to split your output using “`n” That is a back tick in front of the n, not a single quote.
First save all of the output into a single variable. Let’s call this variable $Data.
Next, Split the data: $Split = $Data.Split(“`n”)
Not access the data. $Split[0]
I see that the native cmdlets don’t easily offer up -ManagedBy. You could do something like this …
Add-PSSnapin -Name Quest.ActiveRoles.ADManagement
$mgrName = "swindmiller"
$mgrDN = (Get-QADUser ).DN
$computers = Get-QADComputer -ManagedBy $mgrDN
$results = foreach ($computer in $computers)
{
$model, $serial, $building = $computer.Description -split "\s*/\s*"
[PSCustomObject]@{
Computer = $computer.ComputerName.trimend('$')
Manager = $mgrName
Model = $model
Serial = $serial
Building = $building
}
}
# Sample outputs - pick one or more, your choice
$results
$results | Format-Table -AutoSize
$results | Out-GridView
$results | Export-Csv -Path .\foo.csv -NoTypeInformation -Encoding ASCII
$results | Out-File -FilePath .\foo.txt -Encoding ASCII
$results | Export-Clixml -Path .\foo.xml -Encoding ASCII
Thanks so much Bob McCoy! I am learning but was totally thinking about it the wrong way. Thanks!
I was then able to use $results | Select-Object Computer,Model at the end.
This worked perfectly.
The only change I had to make was to the 3rd line:
$mgrDN = (Get-QADUser ).DN
to
$mgrDN = (Get-QADUser $mgrName).DN
Scott
Yeah, sorry. That was an edit error when I was removing real world data from the posted script.
As with just about anything in PowerShell you will find there are multiple ways to get it done. I prefer a collection of objects. That gives me maximum flexibility in my output options at the end.