|
ここでは、PowerShell Get-BrowseMaster Function のサンプルを紹介します。
- NBTSTAT -a $Env:ComputerName
次の事例を実行してみよう。
#!/usr/bin/pwsh
# http://www.computerperformance.co.uk/powershell/powershell_function_browsemaster.htm
NBTSTAT -a $Env:ComputerName
環境変数 ComputerName をパラメータとして、NBTSTAT -a を実行します。
それでは、実行してみましょう。下記のような結果が得られます。
NBTSTAT : The term 'NBTSTAT' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At /xxxx/PSUX-Support/PS_ref_07/067_PS_BrowseMaster_01.ps1:4 char:1
+ NBTSTAT -a $Env:ComputerName
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (NBTSTAT:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、NBTSTAT コマンドがサポートされていない。
[ 目次 ]
- $Discovery = NBTStat -a $Env:ComputerName | Out-String -Stream | Select-String "MSBrowse"
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell checks if local machine is the Windows Browse Master
# Clear-Host
$Discovery = NBTStat -a $Env:ComputerName |
Out-String -Stream | Select-String "MSBrowse"
If($Discovery -match "MSBrowse") {
"$Env:ComputerName is the Browse master"
}
5 〜 6 行で、環境変数 ComputerName をパラメータとして、NBTSTAT -a を実行し、結果から "MSBrowse" を選択して、$Discovery に格納します。
7 〜 9 行で、$Discovery に "MSBrowse" があると、8 行目を実行します。
それでは、実行してみましょう。下記のような結果が得られます。
NBTStat : The term 'NBTStat' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At /xxxx/PSUX-Support/PS_ref_07/067_PS_BrowseMaster_02.ps1:5 char:14
+ $Discovery = NBTStat -a $Env:ComputerName |
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (NBTStat:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、NBTSTAT コマンドがサポートされていない。
[ 目次 ]
- "^\\\\(?\S+)\s+" {$matches.Name}
次の事例を実行してみよう。
#!/usr/bin/pwsh
# Clear-Host
Switch -RegEx (NET.EXE VIEW) {
"^\\\\(?\S+)\s+" {$matches.Name}
}
NET.EXE VIEW を実行して、サーバー名一覧を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
NET.EXE : The term 'NET.EXE' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At /xxxx/PSUX-Support/PS_ref_07/067_PS_BrowseMaster_03.ps1:5 char:16
+ Switch -RegEx (NET.EXE VIEW) {
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (NET.EXE:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
3行目の正規表現は、UNIX と同じのようだ!
Linux版では、NET.EXE コマンドがサポートされていない。
[ 目次 ]
- NBTStat -a $LocalMachine | Out-String -Stream | Select-String "MSBrowse"
次の事例を実行してみよう。
#!/usr/bin/pwsh
Function Global:Get-BrowseMaster {
[cmdletbinding()]
Param(
[String]$LocalMachine ="$Env:COMPUTERNAME",
[Switch]$All
)
Begin{
Clear-Host
$x = 0
}
Process{
### Check if the local machine is the Browse Master ####
$Discovery = NBTStat -a $LocalMachine | Out-String -Stream | Select-String "MSBrowse"
If($Discovery -match "MSBrowse") {
Write-Host "$LocalMachine is the Browse master" -BackgroundColor Magenta ;$x =1
}
#### Logic to find Browse Master on the network ####
If($x -ne 1 -or $All) {
$NetworkComputer =switch -RegEx (NET.EXE VIEW) {
"^\\\\(?\S+)\s+" {$matches.Name}
}
Clear-Host
Foreach($LocalMachine in $NetworkComputer) {
$Discovery = NBTStat -a $LocalMachine | Out-String -Stream | Select-String "MSBrowse"
If($Discovery -match "MSBrowse") {
Write-Host "$LocalMachine is the Browse Master" -BackgroundColor Magenta
}
Else {
"$LocalMachine is an imposter"
}
} # End of Foreach
} # End of If
} # End of Process
} # End of Function Get-BrowseMaster
Get-BrowseMaster
Browse Master を調べる関数です。詳細な説明は、省略します。
それでは、実行してみましょう。下記のような結果が得られます。
NBTStat : The term 'NBTStat' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At /xxxx/PSUX-Support/PS_ref_07/067_PS_BrowseMaster_04.ps1:17 char:16
+ $Discovery = NBTStat -a $LocalMachine | Out-String -Stream | ...
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (NBTStat:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
NET.EXE : The term 'NET.EXE' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At /xxxx/PSUX-Support/PS_ref_07/067_PS_BrowseMaster_04.ps1:23 char:37
+ $NetworkComputer =switch -RegEx (NET.EXE VIEW) {
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (NET.EXE:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、NBTStat および NET.EXE コマンドがサポートされていない。
|