前言
在工作中,不管是邮件管理员还是系统管理员如果经常遇到重复性的工作去操作,我想大多数人第一次还可以,第二次的话就会想能不能写个脚本来解决?智能化一点不要那么费劲,于是才会有脚本。
用powershell脚本来解决重复性的问题
- 用脚本远程登录exchange服务器
#定义登录脚本的用户名及密码
#注意脚本密码为明文,请注意密码安全
$username = "用户名"
$password = ConvertTo-SecureString "密码" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $password)
# 登录到远程主机
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<ServerFQDN>/PowerShell/ -Authentication Kerberos -Credential $cred
Import-PSSession $Session -DisableNameChecking
注意需要修改你的:用户名、密码、http://<ServerFQDN>/PowerShell/里面的地址或者是IP都可以。
使用远程 PowerShell 连接到 Exchange 服务器 | Microsoft Learn
- 使用脚本来判断某个网段的IP地址情况
#查询服务器是否通畅
#定义检查地址列表
$filePath = "D:\file.txt"
$addresses = Get-Content -Path $filePath
foreach ($address in $addresses) {
$result = Test-Connection -ComputerName $address -Count 1 -Quiet
if ($result) {
Write-Host "地址 $address 可达."
} else {
Write-Host "地址 $address 不可达."
}
}
注意:列表里写你所需要的计算机名称,每次测试一次返回结果,当然你可以加大次数值。
- 邮箱管理员根据需要查询用户的基本信息
#批量查询用户的mail账号信息
function mailuser{
$emails = Get-Content "D:\mailuser.txt"
foreach ($email in $emails) {
Get-MailboxStatistics $email | Select-Object DisplayName,TotalItemSize,totaldeleteditemsize,LastLogonTime
}
} mailuser |Export-Csv -Encoding Utf8 -NoTypeInformation D:\mailboxinfo-2023.csv
注意:这个查询用户姓名、邮箱的大小、删除垃圾箱大小、最后登录邮箱的日期等。
- 远程登录服务器上查询磁盘空间
#定义登录脚本的用户名及密码
#注意脚本密码为明文,请注意密码安全
$username = "contoso\admin"
$password = ConvertTo-SecureString "pa55w.rd@admin" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $password)
# 登录到远程主机
$session = New-PSSession -ComputerName Backup -Credential $cred
# 查看本机磁盘状态
Invoke-Command -Session $session -ScriptBlock {
Get-Volume
#Get-Volume -DriveLetter D
#Get-Volume | Select-Object DriveLetter, FileSystemLabel, Size, UsedSpace, DriveType
}
- 远程登录服务器上查询某个磁盘上文件夹的空间
#定义登录脚本的用户名及密码
#注意脚本密码为明文,请注意密码安全
$username = "contoso\admin"
$password = ConvertTo-SecureString "pa55w.rd@admin" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $password)
# 登录到远程主机
$session = New-PSSession -ComputerName Backup -Credential $cred
Invoke-Command -Session $session -ScriptBlock {
$startFolder = "D:"
$colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
{
$subFolderItems = (Get-ChildItem $i.FullName -recurse | Measure-Object -property length -sum)
$FileSize="{0:N2}" -f ($subFolderItems.sum / 1TB)
$Unit='TB'
if($FileSize -lt 1)
{
$FileSize="{0:N2}" -f ($subFolderItems.sum / 1GB)
$Unit='GB'
}
write-host $i.FullName ' -- ' $FileSize $Unit -fore green
}
}
- 安装完一台新操作系统(Windows server )时需要的重复操作动作
#安装完新机器后,自动化关闭一些基础参数
#定义登录脚本的用户名及密码
#注意脚本密码为明文,请注意密码安全
$username = "contoso\admin"
$password = ConvertTo-SecureString "Aa123456" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $password)
# 登录到远程主机
$session = New-PSSession -ComputerName <远程主机名> -Credential $cred
# 查看本机磁盘状态
Invoke-Command -Session $session -ScriptBlock {
Get-Volume | Select-Object DriveLetter, FileSystemLabel, Size, UsedSpace, DriveType
}
# 关闭 Windows 防火墙
Invoke-Command -Session $session -ScriptBlock {
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False
}
# 关闭 UAC
Invoke-Command -Session $session -ScriptBlock {
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System" -Name "ConsentPromptBehaviorAdmin" -Value 0
}
# 关闭 IE 增强安全配置
Invoke-Command -Session $session -ScriptBlock {
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}" -Name "IsInstalled" -Value 0
}
注意:用户名、密码、远程主机名都需要更改,本脚本功能是查看磁盘状态、关闭防火墙、关闭UAC、关闭IE增强安全配置。
- 在UG授权不够用的情况下,你也可以进行如下操作。
#UG 授权服务不够用的情况下,你可以在3分钟内刷新一下服务达到使用的记录
#还需要在计划任务重配合使用,来调用这个脚本,来达到刷新授权的功能
#请使用Get-Service命令查询一下具体的命令
$serviceName = "Siemens PLM License Server"
$serviceStatus = Get-Service -Name $serviceName
if ($serviceStatus.Status -eq "Stopped") {
Write-Host "Starting $serviceName service..."
Start-Service -Name $serviceName
} else {
Write-Host "$serviceName service is already running."
}
配合一下计划任务来使用。
目前能想到就是这些了,有了在补充一下,有点记忆,方便自己。