Extract Variable Values from String

Looking for an elegant way to extract values out of a few strings:
Sorry this is super messy, but I’m just trying to make it all work

Blockquote

$FileNameA = “C:\ADBackup\GPOs{2C197968-849A-4A6E-B2CB-2715357DF2C5}\Machine\Preferences\Groups\Groups.xml”

#REM GPGuid is the value between the { } brackets from $FileNameA
#REM GPKey is the after } but does not include the last \ on the right or data after, basically chop off \Groups.xml
$GPGuidA = 2C197968-849A-4A6E-B2CB-2715357DF2C5
$GPKeyA = Machine\Preferences\Groups
###################################################################################

$FileNameB = “C:\ADBackup\GPOs{7AF2FC2F-7A88-47BB-9F61-BEE0C929DF6E}\User\Preferences\Drives\Drives.xml”

#REM GPGuid is the value between the { } brackets from $FileNameB
#REM GPKey is the after } but does not include the last \ on the right or data after, basically chop off \Drives.xml

Blockquote

I got it to partially work, but it’s super sloppy.

Blockquote

$Temp = {
$pos = $name.IndexOf($Temp)
$leftPart = $name.Substring(0, $pos)
$rightPart = $name.Substring($pos+1)
$Temp = }
$pos = $rightpart.IndexOf($Temp)
$GPOGuid = $rightpart.Substring(0, $pos)
$Temp = }
$pos = $name.IndexOf($Temp)
$GPOPath = $name.Substring($pos+2)
$Temp = \
$pos = $name.IndexOf($Temp)
$GPOXML = $name.Substring($pos+1) ###Not working

Blockquote

Next I want to read in the contents of the .XML files and get data out of them:

Contents of Groups.xml
#This GPO adds MyCorp\Office365_Containers to a local server group named FSLogix ODFC Include List and adds MyCorp\Profile_Containers to a local server group named FSLogix Profile Include List

<?xml version="1.0" encoding="UTF-8"?>[](%5Cl%20%22%22)[](%5Cl%20%22%22)

Contents of Drives.xml
#REM This GPO maps 3 drive letters

<?xml version="1.0" encoding="UTF-8"?>[](%5Cl%20%22%22)[](%5Cl%20%22%22)[](%5Cl%20%22%22)

##Attaching DrivesXML.jpg


file so formatting is better

hey,

Not sure I understand but i think you can use regular expressions to get those values.

I searched on google how to find value between two strings or exclude string etc and had a go.

find value between brackets

value after } but does not include the last \ on the right or data after this one is messy :smile:

i might be completely off with my approach lol

# REM GPGuid is the value between the { } brackets from $FileNameA
[regex]$rxGuid = '(?<={).*(?=})'

# REM GPKey is the after } but does not include the last \ on the right or data after, basically chop off \Groups.xml
[regex]$rxKey = '(?<=}\\)(.*)(?:(?![^\\])\\)'

$FileNameA = “C:\ADBackup\GPOs{2C197968-849A-4A6E-B2CB-2715357DF2C5}\Machine\Preferences\Groups\Groups.xml”
$FileNameB = “C:\ADBackup\GPOs{7AF2FC2F-7A88-47BB-9F61-BEE0C929DF6E}\User\Preferences\Drives\Drives.xml”

$GPGuid = $rxGuid.Matches($FileNameA).value
$GPKey = $rxKey.Matches($FileNameA).groups[1].value

try to format your code, there’s a guide how to do it on the forum. for the second part you can check online/this forum how to extract data from xml file or attach sample xml and we can try to test

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

An image of code or XML sample data is not helpful at all.

PowerShell is able to work with XML data. So if I got it right you don’t need regular expressions at all.

ah apologies I assumed user was referring to filename in string and thought xml part was something different.

You’re example was perfect and I got it to work. Greatly appreciated!!


$FileName = '*.xml'  #you can use wildcards here for name and for extension
$searchinfolder = $ADBackup + "\GPOs"
$Report = Get-ChildItem -Path $searchinfolder -Filter $Filename -Recurse | %{$_.FullName}
$Report


     Foreach ($UserGPO in $Report) {
        # REM GPGuid is the value between the { } brackets from $UserGPO
        [regex]$rxGuid = '(?<={).*(?=})'
        # REM GPKey is the after } but does not include the last \ on the right or data after, basically chop off \Groups.xml
        [regex]$rxKey = '(?<=}\\)(.*)(?:(?![^\\])\\)'
        $GPGuid = $rxGuid.Matches($UserGPO).value
	$GPKeyPrefix = "HKCU\"
        $GPKey = $rxKey.Matches($UserGPO).groups[1].value
        $GPOGUIDS = (Get-Content -Path 'C:\ADBackup\GPOGUIDs.csv' | Where-Object {$_.gpoGuid -eq $GPGuid})
        $GPName = (Import-csv 'C:\ADBackup\GPOGUIDs.csv' | where {$_.gpoGuid -eq $GPGuid}).DisplayName
	$GPName = "GPOTest"		###Remove this line later using temp value here
	$GPValueName = "Temp"		###Read these values from XML file###
	$GPType = "String"		###Read these values from XML file###
	$GPValue = "ABC123"		###Read these values from XML file###

        $Test = "Set-GPRegistryValue -Name $GPName"
        $Test = $Test + " -Key $GPKeyPrefix$GPKey"
        $Test = $Test + " -ValueName $GPValueName"
        $Test = $Test + " -Type $GPType"
        $Test = $Test + " -Value $GPValue"
        Write-Host "$Test"

     }

Output:
Set-GPRegistryValue -Name GPOTest -Key HKCU\User\Preferences\Drives -ValueName Temp -Type String -Value ABC123

Sorry about the XML before. I wasn’t able to upload it. I manually formatted it this time around and hope it’s useful.

<?xml version="1.0" encoding="UTF-8"?>
   <Drives clsid="{8FDDCC1A-0C3C-43cd-A6B4-71A6DF20DA8C}">
      <Drive clsid="{935D1B74-9CB8-4e3c-9914-7DD559B7A417}" bypassErrors="1" uid="{73F410F7-7539-49E0-A284-E2FEBCE334CD}" changed="2021-05-10 05:34:52" image="0" status="F:" name="F:">
         <Properties letter="F" useLetter="1" persistent="1" label="" path="\\phcfs01\fapps" userName="" allDrives="NOCHANGE" thisDrive="SHOW" action="C"/>
         </Drive>
      <Drive clsid="{935D1B74-9CB8-4e3c-9914-7DD559B7A417}" uid="{94F985A1-1E6E-4ECD-AAF2-57E7592D149D}" changed="2021-05-10 05:35:32" image="0" status="Q:" name="Q:">
         <Properties letter="Q" useLetter="1" persistent="1" label="" path="\\phcfs01\nsd" userName="" allDrives="NOCHANGE" thisDrive="SHOW" action="C"/>
      </Drive>
      <Drive clsid="{935D1B74-9CB8-4e3c-9914-7DD559B7A417}" uid="{D38E3EFB-29E2-4CA2-B21C-E70752581020}" changed="2021-05-10 05:36:06" image="0" status="R:" name="R:">
         <Properties letter="R" useLetter="1" persistent="1" label="" path="\\phcfs01\rapps" userName="" allDrives="NOCHANGE" thisDrive="SHOW" action="C"/>
      </Drive>
   </Drives>

Even if I import the values from the XML file I’m not sure how to convert it to something the USER GPO will take. It appears USER GPO settings are stored in HKCU in the registry. However, the value DRIVE as in Drive Mappings ends up in HKCU\Network. Please refer to below copy of registry keys. I’ve been using installed Poweshell module GPRegistryPolicy to get most of this working. Computer GPO settings export to CSV and can be imported on a different system easily, but User is still not working.

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Network]

[HKEY_CURRENT_USER\Network\f]
“RemotePath”="\\phcfs01\fapps"
“UserName”=dword:00000000
“ProviderName”=“Microsoft Windows Network”
“ProviderType”=dword:00020000
“ConnectionType”=dword:00000001
“ConnectFlags”=dword:00000000
“DeferFlags”=dword:00000004

[HKEY_CURRENT_USER\Network\q]
“RemotePath”="\\phcfs01\nsd"
“UserName”=dword:00000000
“ProviderName”=“Microsoft Windows Network”
“ProviderType”=dword:00020000
“ConnectionType”=dword:00000001
“ConnectFlags”=dword:00000000
“DeferFlags”=dword:00000004

[HKEY_CURRENT_USER\Network\r]
“RemotePath”="\\phcfs01\rapps"
“UserName”=dword:00000000
“ProviderName”=“Microsoft Windows Network”
“ProviderType”=dword:00020000
“ConnectionType”=dword:00000001
“ConnectFlags”=dword:00000000
“DeferFlags”=dword:00000004

You might explain what you actually want to do. Not the way you think you should go to accomplish what you want - the bigger picture. :wink: There might be a better way or at least we might be able to tell you when you’re on the wrong path. :wink:

Hi Olaf,

The ultimate goal is to be able to export Computer and User GPO settings so they can be imported in to a different system. Different system means a new Active Directory Domain that is independent of the source domain. I have certain system settings like Event Log sizes or firewall exceptions that I used to deploy by hand. I’ve completed Computer GPO’s, where I can export them to a CSV file and easily import them to a new system. It’s essentially automation so I don’t make a mistake entering it manually. User GPO’s are more difficult. For the time being I’ve created a ZIP file of the \domain\sysvol\domain\policies*.xml including the full path so I can at some point import the data in to the GPO. I’m attaching a screen shot of a GPO User Configuration settings where it maps an F: drive and Q: drive.

It feels a little bit like you’re re-inventing the wheel again. Most of the times someone else already accomplished what you need and shared it with the world. You just have to find the right source …

I googled “Export GPO Import GPO” and found some promissing sites: