0

文字列から削除したいlocalhostのですが、次のコマンドが機能しません。アイデアはありますか?

オプション1:

[string[]]$Servers = '"localhost","tbhserver"'
$Servers = $servers | Where-Object {$_ -ne "localhost"}

オプション 2:

[string[]]$Servers = '"localhost","tbhserver"'
$Servers 
[System.Collections.ArrayList]$servers = $servers
$servers.Remove("localhost")
4

2 に答える 2

2

2 つのリストではなく、1 つの文字列 '"localhost","tbhserver"' のみを $Servers に割り当てているように見えます。

これは機能しますか?

$Servers = @("localhost", "tbhserver")
$Servers = $Servers | Where-Object {$_ -ne "localhost"}

現在 Windows マシンを使用していないため、これをテストすることはできません。

于 2012-04-08T14:58:36.797 に答える
1

-ne パラメータも使用できます。

$servers = 'localhost', 'tbhserver'
$servers = $servers -ne 'localhost'
于 2012-04-08T16:03:06.763 に答える