powershell PowerShell的snippets.ps1
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell PowerShell的snippets.ps1相关的知识,希望对你有一定的参考价值。
###############
# userpic.ps1 #
###############
# Used to publish new user photo to GAL
param (
[string]$sid = $(throw "-sid is required"),
[string]$photo = $(throw "-photo is required")
)
$ErrorActionPreference = "stop"
$root = [adsi]''
$searcher = New-Object System.DirectoryServices.DirectorySearcher($root)
$searcher.Filter = "(mail=$($sid)*)"
$users = $searcher.FindAll()
if($users.Count -eq 0){
throw "No matching user found"
} elseif($users.Count -eq 1){
$user = $users[0].GetDirectoryEntry()
} else{
$i = 1
foreach($user in $users){
Write-Host "$i : $($user.Path)"
$i += 1
}
$pick = Read-Host "Which user?"
$user = $users[$pick - 1].GetDirectoryEntry()
}
$file = [byte[]](Get-Content $photo -Encoding Byte)
$user.Properties["thumbnailPhoto"].Clear()
$user.Properties["thumbnailPhoto"].Add($file)
$user.commitChanges()
##############
# screen.ps1 #
##############
# Sends PrintScreen every 60 seconds
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
While ($true) {
[System.Windows.Forms.SendKeys]::SendWait("{PRTSC}")
Start-Sleep -s 30
}
#################
# filesplit.ps1 #
#################
#Usage filesplit.ps1 <filename> <# of lines>
Param(
[parameter(mandatory=$true,position=0)][string]$filename,
[parameter(position=1)][string]$linesperFile = 100000
)
$sw = new-object System.Diagnostics.Stopwatch
$sw.Start()
$rootName = (join-path ([io.fileinfo]$filename).directoryname ([io.fileinfo]$filename).basename) + '_'
$ext = ([io.fileinfo]$filename).extension
$header = get-content $filename -first 1
$filecount = 0
$reader = $null
try {
$reader = [io.file]::OpenText($filename)
try {
$filecount++
$linecount = 0
"Creating file number $filecount"
$writer = [io.file]::CreateText("{0}{1}{2}" -f ($rootName,$filecount.ToString("000"),$ext))
while($reader.EndOfStream -ne $true) {
"Reading $linesperFile lines"
if($filecount -gt 1){$writer.WriteLine($header)}
while( ($linecount -lt $linesperFile) -and ($reader.EndOfStream -ne $true)) {
$writer.WriteLine($reader.ReadLine());
$linecount++
}
if($reader.EndOfStream -ne $true) {
"Closing file"
$writer.Dispose();
$filecount++
$linecount = 0
"Creating file number $filecount"
$writer = [io.file]::CreateText("{0}{1}{2}" -f ($rootName,$filecount.ToString("000"),$ext))
}
}
} finally {
$writer.Dispose();
}
} finally {
$reader.Dispose();
}
$sw.Stop()
Write-Host "Split complete in " $sw.Elapsed.TotalSeconds "seconds"
以上是关于powershell PowerShell的snippets.ps1的主要内容,如果未能解决你的问题,请参考以下文章
powershell 测量PowerShell命令或PowerShell脚本的速度
powershell远程下载exe并执行
powershell 如何定时执行 ps1
windows powershell 怎么使用啊,是干啥用的
powershell报错?
powershell PowerShell:启动PowerShell作为其他用户提升