2

First post here!

I'm seeking to improve my monitoring of DFS replication on Windows 2008 R2 and so do not have access to the nicer PS cmdlets available to 2012 and above. I've run into an oddity and wondered hwo to get out of it, please? I'm a bit of a PS-novice so am hoping the solution is easy enough. :-)

I query the DFSR VolumeInfo using WMI (see ref) so:

$DFSVolumeInfo = gwmi -Namespace "root\MicrosoftDFS" -Computer $DFSServer -query "select * from DfsrVolumeInfo"

Now, if I return without, and then with quotes:

write-host $DFSVolumeInfo.VolumePath

\\.\E: \\.\D:

write-host "$DFSVolumeInfo.VolumePath"

\\DC2-SRV-DFS01\root\MicrosoftDFS:DfsrVolumeInfo.VolumeGuid="6C4C4203-2BC9-11E4-9EF3-0050568815FA"

\\DC2-SRV-DFS01\root\MicrosoftDFS:DfsrVolumeInfo.VolumeGuid="D214D634-F794-11E3-9EF3-0050568815FA".VolumePath

The latter gives the VolumeGuid too.

The Class gives the following properties for VolumePath

VolumePath Data type: string Access type: Read-only Qualifiers: DisplayName ("Replication Group GUID") The volume path format; this is either \\.\C: or \\?\volume{GUID}.

Is there anyway to return the VolumePath, i.e. C:, D: etc inside of quotes, rather than the GUID?

The output needs to be more human-readable and so I'll be echo-ing the output with quotes i.e. "$DFSVolumeInfo.VolumePath on $DFSServer has a state of $DFSVolumeInfo.State"

briantist
  • 45,546
  • 6
  • 82
  • 127

1 Answers1

1

You can directly embed a variable in a string like this: "$Variable" but if you need a more complex statement, even if just accessing a property, it will only embed the variable name (the . and everything after, when embedded in the string will be interpreted literally). You have a few options:

Sub-expression

"Here's a thing: $($DFSVolumeInfo.VolumePath) <-- that's a thing"

Anything inside $() is evaluated first then inserted into the string. It could be an entire powershell program.

Concatenation

"Here's a thing: " + $DFSVolumeInfo.VolumePath + " <-- that's a thing"

Break it apart and add the values together.

The format -f operator

"Here's a thing: {0} <-- that's a thing" -f $DFSVolumeInfo.VolumePath

Make the string a format expression, then it fills in the values you pass to the right side of the operator.

Pre-fill a variable

$computedValue = $DFSVolumeInfo.VolumePath
"Here's a thing: $computedValue <-- that's a thing"
briantist
  • 45,546
  • 6
  • 82
  • 127