powershell - 脚本调整包括域中的所有计算机
问题描述
我有一个脚本,它扫描域中的给定计算机以识别并禁用 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
}
}
解决方案
如果您替换为以下内容$computers = @("nr1", "nr2", "nr3")
:
Import-Module ActiveDirectory
$computers = Get-ADComputer -Properties DNSHostName
那应该返回一个主机名数组。您可能需要通过 提供凭据,如果您需要排除任何机器-Credential
,您可以得到结果。-Filter
Get-ADComputer
请参阅此处的文档和示例。
推荐阅读
- wordpress - WordPress:插件上传有问题
- node.js - Sequelize - 如果 allowNull === false 并且值为 null 则运行验证
- java - 在 MATLAB 中从 java .class 运行 main 并附带 .jar 编解码器文件
- gitlab - 如何从私人 gitlab 存储库创建公开发布?
- plot - Julia 中的 3D 散点图
- java - Groovy 日期格式
- python - 使用没有神经网络的预训练词向量进行文本分类
- sql - BigQuery:118% 的峰值使用资源在查询执行期间超过,在分析 over() 中使用最多
- nativescript - 清单合并错误 - Nativescript 5.1
- orgchart - 是否可以将 onClick 事件添加到 nodeMenuButton?