powershell to Ping,RDP,RemoteRegistry,WMI

时间:2014-07-28 09:43:48

标签: powershell powershell-remoting

我写了一个Powershell脚本,它将执行Ping,RDP(3389),远程注册表和WMI连接到远程计算机。下面是我提出的脚本。现在我想以短格式获取输出,例如:

    PING             :    SUCCESS
    RDP              :    FAIL
    Remote Registry  :    FAIL
    WMI              :    SUCCESS

    Remote Registry check has FAILED.
    RDP check has FAILED.

    PING check SUCCEEDED
    WMI check SUCCEEDED

我正在寻找帮助,因为我是Powershell脚本的新手。提前谢谢!!


Write-Host `n
$inputfromuser = Read-Host "Enter Server Name"

If($inputfromuser -like "")
{
    Write-Host "User Input cannot be blank. Please enter the server name"
    Write-Host `n
}

else 
{

    Write-Host `n
    Write-Host -ForegroundColor Yellow "CHECKING PING CONNECTIVITY TO SERVER  $inputfromuser"
    Test-Connection -ComputerName $inputfromuser -Quiet -Count 1
    Write-Host `n
    Write-Host -ForegroundColor Yellow "CHECKING RDP PORT 3389 CONNECTIVITY ...."
    Test-NetConnection -ComputerName $inputfromuser -CommonTCPPort RDP -InformationLevel Quiet
    Write-Host `n

    Write-Host -ForegroundColor Yellow "CHECKING REMOTE REGISTRY CONNECTIVITY ...."
    $regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$inputfromuser)
    $ref = $regkey.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")
    If (!$ref) {
        Write-Host -ForegroundColor Red "REMOTE REGISTRY CHECK FAILED"
    }

   Else {
        Write-Host -ForegroundColor Green "REMOTE REGISTRY CHECK SUCCESS"  
   }
   Write-Host `n

   Write-Host -ForegroundColor Yello "CHECKING REMOTE WMI CONNECTIVITY ...."
   $wmi = GWMI -Query "Select * from Win32_PingStatus where Address = '$inputfromuser'"
   If (!$wmi) {
        Write-Host -ForegroundColor Red "REMOTE WMI CHECK FAILED"
    }

   Else {
        Write-Host -ForegroundColor Green "REMOTE WMI CHECK SUCCESS"
    }
   Write-Host `n

 }

1 个答案:

答案 0 :(得分:3)

我建议将测试与输出创建分开。将检查结果分配给单独的变量:

$ping = Test-Connection ...
$rdp  = Test-NetConnection ...
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $inputfromuser)
$ref  = $regkey.OpenSubKey("...")
$wmi  = Get-WmiObject -Query "..."

创建2个哈希表:

$state_noun = @{
  $true  = 'SUCCESS'
  $false = 'FAIL'
}
$state_verb = @{
  $true  = 'SUCCEEDED'
  $false = 'FAILED'
}

并使用here-string创建输出:

$result = @"
PING             :    $($state_noun[$ping])
RDP              :    $($state_noun[$rdp])
Remote Registry  :    $($state_noun[[bool]$ref])
WMI              :    $($state_noun[[bool]$wmi])

Remote Registry check has $($state_verb[[bool]$ref]).
RDP check has $($state_verb[$rdp]).

PING check $($state_verb[$ping])
WMI check $($state_verb[[bool]$wmi])
"@

Write-Host $result

如果输出中必须有突出显示/彩色值,请使用如下自定义输出函数:

function Write-Result {
  [CmdletBinding()]
  Param(
    [Parameter()][string]$text,
    [Parameter()][bool]$value
  )

  Write-Host $text -NoNewline
  if ($value) {
    Write-Host $value -NoNewline -ForegroundColor Green
  } else {
    Write-Host $value -NoNewline -ForegroundColor Red
  }
  Write-Host '.'
}

Write-Result 'PING             :    ' $state_noun[$ping]
...
Write-Result 'Remote Registry check has ' $state_verb[[bool]$ref]
...