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
到以上站点下载此脚本。
Powershell 十个常见任务的更多相关文章
- Java学习之道:Java中十个常见的违规编码
近期,我给Java项目做了一次代码清理工作.经过清理后,我发现一组常见的违规代码(指不规范的代码并不表示代码错误)反复出如今代码中.因此,我把常见的这些违规编码总结成一份列表,分享给大家以帮助Java ...
- Java中十个常见的违规编码
摘要:作者Veera Sundar在清理代码工作时发现一些常见的违规编码,因此,Veera Sundar把针对常见的一些违规编码总结成一份列表,以便帮助Java爱好者提高代码的质量和可维护性. 最近, ...
- 十个常见的Java异常出现原因
异常是 Java 程序中经常遇到的问题,我想每一个 Java 程序员都讨厌异常,一 个异常就是一个 BUG,就要花很多时间来定位异常问题. 1.NullPointerException 空指针异常,操 ...
- Python三十个常见的脚本汇总
1.冒泡排序 2.计算x的n次方的方法 这里有我自己整理了一套最新的python系统学习教程,包括从基础的python脚本到web开发.爬虫.数据分析.数据可视化.机器学习等.送给正在 ...
- 2.PowerShell概述
PowerShell PowerShell命令窗一般随系统带着,运行->输入:powershell,即可打开命令窗口. 命令 Powershell有诸多命令,兼容cmd命令 语法和命令 在此我推 ...
- sass教程汇总
Sass @at-root http://www.w3cplus.com/preprocessor/Sass-3-3-new-feature-at-root-bem.html Sass中连体符(&am ...
- Sass介绍及入门教程
Sass是什么? Sass是"Syntactically Awesome StyleSheets"的简称.那么他是什么?其实没有必要太过于纠结,只要知道他是“CSS预处理器”中的一 ...
- 《Linux就该这么学》第十八天课程
1.使用MariaDB数据库管理系统 今天没什么笔记,就不发了.想深入学习的可以前往原创地址:https://www.linuxprobe.com/chapter-18.html 图18-1 Mari ...
- Sass带来的变革_sass, scss 教程_w3cplus - Google Chrome
Sass带来的变革 作者:大漠 日期:2014-11-17 点击:5291 sass scss 接触Sass差不多有一个年头了,在这一年来的时间中,也花了不少心思在Sass的学习上.同时也让自己喜欢上 ...
随机推荐
- Codeigniter的Redis使用
1. ./config/redis.php: <?php $config['redis_host'] = '127.0.0.1'; $config['redis_port'] = '6379'; ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- “Operation is not valid due to the current state of the object.”
将Repeater单页显示的2000条数据一次性提交的时候出现这个错误: Operation is not valid due to the current state of the object. ...
- configuration error-could not load file or assembly crystaldecisions.reportappserver.clientdoc
IIS启动网站后报错: configuration error Could not load file or assembly 'crystaldecisions.reportappserver.cl ...
- 优化一个奇葩表设计上的全表扫描SQL
之前在一个比较繁忙的系统抓到的耗时长.消耗CPU多的一条SQL,如下:SELECT * FROM Z_VISU_DATA_ALARM_LOG TWHERE TO_DATE(T.T_TIMESTR, ' ...
- winform快速开发平台 -> 快速绑定ComboBox数据控件
通常我们在处理编辑窗体时.往往会遇到数据绑定.例如combobox控件绑定数据字典可能是我们经常用到的.然而在我的winform快速开发平台中我是如何处理这个频繁的操作呢? 首先,我们要绑定combo ...
- <HTML>菜鸟入门基础须知
将持续更新-- 一,基础常用标签and属性 既然要学习这门知识,那必须得先知道这是什么能做什么,HTML:是一种超文本标记语言,什么意思呢,我拆开看一下,超(超链接)文本(犹如TXT)标记(改变成自己 ...
- Vue - 自定义指令
1.Vue.directive(id,definition)注册一个全局自定义指令,接收两个参数,指令ID以及定义对象 2.钩子函数:将作用域与DOM进行链接,链接函数用来创建可以操作DOM的指令 b ...
- LeetCode 104. Maximum Depth of Binary Tree
Problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...
- 北京电子科技学院(BESTI)实验报告5
北京电子科技学院(BESTI)实验报告5 课程: 信息安全系统设计基础 班级:1452.1453 姓名:(按贡献大小排名) 郑凯杰.周恩德 学号:(按贡献大小排名) 20145314.20145217 ...