Powershell 十个常见任务
学习Powershell的时候,基本的语法也了解了一些,但是就是不知道要写些什么?作为一个过来者,和大家一起分享下常见的几个管理任务脚本。
1.更改本地Administrator账号密码
[ADSI]$Admin = "WinNT://计算机名/用户名"
$Admin.SetPassword(P@ssw0rd)
2.重启或关闭计算机
为了能实现对多台计算机操作,将所有要操作计算机名保存在文本文件中。
Get-Content c:\works\server.txt |
Where-Object {Test-Connection $_ -Quiet -Count 2} |
foreach {Write-Host "restart $_ "-force "Green"
Restart-Computer $_ -Force -WhatIf
}
3.重启服务
Get-Content c:\works\server.txt | Where-Object {Test-Connection $_ -Quiet -Count 2} |
Invoke-Command {Restart-Service "wuauserv" -PassThru} –ComputerName $_
4.终止进程
Invoke-Command {ps notepad |kill} -ComputerName v-pc
5.打印磁盘信息
function Get-DiskUtil {
param([string] $computername = $env:COMPUTERNAME)
process{
if($_){
$computername=$_
}
gwmi Win32_LogicalDisk -Filter "drivetype = 3" -ComputerName $computername |
select @{name="Computername";Expression={$_.systemname}},
deviceID,
@{Name="SizeGB";expression={"{0:N2}" -f ($_.size/1GB)}},
@{Name="FreeGB";expression={"{0:N2}" -f ($_.Freespace/1GB)}},
@{Name="UsedGB";expression={"{0:N2}" -f (($_.size-$_.freespace)/1GB)}},
@{Name="Perfree";expression={"{0:P2}" -f ($_.Freespace/$_.size)}}
}
}
$data = gc c:\work\server.txt |where {Test-Connection $_ -Quiet -Count 2} |Get-DiskUtil
$data | Sort-Object computername |ConvertTo-Html -Title "DISKUTIL REPORT" -CssUri "c:\sample.css" |Out-File "c:\diskrepot.html"
6.获取近10条错误系统日志
Get-EventLog -LogName System -Newest 10 -EntryType error,warning
Get-EventLog -LogName System -Newest 10 -EntryType error
Get-EventLog -LogName System -Newest 10 -EntryType error | sort source |ft -GroupBy source -Property timewritten,enventid,message
Get-EventLog -logname System -Newest 10 -EntryType error -ComputerName "v-pc1","v-pc2" |sort machinename -Property timewritten,enventid,message
7.文件访问权限
$acl = Get-Acl \\dc\share
$acl.access |where {$_identityrefernce -match "sales"}
Get-Acl \\dc\share |Export-Clixml .\chisales.xml
8.系统启动时间
function Get-Boot {
param([string]$computername = $env:COMPUTERNAME)
process{
if($_){$computername = $_}
gwmi Win32_OperatingSystem -ComputerName $computername |
select @{Name="computername";expression={$_.csname}},
@{Name="lastboottime";expression={$_.Converttodatetime($_.lastbootuptime)}},
@{Name="Uptime";expression={(Get-Date) - ($_.Converttodatetime($_.lastbootuptime))}}
}
}
9.获取系统版本信息
function Get-SP {
param([string]$computername = $env:COMPUTERNAME)
process{
if($_){$computername = $_}
gwmi Win32_OperatingSystem -ComputerName $computername |
select @{Name="computername";expression={$_.csname}},
@{Name="OperatingSystem";expression={$_.caption}},
@{Name="SPName";expression={$_.csdversion}},
@{Name="Version";expression={$_.servicepackmajorversion}}
}
}
10.删除过期文件
http://gallery.technet.microsoft.com/scriptcenter/Remove-old-files-from-053499f9
到以上站点下载此脚本。