Powershell example 4

BulkCopy.ps1

#BulkCopy.ps1
Set-Location "C:/Logs"
$files=Get-ChildItem |where {$_.extension -eq ".log"}
foreach ($file in $files) {
 $filename=($file.FullName).ToString()
 $arr=@($filename.split("."))
 $newname=$arr[0]+".old"
 Write-Host "copying "$file.Fullname "to"$newname
 copy $file.fullname $newname -force
}

 

BulkRename.ps1

#BulkRename.ps1
Set-Location "C:/Logs"
$files=get-childitem -recurse |where {$_.extension -eq ".Log"}
foreach ($file in $files) {
 $filename=($file.name).ToString()
 $arr=@($filename.split("."))
 $newname=$arr[0]+".old"
 Write-Host "renaming"$file.Fullname "to"$newname
 ren $file.fullname $newname -force
}

ListWMIProperties.ps1

#ListWMIProperties.ps1
$class=Read-Host "Enter a Win32 WMI Class that you are interested in"
$var=get-WMIObject -class $class -namespace "root/Cimv2"
$properties=$var | get-member -membertype Property
Write-Host "Properties for "$Class.ToUpper()
foreach ($property in $properties) {$property.name}

ListWMIValues.ps1

#ListWMIValues.ps1
$class=Read-Host "Enter a Win32 WMI Class that you are interested in"
$var=get-WMIObject -class $class -namespace "root/CimV2"

$properties=$var | get-member -membertype Property
Write-Host -foregroundcolor "Yellow" "Properties for "$Class.ToUpper()
# if more than one instance was returned then $var will be an array
# and we need to loop through it
$i=0

if ($var.Count -ge 2) {
 do {
  foreach ($property in $properties) {
  #only display values that aren't null and don't display system
  #properties that start with __
   if ($var[$i].($property.name) -ne $Null -AND `
$property.name -notlike "__*") {
   write-Host -foregroundcolor "Green" `
$property.name"="$var[$i].($property.name)
   }
  }
  Write-Host "***********************************"
#divider between instances
 $i++
 }while($i -le ($var.count-1))
}
# else $var has only one instance
else {
  foreach ($property in $properties) {
   if ($var.($property.name) -ne $Null -AND `
$property.name -notlike "__*") {
   write-Host -foregroundcolor "Green" `
$property.name"="$var.($property.name)
   }
  }
}

 

 

WMIReport.ps1

#WMIReport.ps1
$OS=Get-WmiObject -class win32_operatingsystem `
| Select-Object Caption,CSDVersion
#select fixed drives only by specifying a drive type of 3
$Drives=Get-WmiObject -class win32_logicaldisk | `
where {$_.DriveType -eq 3}
Write-Host "Operating System:" $OS.Caption $OS.CSDVersion
#Write-Host `n
Write-Host "Drive Summary:"
write-Host "Drive"`t"Size (MB)"`t"FreeSpace (MB)"
foreach ($d in $Drives) {
 $free="{0:N2}" -f ($d.FreeSpace/1048576)
 $size="{0:N0}" -f ($d.size/1048576)
 Write-Host $d.deviceID `t $size `t $free
}

 

GetOwnerReport.ps1

#GetOwnerReport
$report="C:/OwnerReport.csv"
$StartingDir=Read-Host "What directory do you want to start at?"
Get-ChildItem $StartingDir -recurse |Get-Acl `
| select Path,Owner | Export-Csv $report -NoTypeInformation
#send two beeps when report is finished
write-Host `a `a `n"Report finished. See "$report

ChangeACL.ps1

#ChangeACL.ps1
$Right="FullControl"

#The possible values for Rights are
# ListDirectory
# ReadData
# WriteData
# CreateFiles
# CreateDirectories
# AppendData
# ReadExtendedAttributes
# WriteExtendedAttributes
# Traverse
# ExecuteFile
# DeleteSubdirectoriesAndFiles
# ReadAttributes
# WriteAttributes
# Write
# Delete
# ReadPermissions
# Read
# ReadAndExecute

# Modify
# ChangePermissions
# TakeOwnership
# Synchronize
# FullControl

$StartingDir=Read-Host " What directory do you want to start at?"
$Principal=Read-Host " What security principal do you want to grant" `
"$Right to? `n Use format domain/username or domain/group"

#define a new access rule
#the $rule line has been artificially broken for print purposes
#It needs to be one line. The online version of the script is properly
#formatted.
$rule=new-object System.Security.AccessControl.FileSystemAccessRule
($Principal,$Right,"Allow")

foreach ($file in $(Get-ChildItem $StartingDir -recurse)) {
  $acl=get-acl $file.FullName
  #display filename and old permissions
  write-Host -foregroundcolor Yellow $file.FullName
  #uncomment if you want to see old permissions
  #write-Host $acl.AccessToString `n

  #Add this access rule to the ACL
  $acl.SetAccessRule($rule)

  #Write the changes to the object
  set-acl $File.Fullname $acl

  #display new permissions
  $acl=get-acl $file.FullName
  Write-Host -foregroundcolor Green "New Permissions"
  Write-Host $acl.AccessToString `n
}
时间: 2024-04-26 03:55:39

Powershell example 4的相关文章

使用AppVeyor CI 和PowerShell部署应用概述

开头语 关于如何为单一的ASP.NET web应用程序设置持续集成,你可以找到很多文章.这些文章都写到如何通过Web Deploy来构建完美的环境来部署简单.只需稍作修改VS.NET模板的web应用程序.任何东西在这一完美环境下都能顺利进行. 但是,真正部署应用程序的话却并非易事.总是有问题不断出现在以下情况中:当需要在注册表(Registry)或自定义文件夹中配置设置,或者你需要部署到Web集群时. 本文中,我们通过使用PowerShell远程处理(PowerShell remoting)和A

超越PowerShell PowerGUI使用心得

我们知道PowerShell将成为cmd的继任者,微软已经将其集成到Windows Server 2008和Windows 7中.不知道大家有没有注意到,在Windows 7中除了PowerShell之外,还有一个名为PowerGUI的工具,从名称上看其实它就是基于图形用户界面的PowerShell,是一款第三方PowerShell增强软件.当然,也不仅仅如此.因为PowerGUI提供了高效率的NET风格的脚本开发环境,而且也便于脚本的调试,这对于一个PowerShell脚本开发者来说,使用Po

如何利用PowerShell分析SharePoint WebApplication体系结构

之前一篇文章<两张图看清SharePoint 2013 Farm 逻辑体系结构>谈到Web Application,Content Database,Site Collection的关系.有了这个逻辑结构图之后,这篇文章将使用PowerShell,来更加直观的展现SharePoint WebApplication的体系结构. SharePoint WebApplication Structure 从上图可以看出,一个WebApplication可以包含多个Content Database,可以

PowerShell如何批量签入SharePoint Document Library中的文件

由于某个文档库设置了编辑前签出功能,导致批量导入文件时这些文件默认的状态都被签出了.如果手动签入则费时费力,故利用PowerShell来实现批量签入Document Library中的文件. Resolution Add-PSSnapin Microsoft.SharePoint.PowerShell function CheckInDocument([string]$url){ $spWeb=Get-SPWeb $url $spDocument=$spWeb.Lists.TryGetList(

如何通过PowerShell获取Site Collection下被签出的文件

由于权限的设置,当文件被签出时导致别人不可见了,这对校验文件个数的人来说着实是件烦恼的事.幸好利用PowerShell,可以获取Site Collection下被签出的文件. Resolution Add-PSSnapin Microsoft.SharePoint.PowerShell function GetAllCheckOutFiles([string]$siteUrl){ $spSite=Get-SPSite $siteUrl $spSite.AllWebs|%{ $_.Lists|wh

如何通过Windows 8 Powershell创建USB引导盘

大家可能已经比较熟悉了制作Windows USB启动盘进行操作系统引导和安装的方法,该方法一般需要第三方的工具例如UltraISO,Windows 7 USB Tool或者RM等加载安装ISO映像文件,拷贝安装Binary然后再用Diskpart磁盘管理工具封装启动分区等. 现在Windows8中,USB的启动盘制作更加简单,可以不再需要第三方工具或者Diskpart磁盘分区工具了,Windows 8本身就可以加载ISO或VHD(虚拟磁盘). 过去,采用的方式流程为:Diskpart分区,Lis

为 windows 服务器安装 PowerShell 管理库

最近在摸索一种命令方式备份Hyper-V里的虚拟机,因为平时备份都是手动关闭VM,然后Export,这样浪费很多的时间,于是折腾了下,想用命令实现自动化完成VM的备份. 查阅了下资料,看到了一个开源的软件能够用此Powershell Management Library里的命令完成大部分的虚拟机管理操作,在这里分享此网站: http://pshyperv.codeplex.com/ 但是在Import 此Module时出现如下错误: Import-Module : There were erro

2008R2core模式下安装PowerShell[为企业部署Windows Server 2008系列十一]

windows server 2008 R2版本发布一段时间了,再三斟酌后还是决定将2008R2的应用也写进本次2008 主题中来. 很多朋友喜欢core模式,甚至喜欢命令行,那么安装了core模式的你,再给core模式 的2008R2安装上PowerShell 那真是将命令行发挥到极致了(帅呆了!!). 对,我也是这样想 的! 那么下面我们一起来看看如何在core模式下安装PowerShell: 第一步:用下面的命 令列出当前服务器的功能和状态: DISM /Online /Get-Featu

精通Windows Server 2008多元密码策略之PowerShell篇

前言 在上两篇文章<精通Windows Server 2008 多元密码策略之ADSIEDIT篇>和<精通Windows Server 2008 多元密码策略之LDIFDE篇>中我向大家介绍了如何通过ADSIEDIT工具.活动目录用户和计算机管理单元和LDIFDE命令行工具创建.管理密码设置对象PSO.在这篇文章中,我将向大家展示如何使用Quese公司出品的针对AD管理的PowerShell来实现.管理多元密码策略. 按照惯例,为了让大家在操作的时候有一个清晰的思路,我将主要的操作

Windows PowerShell的12项功能

解答PowerShell:PowerShell原来的开发代号是Monad,原计划是用以替代Windows中的命令行工具,但是后来微软说它只是技术上的扩充.使用PowerShell,管理员可以做任何在图形界面下所做的事情.Windows PowerShell 1.0可以运行在Windows XP SP2.Windows Server 2003和Windows Vista上. 1. 内置Cmdlets (即"commandlets") Windows PowerShell中的所有Cmdle