Active Directory 的另一个主要部分是 DNS。如果您是 Windows 服务器管理员,那么您很清楚它的工作原理,但在没有知道命令实际操作的情况下,使用 Powershell 进行管理和自动化有时会变得困难。
DNS 在组织中扮演着至关重要的角色,因此需要一些窥视来确保其正常运行。在我们领域中,通过 GUI 为服务器或工作站创建 DNS 记录似乎很容易,但当需要同时创建多个 DNS 记录时就会变得困难起来。从创建记录到将其指向正确的 IP 地址都具有挑战性。但我已经接受了这一挑战,并编写了一些相关脚本,并对其进行了优化以便无需任何混乱即可运行。如果您喜欢这部分内容,则肯定也会喜欢 DHCP powershell 脚本以及我的 powershell 脚本库。
好的开始意味着良好的结束,在下面是一些例子。从恢复 DNS 服务器到在 DNS 中创建区域,我都做到了。以下是一些书籍供您参考,如果您打算学习关于 DNS 或 powershell 的知识,请查阅以下最佳可用 DNS Powershell 脚本列表。
对于DNS有用的Powershell命令
添加DNS转发器
1
Add-DnsServerForwarder-IPAddress IP -PassThru
添加根提示服务器
1
Add-DnsServerRootHint-NameServer"domain.com"-IPAddress IP
获取DNS服务器配置
1
Get-DnsServer-ComputerName"IP"
获取DNS服务器转发器设置
1
Get-DnsServerForwarder
从DNS服务器中删除转发器
1
Remove-DnsServerForwarder-IPAddress IP -PassThru
设置DNS服务器配置
1
Get-DnsServer-CimSession IP | Set-DnsServer
清除 DNS 缓存
1
Clear-DnsServerCache-ComputerName"Name of server"-Force
function [<scope:>]<name> [([type]$parameter1[,[type]$parameter2])] { param([type]$parameter1 [,[type]$parameter2]) dynamicparam {<statement list>} begin {<statement list>} process {<statement list>} end {<statement list>} }
[string] $Name ) Process { Write-Host ("Hi $Name !") write-host$Name"today is $(Get-Date)" } }
show-message
结果:
1 2 3 4 5
cmdlet show-Message at command pipeline position 1 Supply values for the following parameters: Name: Dhrub Hi Dhrub ! Dhrub today is 09/01/2021 13:41:12
# PowerShell script for monitoring system performance # Define the performance counters for CPU and memory usage $cpu = "\Processor(_Total)\% Processor Time" $memory = "\Memory\Available MBytes"
# Get the performance counter data for CPU and memory usage $data = Get-Counter-Counter$cpu,$memory-SampleInterval1-MaxSamples10
# Create a chart object from the performance counter data $chart = New-Object System.Windows.Forms.DataVisualization.Charting.Chart $chart.Width = 800 $chart.Height = 600 $chart.BackColor = "White"
# Add a chart area, a series for CPU usage, a series for memory usage, and a legend to the chart object $area = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea $area.AxisX.Title = "Time (seconds)" $area.AxisY.Title = "Usage (%)" $area.AxisY2.Title = "Available (MB)" $chart.ChartAreas.Add($area)
# Save the chart object as an image file $chart.SaveImage("C:\Performance.png","png")
使用 PowerShell 备份和恢复文件夹
PowerShell 可以帮助您轻松高效地备份和恢复 Windows 11 系统中的文件夹。您可以使用 PowerShell 执行各种操作,如创建、复制、移动、重命名、删除、搜索和压缩文件夹。您还可以使用 PowerShell 创建和使用备份策略、备份集和备份项。
1 2 3 4 5 6 7 8 9 10 11 12 13
# PowerShell script for backing up and restoring files and folders # Define the folder to backup and the backup location $folder = "C:\Users\YourName\Documents" $location = "D:\Backup"
# Create a backup policy that runs daily and keeps backups for 30 days $policy = New-BackupPolicy-Frequency Daily -RetentionPeriod30
# Set the backup policy for the computer Set-BackupPolicy-Policy$policy
# Backup the folder to the backup location Backup-File-Source$folder-Destination$location
Here is a summary of a PowerShell script that restores a file or folder from a backup to a specified location:
1 2 3 4 5 6 7 8 9 10 11 12 13
# Define the file or folder to restore and the restore location $file = "C:\Users\YourName\Documents\Report.docx" $location = "C:\Users\YourName\Desktop"
# Get the latest backup set that contains the file or folder $set = Get-BackupSet | Sort-Object-Property CreationTime -Descending | Select-Object-First1
# Get the backup item that matches the file or folder $item = Get-BackupItem-BackupSet$set-Path$file
# Restore the file or folder to the restore location Restore-File-BackupItem$item-Destination$location
使用 PowerShell 安装和更新 Windows 功能
PowerShell 可以帮助您轻松高效地在 Windows 11 系统上安装和更新 Windows 功能。您可以使用 PowerShell 执行各种操作,如列出、启用、禁用或更新诸如 Hyper-V、Windows 子系统 for Linux 或 Windows 沙盒等 Windows 功能。