测试是否启用了 powershell 远程处理
Test if powershell remoting is enabled
我一直在寻找但无法找到我正在寻找的具体内容,我需要一种方法来测试大约 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 { }
}
}
您可以使用 Test-WSMan cmdlet 检查远程计算机上的 WinRM 服务是否 运行。
[bool](Test-WSMan -ComputerName 'ComputerName' -ErrorAction SilentlyContinue)
冒着跑题的风险...
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
}
根据上面的 Straffs 代码,这是我如何确定 PS 远程处理是否在一台或多台机器上工作的工作版本。保存代码并将其与名为 "PCList.txt" 运行 脚本的文件放在同一文件夹中,它将显示列表中每个系统的状态,并在每次 运行 .CSV格式直接点击进入excel。在 PCList.txt 文件中每行放置一台机器。您必须具有对远程计算机的本地管理员访问权限才能正常工作,始终 "Run As Administrator"
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
}
我一直在寻找但无法找到我正在寻找的具体内容,我需要一种方法来测试大约 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 { }
}
}
您可以使用 Test-WSMan cmdlet 检查远程计算机上的 WinRM 服务是否 运行。
[bool](Test-WSMan -ComputerName 'ComputerName' -ErrorAction SilentlyContinue)
冒着跑题的风险...
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
}
根据上面的 Straffs 代码,这是我如何确定 PS 远程处理是否在一台或多台机器上工作的工作版本。保存代码并将其与名为 "PCList.txt" 运行 脚本的文件放在同一文件夹中,它将显示列表中每个系统的状态,并在每次 运行 .CSV格式直接点击进入excel。在 PCList.txt 文件中每行放置一台机器。您必须具有对远程计算机的本地管理员访问权限才能正常工作,始终 "Run As Administrator"
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
}