现在的位置: 首页 > 综合 > 正文

关于携程几道Powershell面试试题的分析!

2014年02月16日 ⁄ 综合 ⁄ 共 3267字 ⁄ 字号 评论关闭



1. 查找D盘下所有的bat文件,文件里面含有xxx@163.com的,将结果写入到result_20131010164624.txt


结果类似于


D:\DB_BAK\AA.BAT


D:\DB_BAK\CC\CC.BAT


脚本如下:


#This Script is useful for list the Character that list in file content
#执行方式如下:
#.\filesearch.ps1 -searchstring "寻找字符" -SearchLocation "寻找路径"
param
(
[string] $searchstring,
[string] $SearchLocation
)
Set-Location $SearchLocation
#转到POWERSHELLL 执行路径
$Filename=Get-ChildItem *.* -include *.txt -Recurse |Select-String -Pattern $searchstring|select filename
#采用递归方式获取当前文件中有SearchString的文件
$Logfile="Result_"+(get-date).tostring("yyyyMMddhhmmss")+".csv"
#生成日志文件名字
$Filename |Export-Csv -Path $Logfile -Encoding default
#将结果导出为日志

2. 查找D盘下所有的bat文件,文件中含有xxx@163.com的,请返回


文件的完整路径+行数(注:一行中有多个xxx@163.com字符串的算一行)


结果类似:


D:\db_bak\aa.bat    含指定的字符串xxx@163.com”10


D:\db_bak\cc\cc.bat    含指定的字符串xxxa@163.com”20


 脚本如下:



#This Script is useful for list the Character that list in file content
#执行方式如下:
#.\filesearch.ps1 -searchstring "寻找字符" -SearchLocation "寻找路径"
param
(
[string] $searchstring,
[string] $SearchLocation
)
Set-Location $SearchLocation
#转到POWERSHELLL 执行路径

$Logfile="Result_"+(get-date).tostring("yyyyMMddhhmmss")+".csv"
#获取当前的生成日志文件名称
$filegroups=Get-ChildItem  -include *.txt -Recurse|Select-String -Pattern $searchstring|Group-Object -Property:path|select name,count
#分组获取当前有相关的patten的参数的文件及文件拥有的行数
$filepropertys=@()
#定义文件属性数组为空
foreach($filegroup in $filegroups)
#根据查询到的文件进行轮询
{
$Filename=$filegroup.name
#得出文件的路径
$filecount="含指定的字符串"+$searchstring+"    "+$filegroup.count+"行"
#告知字符串在指定文件中的哪一行
$fileproperty=New-Object psobject
#新建属性对象
$fileproperty|Add-member -MemberType NoteProperty -Name "Filename" -Value $Filename
#为对象新建列名
$fileproperty|Add-Member -MemberType NoteProperty  -Name "Count" -Value $filecount
#为对象新建列名
$filepropertys=$filepropertys+$fileproperty
#将字符串数组队列进行自动累计
}


$filepropertys |Export-Csv -Path $Logfile -Encoding default
#将文件对象导出问日志文件


3. 查找D盘下所有的bat文件,文件中含有xxxx@163.com的,请返回


文件的完整路径+ 指定字符串的个数


结果类似:


D:\db_bak\aa.bat      含指定的字符串xxx@163.com 12


D:\db_bak\cc\cc.bat    含指定的字符串xxx@163.com”22


 脚本如下:


#执行方式如下:.\filecountstring.ps1 -searchstring 寻找的字符串  -SearchLocation 寻找文件的目录
param
(
[regex] $searchstring,
[string] $SearchLocation
)
Set-Location $SearchLocation
#转到POWERSHELLL 执行路径

$Logfile="Result_"+(get-date).tostring("yyyyMMddhhmmss")+".csv"
#获取当前的生成日志文件名称
$filetotals=(Get-ChildItem -Include *.txt -Recurse).fullname

#获取文件及文件路径
$fileproperty=@()

#定义属性为空
foreach($filetotal in $filetotals)

#根据日志查询
{
$FileALLP=New-Object psobject

#新建PS对象
$filecontent=Get-Content -Path $filetotal

#获取的对象的目录文本
$filecount=(Select-String -Pattern $searchstring -InputObject $filecontent -AllMatches).matches.count

#根据寻找的字符串统计字符串个数

$filecontentstring="含指定的字符串"+$searchstring+"   "+$filecount+"个"

#求出日志格式
$FileALLP |Add-Member -MemberType NoteProperty -Name "文件路径" -Value $filetotal

#添加对象的文件路径属性
$FileALLP |Add-Member -MemberType NoteProperty -Name "拥有字符串数量" -Value $filecontentstring

#添加拥有字符串数量属性

$fileproperty=$fileproperty+$FileALLP

#将对象进行数组累加
}

$fileproperty | Export-Csv -encoding default -Path $Logfile

将日志导出为相关的文本文件


4. 查找D盘下所有的bat文件,文件中含有xxx@163.com的,


将xxx@163.com替换为 xxx@163.com


param
(
[regex] $searchstring,

[regex] $replcestring,
[string] $SearchLocation
)
Set-Location $SearchLocation
#转到POWERSHELLL 执行路径

$Logfile="Result_"+(get-date).tostring("yyyyMMddhhmmss")+".csv"
#获取当前的生成日志文件名称
$filetotals=(Get-ChildItem -Include *.txt -Recurse).fullname
foreach($filetotal in $filetotals)
{
$filecontent=Get-Content -Path $filetotal
$filecontent -replace $searchstring,$replcestring |Set-Content $filetotal

}

抱歉!评论已关闭.