text manipulation with powershell

152 Views Asked by At

I'm trying to format the output of this command to get only the dfsnamespace only like that :

\\F-TYPHON\DATA13\AI-Project

I can not use the Get-DfsnFolderTarget cmdlet because the RSAT-DFS-Mgmt-Con is not installed on all servers and I cannot install it .

$DFSPath="\\F-TYPHON\shared\AI-Project"

PS C:\> dfsutil client property state $DFSPath

Active, Online      \\F-TYPHON\DATA13\AI-Project

Done processing this command.

I've tried this .

PS C:\> $dfs=dfsutil client property state $DFSPath

PS C:\> $dfs.trimstart("Active, Online")

Method invocation failed because [System.Object[]] doesn't contain a method named 'trimstart'. At line:1 char:15 + $dfs.trimstart <<<< ("Active, Online") + CategoryInfo : InvalidOperation: (trimstart:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

any help will be apreciated I can list all volume data for the filer but there's many incoherence in the structure so I need only to list the shared folder under "shared" on a filer and then procces it with dfsutil to get the absolut path

2

There are 2 best solutions below

0
AudioBubble On BEST ANSWER

Use a regular expression to match the text output of dfsutil:

$DFSPath="\\F-TYPHON\shared\AI-Project"

if ((dfsutil client property state $DFSPath) -match "(?<=\s{2,})\\\\.*"){
   $DFSNameSpace = $Matches.Value
}
  • here (?<=\s{2,})\\\\.* matches two or more whitespace \s in a lookbehind
    followed by two (escaped) backslashes and the remainder of the line.
0
Wissem Rekaya On

thank you for your help i've found a solution that i can use in a way it's not 100% powershell but i can use it to extract the output in a format that i can use it in a loop and than make automated robocopylines with powershell

here the code for the output that i found

$DFSPath="\F-TYPHON\shared\AI-Project"

$dfspath=(dfsutil client property state $DFSPath |findstr /i \F-TYPHON)|out-string

$dfs=$dfspath.Trimstart("Active, Online ")

write-host $dfs the output is

\F-TYPHON\DATA13\AI-Project

it's certainly not the best but i can work with it

if you have any others suggestion you're welcome