powershell - 带和不带引号的Powershell奇数返回DFSR卷路径

标签 powershell microsoft-distributed-file-system

首先在这里发布!

我正在寻求改善对Windows 2008 R2上DFS复制的监视,因此无法访问2012年及更高版本可用的更好的PS cmdlet。我遇到了一个奇怪的问题,想知道如何摆脱它,好吗?我有点PS新手,所以希望解决方案足够容易。 :-)

我使用WMI查询DFSR VolumeInfo(请参阅ref),因此:

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

现在,如果返回时不带引号,然后带引号:

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



后者也给出了VolumeGuid

该类为VolumePath提供以下属性

VolumePath数据类型:字符串
访问类型:只读
限定词:DisplayName
(“复制组GUID”)卷路径格式;这是\\.\C:\\?\volume{GUID}

无论如何,是否有引号而不是GUID返回VolumePath,即C:,D:等?

输出需要更易于理解,因此我将用引号将输出回显,即"$DFSVolumeInfo.VolumePath on $DFSServer has a state of $DFSVolumeInfo.State"

最佳答案

您可以将变量直接嵌入到这样的字符串中:"$Variable",但是如果您需要更复杂的语句,即使仅访问属性,它也将仅嵌入变量名称(.及其之后嵌入字符串中的所有内容将是按字面解释)。您有几种选择:

子表达

"Here's a thing: $($DFSVolumeInfo.VolumePath) <-- that's a thing"
$()中的所有内容都首先被评估,然后插入到字符串中。它可能是整个powershell程序。

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

拆开并将它们加在一起。

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

使字符串成为格式表达式,然后填写您传递给运算符右侧的值。

预填充变量
$computedValue = $DFSVolumeInfo.VolumePath
"Here's a thing: $computedValue <-- that's a thing"

关于powershell - 带和不带引号的Powershell奇数返回DFSR卷路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32612048/

相关文章:

powershell - 检查远程计算机中的 WinRM 服务

windows - 从命令行界面获取系统信息

hadoop - 从Java主类访问MR作业输出

使用 Windows DFS 复制进行 SQLite 备份

hadoop - 如何在hadoop dfs中检查群集的完整路径

Powershell:当文件名包含字符 [ ] 时,移动项目不起作用

powershell - 删除字符串中给定变量之前的所有内容

powershell - PowerShell Pscx Expand-Archive方法故障检测

linux - linux 上 fuse 文件系统的输入/输出错误?

apache - 需要有关在 Apache Mesos 上设置 Apache Hadoop 的帮助