Data Formatting Trouble

Hello

 I need some help formatting some data. I am scraping data from WMI into an array.

$data = Get-WmiObject -ComputerName $SiteServer -Namespace $Namespace -Query $query | Select Name | Sort Name

I am getting the data back, paraphrased below. I want to drop the "domain\" portion from each line in the array so all I have is a list of group names without the domain.

Domain\Group Name 

Anyone have suggestions on how to do that?                                                                                                                                             

-Tony

August 20th, 2015 2:57pm

Hi,

Split on the \ character and select the last item.

$example = 'domain\user'
$userName = $example.Split('\')[-1]

$userName

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 3:01pm

What WMI class are you referencing?

What namespace are you referencing?

What query statement are you using?

August 20th, 2015 3:05pm

Here is more info:

#// required parameters
$SiteServer = "sccm_site_server"
$Sitename = "PS1"
$Namespace = "ROOT\SMS\site_$sitename"

$query = "SELECT Name from sms_R_UserGroup WHERE name LIKE '%DoIT - IT Share - Has Paid for %'"
#//$query = "SELECT Name from sms_R_UserGroup"


$data = Get-WmiObject -ComputerName $SiteServer -Namespace $Namespace -Query $query | Select Name | Sort Name
 

The data in the array looks like this: UMC-USERS\DoIT - IT Share - Has Paid for Windows Server 2012. I need to drop the "umc-users\" .

-Tony

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 3:15pm

The data in the array looks like this: UMC-USERS\DoIT - IT Share - Has Paid for Windows Server 2012. I need to drop the "umc-users\" .

Pipe $data through a ForEach-Object loop and do what I posted above on each element.
August 20th, 2015 3:19pm

You can do it like Mike has described.

If it gets little more complicated you can also use Regex to extract Information from strings. But in this example just go for.

$data | % {$_.Name = $_.Name.Split('/')[-1]}
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 3:23pm

If you like regular-expressions and complexity ;-) :

$result="domain\groupname" -match "(?<=\\).+"
$groupname = $($Matches.Values).ToString()
$groupname
August 20th, 2015 4:59pm

Here is how I put it together: 

Short Hand

$groups| % {$_.name = $_.name.Split('\')[-1]}

Long Hand

ForEach ($group in $groups) {

#//$group.name.Split('\')[-1]
}

Thanks for helping all!

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 6:09pm

Cheers, you're very welcome. Glad you got it working.
August 20th, 2015 10:39pm

What is the significance of the [-1], I have been researching but have not quite figured it out yet. -Tony
Free Windows Admin Tool Kit Click here and download it now
August 22nd, 2015 2:52pm

Hi Tony,

-1 means the last item. Or possibly better to understand - the first item starting from the last position.

August 22nd, 2015 3:56pm

Both will work.

The Long Hand version will be faster, but you'll need to concern yourself with memory usage if you've a huge collection of objects to process.

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 2:54pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics