从文本文件中读取计算机名称并执行命令

时间:2015-04-22 15:51:13

标签: powershell

我有一个简单的Powershell脚本来检查网络上计算机上BitLocker驱动器加密的状态。我希望脚本能够确定文本文件中多台计算机的状态。

到目前为止,这是我的基本脚本:

$GetID = Read-Host "What is the Device ID?"
$ComputerID = manage-bde -status -cn "$GetID"
foreach ($Computer in $ComputerID) {
    Write-Host("$Computer")
}

这样做会提示技术人员输入主机名并给出结果。如何让脚本向技术人员提示文本文件的路径,然后让脚本从该文本文件中提供结果列表?

1 个答案:

答案 0 :(得分:0)

$TextFilePath = Read-Host "What is the path to the text file?"
If (Test-Path $TextFilePath){
    $ComputersArray = Get-Content $TextFilePath
    ForEach ($Computer in $ComputersArray) {
        If (Test-Connection $Computer -Count 1){
            $ComputerStatus = manage-bde -status -cn "$Computer"
            Write-Host($ComputerStatus)
        } Else {
            Write-Host("$Computer appears to be offline.")
        }
    }
} Else {
    Write-Error "The text file was not found, check the path."
}