Search code examples
powershellmicrosoft-distributed-file-systemnetapp

text manipulation with powershell


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


Solution

  • 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.