powershell seach const variable

来源:互联网 发布:q房网经纪人端口下载 编辑:程序博客网 时间:2024/06/11 15:40
##search const


1>.declare some const , save to E:\1.txt


public const string a = "1";public const string b = "2";public const string c = "3";public const string d = "4";




2>.save codes into 1.ps1


#########################################
#Summary:Seach const varible by file name
#########################################




function SCByFile($fn){#define regex$regex = "\s+const\s+(?<type>.*)\w?\s+(?<name>.*)\s+=\s+(?<value>.*);"#define object$obj = new-object psobject #this part may be only work in 3.0#New-Object PSObject - Property @{#FileName = ""#DataType = ""#Name = ""#Value = ""#}$obj | add-member -membertype noteproperty -value "" -name FileName$obj | add-member -membertype noteproperty -value "" -name DataType$obj | add-member -membertype noteproperty -value "" -name Name$obj | add-member -membertype noteproperty -value "" -name Value#search by regular expression patternselect-string $regex $fn |ForEach {ForEach ($match in $_.Matches){$obj.Value = $match.Groups["value"].Value$obj.Name = $match.Groups["name"].Value$obj.DataType = $match.Groups["type"].Value$obj.FileName = $fn#populate obj $obj}}}





############################################
#Summary : Search Const Varible by Directory
############################################
function SCByDir($d){$files = dir $dForEach($file in $files){SCByFile($file)}}


3>.
a>.import module
import-module D:\1.ps1


b>.run


SCByFile("E:\1.txt");


or

SCByDir("E:\");



原创粉丝点击