Powershell example 4

简介: BulkCopy.ps1 #BulkCopy.ps1 Set-Location "C:/Logs" files=Get-ChildItem |where {files=Get-ChildItem |where {_.

BulkCopy.ps1

Image from book
#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
}
Image from book

 

BulkRename.ps1

Image from book
#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
}
Image from book

ListWMIProperties.ps1

Image from book
#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}
Image from book

ListWMIValues.ps1

Image from book
#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)
   }
  }
}
Image from book

 

 

WMIReport.ps1

Image from book
#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
}
Image from book

 

GetOwnerReport.ps1

Image from book
#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
Image from book

ChangeACL.ps1

Image from book
#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
}
Image from book
目录
打赏
0
0
0
0
20
分享
相关文章
Powershell example 1
Restart-Computers.ps1 # Define input parameters param ( [string] filename=(throw "Filename is required!") ) ...
661 0
Powershell example 2
ProcessCPU.ps1 #ProcessCPU.ps1 process=getprocesslow=0 #counter for low cpu processes $med=0 #counter for medi...
832 0
Powershell example 3
Blocktest.ps1 sb = {   foreach (process in input) {process.
714 0
Powershell example 5
SetPermswithCACLS.ps1 #SetPermsWithCACLS.ps1 # CACLS rights are usually # F = FullControl # C = Change # R = Reado...
903 0
Powershell example 6
GetSecurityRSS.ps1 #GetSecurityRSS.ps1 #Query Microsoft's Basic Security Feed for latest bulletins #Critical bullet...
887 0
|
6月前
|
Powershell 重新排列去重 Windows环境变量
【9月更文挑战第13天】本文介绍如何使用PowerShell对Windows环境变量进行重新排列和去重。首先通过`$env:`访问环境变量,接着使用`-split`命令分割路径,再利用`Select-Object -Unique`去除重复项。之后可根据需要对路径进行排序,最后将处理后的路径组合并更新环境变量。注意修改环境变量前应备份重要数据并了解潜在影响。
198 10
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
210 0
|
10月前
|
windows可以安装Ubuntu,ubuntu上也可以安装Powershell
powerhsell除了可以在windows上使用外,还可以在Ubuntu上部署开发环境。下面介绍Ubuntu上安装powershell的方法。
271 0
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的
150 0

相关课程

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等