web-dev-qa-db-ja.com

PowerShellリモート処理が有効になっているかどうかをテストします

私は探していましたが、探しているものを具体的に見つけることができません。約900台のマシンをテストして、PowerShellリモート処理が有効になっているかどうかを確認する方法が必要です。以下のスクリプトを使用して、PowerShellを確認できることがわかりました。インストールされていますが、実際にマシンをリモートで管理できるかどうかはチェックされていません。何かアイデアはありますか?

function Check-PSVersion
{

[CmdletBinding()]
Param (
    [Parameter(Mandatory = $true)]
    $ComputerName
)
if (Test-Path $ComputerName)
{
    $Computers = Get-Content $ComputerName
}
Else
{
    $Computers = $ComputerName
}

Foreach ($Computer in $Computers)
{
    Try
    {
        Write-Host "Checking Computer $Computer"
        $path = "\\$Computer\C$\windows\System32\WindowsPowerShell\v1.0\powershell.exe"
        if (test-path $path) { (ls $path).VersionInfo }
        else
        {
            Write-Host "Powershell isn't installed" -ForegroundColor 'Red'
        }
        Write-Host "Finished Checking Computer $Computer"
    }

    catch { }
}
}
8
Luke

Test-WSManコマンドレットを使用して、WinRMサービスがリモートコンピューターで実行されているかどうかを確認できます。

[bool](Test-WSMan -ComputerName 'ComputerName' -ErrorAction SilentlyContinue)
16
ojk

トピックから外れるリスクがあります...

function server_available?( $_server_to_act_on ) {
   if ( Test-Connection -ComputerName $_server_to_act_on -Count 1 -Quiet ) {
     Write-Host "`nTest-Connection: $_server_to_act_on OK" -NoNewline
     if ( [bool](Test-WSMan -ComputerName $_server_to_act_on -ErrorAction SilentlyContinue) ) {
       Write-Host ", Test-WSMan: $_server_to_act_on OK" -NoNewline
       if ( [bool](Invoke-Command -ComputerName $_server_to_act_on -ScriptBlock {"hello from $env:COMPUTERNAME"} -ErrorAction SilentlyContinue) ) {
         Write-Host ", Invoke-Command: $_server_to_act_on OK" -NoNewline
         return $true
       }
     }
  }
  return $false
}
1
Straff

上記のStraffsコードから作業して、これはPSRemotingが1つ以上のマシンで動作しているかどうかを判断する方法の私の作業バージョンです。コードを保存し、「PCList.txt」という名前のファイルと同じフォルダーに配置します。スクリプトを実行すると、リスト内の各システムのステータスが表示され、直接クリック可能な.CSV形式で実行するたびに新しいレポートが生成されます。 Excelに。 PCList.txtファイルの1行に1台のマシンを配置します。これを機能させるには、リモートマシンへのローカル管理者アクセス権が必要です。常に「管理者として実行」してください。

function Test-CanRemoteSystetmRunPSCmd( $_server_to_act_on ) 
{

  Write-Host "`n`n$Counter of $Total - Testing System:`t$_server_to_act_on`n"
  if ( Test-Connection -ComputerName $_server_to_act_on -Count 1 -Quiet ) 
  {
    Write-Host "`nTest-Connection: $_server_to_act_on OK, " -NoNewline
    if ( [bool](Test-WSMan -ComputerName $_server_to_act_on -ErrorAction SilentlyContinue) ) 
    {
      Write-Host "Test-WSMan: $_server_to_act_on OK, " -NoNewline
      if ( [bool](Invoke-Command -ComputerName $_server_to_act_on -ScriptBlock {"hello from $env:COMPUTERNAME"} -ErrorAction SilentlyContinue) ) 
      {
        Write-Host "Invoke-Command: $_server_to_act_on OK. `n"  
        return "$_server_to_act_on,SUCCESS"
      }
      Else
      {

        Return "$_server_to_act_on,FAILED Invoke-Command"
        Continue 

      }
    }
    Else
    {

      Return "$_server_to_act_on,FAILED Test-WSMAN"
      Continue 

    }
  }
  Else
  {

    Return "$_server_to_act_on,FAILED Test-Connection"
    Continue 

  }  
}

Clear-Host
# Intialze the report name  and header anchored in the current script folder 
$_NewRunTime    = Get-Date                                                              # Initialize the time of the script being run
$_LastRunTime   = $_NewRunTime
$_Year          = $_NewRunTime.Year
$_Month         = $_NewRunTime.Month
$_Day           = $_NewRunTime.Day
$_Hour          = $_NewRunTime.Hour
$_Min           = $_NewRunTime.Minute
$_Sec           = $_NewRunTime.Second
$_Report_DateTime_Header = "$_Year.$_Month.$_Day.$_Hour.$_Min.$_Sec"
$_report_File = "$PSScriptRoot" + '\' + "$_Report_DateTime_Header"  + '_PSRemotingStatusReport.csv'
$_ReportHeader = "Computer Name,PS Remoting Status,Test Time"
 Out-File -FilePath $_report_File -InputObject $_ReportHeader -Encoding UTF8  # Add a line to the report

$PCList = Get-Content -Path "$PSScriptRoot\PCList.txt"
$Total = $PCList.Count
$Counter = 0

ForEach ($PC in $PCList)
{
  $Counter = $Counter + 1
 $T = Measure-Command {$RemoteCommandCheck = Test-CanRemoteSystetmRunPSCmd $PC}

 $TestTimeTotal = $T.Totalseconds
  If($RemoteCommandCheck -like "*Fail*")
  {

    Write-Host "$RemoteCommandCheck,$TestTimeTotal" -ForegroundColor Red -BackgroundColor Yellow

  }
  Else
  {

    Write-Host "$RemoteCommandCheck,$TestTimeTotal" -ForegroundColor White -BackgroundColor DarkGreen

  }
  $RemoteCommandCheck + ',' + $TestTimeTotal | Out-File -FilePath $_report_File -Encoding UTF8 -Append
}
0
DeployGuy