首页 > 解决方案 > 脚本调整包括域中的所有计算机

问题描述

我有一个脚本,它扫描域中的给定计算机以识别并禁用 Windows 10 中的移动热点功能。脚本正常工作,但我想扫描我的所有域计算机,不仅指定.. 谁能帮我调整这个脚本?

$username = "domain\administrator"
$password = "Your password"
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $password
$computers = @("nr1", "nr2", "nr3")
foreach($computer in $computers){
    $hotspot = Invoke-Command -ComputerName $computer -credential $credential -scriptblock {
    $hotspot = Get-Service "icssvc"
    if($hotspot.Status -eq "Running"){
        Write-Host "Hotspot is turned on on $computer" -ForegroundColor Red
        try{
            Stop-Service "icssvc"
            Write-Host "Successfully stopped service on $computer" -ForegroundColor Green
        }catch{
            Write-Host "Unable to stop service on $computer" -ForegroundColor Red
        }
    }else{
        Write-Host "No Hotspot running on $computer" -ForegroundColor Green
    }
}

标签: powershell

解决方案


如果您替换为以下内容$computers = @("nr1", "nr2", "nr3")

Import-Module ActiveDirectory
$computers = Get-ADComputer -Properties DNSHostName

那应该返回一个主机名数组。您可能需要通过 提供凭据,如果您需要排除任何机器-Credential,您可以得到结果。-Filter

Get-ADComputer 请参阅此处的文档和示例。


推荐阅读