有用的 PowerShell 脚本 |了解最有用的 PowerShell 脚本列表
PowerShell 脚本简介
在本文中,我们将了解有用的 PowerShell 脚本。对于任何用户或管理员来说,每天都需要执行某些任务。还有一些任务更容易完成并且无需人工干预即可完成。为了节省用户时间,可以借助 PowerShell 脚本自动执行任务。这些任务可以是从某个位置下载文件、获取计算机的详细信息、删除早于某一天的文件、在一小时重新启动系统等任何任务。脚本准备好后,可以手动运行,甚至可以使用任务计划程序运行。
例如:要释放驱动器空间,我们可以编写一个 PowerShell 脚本来删除超过 90 天的文件,并安排一个每天运行的任务,以便有足够的空间保持驾驶员的空间。
有用的 PowerShell 脚本示例
下面我们举例说明如何使用 PowerShell 脚本:
用于获取与系统相关的信息的脚本
以下脚本用于获取与系统相关的重要和基本信息,例如系统型号、可用磁盘空间、BIOS 信息、处理器配置、内存详细信息、操作系统详细信息、系统用户和所有者列表、当前用户会话以及各种正在运行的进程和已安装的各种修补程序的状态。
例子#1
代码:
Write-Host "Welcome to the script of fetching computer Information"
Write-host "The BIOS Details are as follows"
Get-CimInstance -ClassName Win32_BIOS输出:
例子#2
代码:
Write-Host "The systems processor is"
Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -Property SystemType输出:
例子#3
代码:
Write-Host "The computer Manufacture and physical memory details are as follows"
Get-CimInstance -ClassName Win32_ComputerSystem输出:
例子#4
代码:
Write-Host "The installed hotfixes are"
Get-CimInstance -ClassName Win32_QuickFixEngineering
输出:
例子#5
代码:
Write-Host "The OS details are below"
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property Build*,OSType,ServicePack*输出:
例子#6
代码:
Write-Host "The following are the users and the owners"
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property *user*输出:
例子#7
代码:
Write-Host "The disk space details are as follows"
Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" |Measure-Object -Property FreeSpace,Size -Sum |Select-Object -Property Property,Sum输出:
例子#8
代码:
Write-Host "Current user logged in to the system"
Get-CimInstance -ClassName Win32_ComputerSystem -Property UserName输出:
例子#9
代码:
Write-Host "Status of the running services are as follows"
Get-CimInstance -ClassName Win32_Service | Format-Table -Property Status,Name,DisplayName -AutoSize -Wrap输出:
在 PowerShell 脚本中实现的示例
下面解释了要在 PowerShell 脚本中实现的示例:
示例#1:将用户添加到 AD
以下脚本会将 CSV 中存在的每个用户添加到 AD。 CSV 将具有需要脚本在 AD 中创建用户的所有与用户相关的属性。如果出现错误,脚本会将错误详细信息记录在文本文件中。创建成功后,控制台中将出现一条消息,提示用户已在 Active Directory 中创建。第一行用于导入 csv 文档并循环遍历每条记录。后续行用于将记录值分配给 AD 中的用户属性。最后一行用于将用户添加到 Active Directory 组。该代码包含在 try-catch 块中,以捕获任何异常,如果发生任何错误,则使用 catch 语句将其打印在控制台上。
代码:
try
{
Import-Csv “C:\test\test.csv” | ForEach-Object {
$Name = $_.Name + “test.com”
New-ADUser `
-DisplayName $_.”Dname” `
-Name $_.”Name” `
-GivenName $_.”GName” `
-Surname $_.”Sname” `
-SamAccountName $_.”Name” `
-UserPrincipalName $UPName `
-Office $_.”off” `
-EmailAddress $_.”EAddress” `
-Description $_.”Desc” `
-AccountPassword (ConvertTo-SecureString “vig@123” -AsPlainText -force) `
-ChangePasswordAtLogon $true `
-Enabled $true `
Add-ADGroupMember “OrgUsers” $_.”Name”;
Write-Host "User created and added in the AD group"
}
}
catch
{
$msge=$_.Exception.Message
Write-Host "Exception is" $msge
}CSV 文件的示例输入:
| Name | Dname | GName | Sname | UPName | off | EAddress | Desc |
| test1 | test1 | test1 | test1 | test1 | test | test1 | |
| test2 | test2 | test2 | test2 | test2 | test2 | test2 | |
| test3 | test3 | test3 | test3 | test3 | test3 | test3 | |
| test4 | test4 | test4 | test4 | test4 | test4 | test4 | |
| test5 | test5 | test5 | test5 | test5 | test5 | test5 |
示例 #2:从路径中删除超过 30 天的文件
以下脚本用于删除自当前日期起 30 天或更长时间创建的文件。要检查文件的路径在 csv 中提到。首先,导入 csv 文件。然后,对于每个路径,检查并删除超过 30 天的文件。
代码:
Write-Host "Welcome to the archive example"
$csv = Import-Csv "C:\Vignesh\test.csv"
foreach($row in $csv)
{
$Path=$row.Path
write-host "The path to be archived is" $row.Path
$DaysTOBeArchived = "-30"
$CurrentDate = Get-Date
$DatetoBeDeleted = $CurrentDate.AddDays($DaysTOBeArchived)
Get-ChildItem $Path -Recurse | Where-Object { $_.CreationTime -lt $DatetoBeDeleted } | Remove-Item
Write-Host "Cleared the files is the path "$row.path
}示例输入:
路径
C:\Vignesh\Test\Test1
C:\Vignesh\Test\Test2
C:\Vignesh\Test\Test3
C:\Vignesh\Test\Test1
C:\Vignesh\Test\Test4
C:\Vignesh\Test\Test5输出:
示例 #3:如果磁盘空间小于 5%,则发送电子邮件
以下示例将在磁盘空间低于 5% 时发送自动邮件。
代码:
$cname="Mycomputer"
ForEach ($c in $cname)
{
$disk=Get-WmiObject win32_logicaldisk -ComputerName $c -Filter "Drivetype=3" -ErrorAction SilentlyContinue | Where-Object {($_.freespace/$_.size) -le '0.05'}
If ($disk)
{
$EmailToAdd = "[email "
$EmailFromAdd = "[email "
$userdet = 'testuser'
$passworddet = "testpwd"
$Subjectdet = "Disk space alert"
$Bodydet = "low space in the system"
$SMTPServerdet = "testswer"
$SMTPMessagedet = New-Object System.Net.Mail.MailMessage($EmailFromAdd,$EmailToAdd,$Subjectdet,$Bodydet)
$SMTPClientdet = New-Object Net.Mail.SmtpClient($SMTPServerdet, 587)
$ SMTPClientdet.EnableSsl = $true
$ SMTPClientdet.Credentials = New-Object System.Net.NetworkCredential($userdet, $passworddet)
$ SMTPClientdet.Send($SMTPMessagedet)
}
}通过任务计划程序运行脚本
通过创建任务,可以每天运行上述脚本,无需用户干预。该任务将在每天的指定时间运行脚本。这种方法的优点是不会有错过脚本运行的风险,并且可以节省用户的时间。以下是通过任务计划程序运行 ps 脚本的步骤
- 打开任务计划程序并创建任务
- 在触发器选项卡中,设置何时需要运行此任务以及作业的频率,例如是否应每天运行或每小时运行以及需要运行的时间
- 在操作选项卡中,指定文件位置
- 最后,单击“确定”。
结论
因此,本文介绍了可用于自动执行任务的各种有用的脚本。脚本的优点是减少了人力和监控的需要。它还展示了如何使用任务计划程序运行脚本。脚本还可以用于在发生问题时向用户发送电子邮件或警报。要获得脚本方面的专业知识,建议编写示例程序并进行练习。










