【问题标题】:Powershell - cut domain name from FQDNPowershell - 从 FQDN 中剪切域名
【发布时间】:2023-03-20 23:23:01
【问题描述】:

我想列出所有集合中的所有会话主机,转换为字符串。我得到了这个:

$SHs = (Get-RDSessionCollection -ConnectionBroker $CB | Select-Object -Property collectionname | ForEach-Object  -Process { Get-RDSessionHost -ConnectionBroker $CB -CollectionName $_.collectionname }) |select SessionHost  |Out-String

输出:

SessionHost                
-----------
SH1.contoso.com
SH2.contoso.com
SH3.contoso.com

现在我想从 fqdn 中删除域名 .contoso.com。

foreach ($SH in $SHs){ 

$SH = $SH.Substring(0,$SH.Length-12)
$SH
}

但输出仍然:

SessionHost                
-----------
SH1.contoso.com
SH2.contoso.com
SH3.contoso.com

期望的输出是:

SH1
SH2
SH3

编辑:我尝试了以下代码(感谢帮助):

$SH = ($SH -split "\.")[0]

$SH = ($SH -split '\.' | Select -first 1) -join '.'

我只收到:

SH01

感谢您的帮助!

【问题讨论】:

  • 不可重现,$Vmname 未定义。
  • 对不起,我忘了编辑这个变量

标签: string powershell cut


【解决方案1】:

如果没有终端服务器来测试它应该可以实际工作:

$SHs = (
    Get-RDSessionCollection -ConnectionBroker $CB | 
        Select-Object -Property collectionname | 
            ForEach-Object -Process { 
                Get-RDSessionHost -ConnectionBroker $CB -CollectionName $_.collectionname 
            }
    ) | 
    Select-Object -Property SessionHost 

foreach ($SH in $SHs.SessionHost) { 
    ($SH -split "\.")[0]
}

【讨论】:

  • 是的……终于。 ;-) :-D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 1970-01-01
  • 2016-09-21
  • 1970-01-01
  • 2010-09-20
  • 2013-11-23
相关资源
最近更新 更多