Spliting a string....Newbie

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Bob McCoy Bob McCoy 5 months, 2 weeks ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #37588
    Profile photo of Scott Windmiller
    Scott Windmiller
    Participant

    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

    #37589
    Profile photo of Bob McCoy
    Bob McCoy
    Participant
  • 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?
#37591
Profile photo of Jason Yoder
Jason Yoder
Participant

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]

#37596
Profile photo of Bob McCoy
Bob McCoy
Participant

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
#37609
Profile photo of Scott Windmiller
Scott Windmiller
Participant

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

#37635
Profile photo of Bob McCoy
Bob McCoy
Participant

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.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.