服务器 频道

Windows Server 2008:PowerShell技巧

 【IT168资讯】使用PowerShell,管理员可以做任何在图形界面下所做的事情。

 1.内置Cmdlets(即"commandlets")

 WindowsPowerShell中的所有Cmdlets都允许这样的动名词:get-service,get-process,stop-service,get-wmiobject.

 2.强大的通配符和操作对象

 要得到以w开头的服务及其依赖服务只要输入:

 PS>get-servicew*|format-listDisplayName,DependentServices

 3.在犯错误前测试命令

 WindowsPowerShell有意向独特的功能:Whatif,可以不执行命令就告诉你命令执行结果.如:

 PS>stop-servicew3*-whatif

 4.获取副本

 PowerShell可以开始和结束所有命令的副本,可以在脚本中轻易测试命令并同时保存.

 PS>Start-Transcript-Pathc:demodfoshow.txt

 PS>Stop-Transcript

 5.从命令行发布Windows对话

 因为WindowsPowerShell位对象而优化,可以向.NETFramework一样从命令行访问COM对象,下列命令告诉你的Vista机器发表"WindowsVistaandPowerShell"字串.

 PS>$spVoice=new-object-com"SAPI.spvoice"

 PS>$spVoice.Speak("WindowsVistaandPowerShell")

 6.使用WindowsPowerShell访问诸如WindowsMediaPlayer11等的应用程序

 PS>$band="ThePosies"

 PS>$player=New-object-comwmplayer.ocx

 PS>$playlist=$player.mediacollection.getbyauthor($band)

 PS>$player.openPlayer($playlist.item(0).sourceurl)

 7.WindowsPowerShell作为命令行存储计算器

 PowerShell可以完成基本的计算工作

 PS>2*2

 不过,WindowsPowerShell也可以快速解决存储问题,例如,备份11GB的数据需要多少个700MB的CD.

 PS>11gb/700mb

 那么,需要多少个1000GB的存储来备份每个320GB,共425个的Vista桌面呢?

 PS>(320gb*425)/1000GB

 8.PowerShell用作日历计算

 计算多少天到新年:

 PS>([DateTime]"1/1/2007"-[datetime]::now).days

 9.返回机器上某种类型文件的数量

 WindowsVista有许多类型的事件记录和组策略文件等.下列命令是返回当前目录及其子目录中VBScript,Bat和PowerShell脚本的数量:

 PS>dir-include*.vbs,*.ps1,*.bat-recurse|group-objectextension-noelement

 10.从命令行收集WindowsSystemAssessmentTool数据

 PS>get-wmiobjectwin32_winsat|format-table__SERVER,*SCORE-autosize

 PS>get-wmiobjectwin32_winsat|select*score|out-chart-Title"SystemAssessmentScoresbyPowerGadgets"

 11.配置UAC(UserAccountControl)

 PS>set-itemproperty-pathHKLM:SOFTWAREMICROSOFTWINDOWSCurrentVersionPoliciesSystem-nameConsentPromptBehaviorAdmin-value0

 12.管理BitLocker

 PS>$drives=get-wmiobject-namespacerootCIMv2SecurityMicrosoftVolumeEncryption-classWin32_EncryptableVolume

 PS>$drives|format-tableDriveLetter,PersistentVolumeID-autosize

 PS>$BitLockDrive=$drives[0]

 PS>$BitLockDrive.GetProtectionStatus()

 PS>$BitLockDrive.DisableKeyProtectors()

 PS>$BitLockDrive.EnableKeyProtectors()

 

0
相关文章