【问题标题】:PowerShell Get Windows OS Version Fast and Do Different ThingsPowerShell 快速获取 Windows 操作系统版本并执行不同的操作
【发布时间】:2021-01-27 00:21:03
【问题描述】:

有没有更快的方法从服务器列表中获取特定的注册表值?我正在选择具有不同 Windows 风格的计算机的文本文件并获取操作系统产品名称。我发现每台计算机需要几秒钟的时间来检索。

当前脚本:

Clear-Host

# Prompt for file containing list of target
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$myDialog = New-Object System.Windows.Forms.OpenFileDialog
$myDialog.Title = "Select File of Target Systems"
$myDialog.InitialDirectory = $PSScriptRoot
$myDialog.Filter = "TXT (*.txt) | *.txt"
$result = $myDialog.ShowDialog()

If ($result -eq "OK") {
    $Computers = Get-Content $myDialog.FileName
}
Else {
    Write-Host "`nCancelled by User`n"
}

$Array = @()
  
# Loop Through Computers
ForEach ($Computer in $Computers) {
    Write-Warning "Processing $Computer"
       
    # Get Registry Values
    Try {      
        $OSVersion = Invoke-Command -ComputerName $Computer -ScriptBlock { (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName }
 
        # Create a custom object 
        $ComplexObject = New-Object PSCustomObject
        $ComplexObject | Add-Member -MemberType NoteProperty -Name "Server name" -Value $Computer
        $ComplexObject | Add-Member -MemberType NoteProperty -Name "OS Version" -Value $OSVersion
 
        # Add custom object to our array
        $Array += $ComplexObject
    }
    Catch {
        $_.Exception.Message
        Break
    }

}
 
# Results
If ($Array) {
    # Display results in new window
    $Array | Out-GridView -Title "OS Version Results"
 
    # Display results in PS console
    $Array
}

我稍后在脚本中的最终目标是根据操作系统版本做不同的事情,所以我想将它们分成独立的列表:

If (We have Win2008 servers) {
    "Do This"
}
If (We have Win2012R2 servers) {
    "Do This"
}

【问题讨论】:

  • 阅读操作系统信息from Active Directory怎么样?
  • 首先,不要循环遍历计算机,一次将它们全部传递给invoke-command。其次,不要创建一个数组然后+=,只需将输出分配给一个变量并让powershell进行收集。第三,不要使用 add-member,创建一个具有适当属性的 pscustomobject。

标签: arrays windows powershell loops pscustomobject


【解决方案1】:
Clear-Host

# Prompt for file containing list of target
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$myDialog = [System.Windows.Forms.OpenFileDialog]::new()
$myDialog.Title = "Select File of Target Systems"
$myDialog.InitialDirectory = $PSScriptRoot
$myDialog.Filter = "TXT (*.txt) | *.txt"
$result = $myDialog.ShowDialog()

If ($result -eq "OK") {
    $Computers = Get-Content $myDialog.FileName
}
Else {
    Write-Host "`nCancelled by User`n"
}

# Get Registry Values
$Array = Try {      
        Invoke-Command -ComputerName $Computers -ScriptBlock {
            (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName
    } -ErrorAction stop | Select-Object @{n="Server Name";e={$_.pscomputername}},
                                        @{n="OS Version";e={$_}}
}
Catch {
    write-warning $_.Exception.Message
    break
}

# Results
If ($Array) {
    # Display results in new window
    $Array | Out-GridView -Title "OS Version Results"

    # Display results in PS console
    $Array
}

【讨论】:

    【解决方案2】:

    您可以使用Get-AdComputer 喜欢:

    Get-ADComputer -Filter {(OperatingSystem -like "*windows*server*") -and (Enabled -eq "True")} -Properties OperatingSystem | Select -ExpandProperty OperatingSystem | ForEach {
      If($_ -match "Windows Server 2008.*"){
        # Server 2008
      }
      If($_ -match "Windows Server 2012.*"){
        # Server 2012
      }
      # Add more like 2016,2019
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-15
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      相关资源
      最近更新 更多