Microsoft

测试Powershell远程管理是否打开

2022年11月20日

1.批量模式

$computers = Get-Content E:\IP.txt
$report = Foreach ($comp in $computers) {
    if (Test-Connection -ComputerName $comp -Count 1 -Quiet) {
        $service = Get-WmiObject -ComputerName $comp -Class Win32_Service -Filter "Name='WinRM'"
        $Service.StartService()
        $AppStateResult = invoke-command $comp -scriptblock {
        $package = "*Microsoft.VP9VideoExtensions*" 
        {
            if (Get-AppxPackage -AllUsers $package) {
                Write-Output "Success"
            }
            else {
                try {
                    Get-AppxPackage -AllUsers *Microsoft.VP9VideoExtensions* | Remove-AppxPackage -AllUsers -ErrorAction Stop write-output = "Success"
                }
                catch {
                    $error[0].exception.message
                }
            }
    }
        [pscustomobject]@{  
            ComputerName = $comp
            AppXState   = $AppStateResult
            Status  = '可访问'
        }
    }
    else {
        [pscustomobject]@{  
            ComputerName = $comp
            AppXState   = 'n/a'
            Status  = '无法访问'
        }
    }
 }
}
$report | export-csv ".\report.csv" -NoTypeInformation

2.简易模式

Function Get-RDP-Status  {
$ComputerName = Get-Content E:\IP.txt -ReadCount 0
foreach ($Computer in $ComputerName) 
{
Test-NetConnection -cn $Computer WINRM | Select-Object ComputerName,RemotePort,PingSucceeded,TcpTestSucceeded |
FT -AutoSize   
}
 }
Get-RDP-Status

3.域控模式

Import-Module ActiveDirectory

$ou = "OU=Servers,DC=Domain,DC=lo"

$servers = Get-ADComputer -Filter * -SearchBase $ou | select-object -expandproperty name

Foreach ($server in $servers){

    $Data = Get-Service -ServiceName *WinRm* -ComputerName $server | select machinename,name | sort machinename | format-table -AutoSize 

    Write($Data) | Out-File .\WebServices.txt -Append

}

相关连接

https://stackoverflow.com/questions/12516574/get-service-on-multiple-remote-machines

https://community.spiceworks.com/topic/2318837-powershell-script-to-select-multiple-computers-and-output-a-success-or-fail

https://social.technet.microsoft.com/wiki/contents/articles/29563.powershell-get-winrm-status-of-multiple-computers.aspx

You Might Also Like

No Comments

Leave a Reply