【问题标题】:Read Computer Names from Text File and Execute a Command从文本文件中读取计算机名称并执行命令
【发布时间】:2015-04-22 15:51:13
【问题描述】:

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

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

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

它的作用是提示技术人员输入主机名并给出结果。如何让脚本提示技术人员输入文本文件的路径,然后让脚本给出该文本文件的结果列表?

【问题讨论】:

    标签: powershell


    【解决方案1】:
    $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."
    }
    

    【讨论】:

    • 谢谢你,成功了。现在我正在尝试将数据输出到 txt 文件并且它部分工作,但是,它只写入我的计算机列表中第一个条目的第一个结果。编辑:尝试添加代码
    • $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" | Out-File -filepath "c:\users\isjko1\Bitlocker-Status.txt" } Else { Write-Host("$Computer appears to be offline.") } } } Else { Write-Error "The text file was not found, check the path." }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    相关资源
    最近更新 更多