【问题标题】:Powershell Oddity Returning DFSR Volumepath with and without QuotesPowershell Oddity 返回带和不带引号的 DFSR 卷路径
【发布时间】:2015-09-16 23:08:36
【问题描述】:

第一次发帖!

我正在寻求改进对 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}

是否有返回 VolumePath,即引号内的 C:、D: 等,而不是 GUID?

输出需要更具人类可读性,因此我将在输出中回显带引号,即"$DFSVolumeInfo.VolumePath on $DFSServer has a state of $DFSVolumeInfo.State"

【问题讨论】:

    标签: powershell microsoft-distributed-file-system


    【解决方案1】:

    您可以像这样直接在字符串中嵌入变量:"$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"
    

    【讨论】:

    • 谢谢布赖恩,这对我的 Nagios 检查有很大帮助:-)
    猜你喜欢
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多