0

hello guys i'm a beginner in powershell , i have an outpout file from a script that i createdto check all volume data (datax) on a netapp SVM(na0maa-euc01) level3 folder who have everyone permission on it and without active directory security groups(must positionned on the level3 folders) , so the outpout txt file is like that

\\na0maa-euc01\data1\Nissan Production Engineering\TC Facility\X11M 
\\na0maa-euc01\data1\Powertrain_DIP-A entry\IQMS\Data
\\na0maa-euc01\data1\Powertrain_DIP-A entry\PROJECT INFORMATION\Data
\\na0maa-euc01\data2\powertrain_emb_ice_algorithm_OLD\Data\05_DCM

those level 3 folder where copied from a 7 mode netapp named f-typhon-old were the acl are ok

\\f-typhon-old\data1\Nissan Production Engineering.mig\TC Facility\X11M
\\f-typhon-old\data1\Powertrain_DIP-A entry.mig\IQMS\Data
\\f-typhon-old\data1\Powertrain_DIP-A entry.mig\PROJECT INFORMATION\Data
\\f-typhon-old\data2\powertrain_emb_ice_algorithm_OLD.mig\Data\05_DCM

so i created a new file with a baseline to correct the acl like that

data1\Nissan Production Engineering\TC Facility\X11M
data1\Powertrain_DIP-A entry\IQMS\Data
data1\Powertrain_DIP-A entry\PROJECT INFORMATION\Data
data2\powertrain_emb_ice_algorithm_OLD.mig\Data\05_DCM

made a little script with a loop on the file with get-acl and set-acl to correct but the issue is i need a text manipulation to create a variable $level3old to store in it the path with .mig in the level1 folders i don't now how to do it or if it was possible in powershell

$Pth = "C:\temp\mig\mig_CDOT\0INCHE\Log\Robocopy\Extract\acl_user"
$filerd = "na0maa-euc01"
$filers = "f-typhon-old"
$N3 = gc $pth\N3_witheveryoneacess_base.txt

foreach ($level3 in $N3) {
    $level3old = how too replace datax\level1\level2\level3 with datax\level1.mig\level2\level3
    get-acl "\\$filers\$level3old " | set-acl "\\$filerd\$level3"
}
1
  • 1
    $level3old = $level3 -replace '^\\\\.*?\\(.*?\\[^\\]+)','$1.mig' will replace the system name and add the .mig Commented Jan 25, 2020 at 13:02

1 Answer 1

1

I am not completely sure what your variables contain at all times. So I will provide some examples.

Assuming $level3 contains the server name and no .mig

$level3 = '\\na0maa-euc01\data1\Nissan Production Engineering\TC Facility\X11M'
$level3old = $level3 -replace '^\\\\.*?\\(.*?\\[^\\]+)','$1.mig'

# Output
$level3old
data1\Nissan Production Engineering.mig\TC Facility\X11M

Assuming $level3 starts with the shared folder and contains no .mig

$level3 = 'data1\Nissan Production Engineering\TC Facility\X11M'
$level3old = $level3 -replace '^.*?\\[^\\]+','$&.mig'

# Output
$level3old
data1\Nissan Production Engineering.mig\TC Facility\X11M

Explanation:

-replace uses regex matching followed by a string replace (with some match references). Since \ is a regex special character, it has to be backslash escaped for a literal match. So to match \, you need \\. To match \\, you need \\\\, i.e. one \ escape for each literal \.

The initial ^ matches the beginning of the string. It is the first possible place to start matching a string.

.*? lazily matches any characters. This usually continues matching until it hits another matching condition. So .*?\\ will match any character up until and including the next \.

[^] is the negation character class. Anything following the ^ within the [] means match anything except those characters. [^\\] matches anything except \.

+ matches one or more of the previous match. [^\\]+ matches anything except \ one or more times.

In the replacement string, $1 refers to the matched characters in capture group 1. In the regex portion, () denotes a capture group. The first open ( begins capture group, which is given the name 1 unless you explicitly name it.

In the replacement string, $& refers to every matched character. $0 could also be used because an entire match defaults to capture group 0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.