powershell 文本对象化和正则表达式的高级应用

本文深入探讨了如何在PowerShell中利用文本对象化和正则表达式进行高效的数据处理。通过实例展示了如何更新Base_User表中的数据,包括设置字段值、筛选和替换特定文本,以及在控制台获取和展示结果的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#在vs调试时发现数据库语句错误,但又不知道具体错误,此时需要找到执行语句来查看数据库的具体报错
#在vs中通过debug获取的对象信息为文本模式
$list='Account	"185xxxxxxxx"	string								
		AuditDateTime	null	System.DateTime?
		AuditStatus	null	string
		AuditUserId	null	string
		AuditUserName	null	string
		BankName	null	string
		Birthday	null	System.DateTime?
		CardId	"230302xxxxxxxx4011"	string						
		ChangePasswordDate	null	System.DateTime?
		Code	"GT1805140001"	string								
		Commission	null	int?
		CompanyId	"090f2a23-d641-4c01-bee9-dc5f80691935"	string  
		CreateDate	null	System.DateTime?
		CreateUserId	null	string	
		CreateUserName	null	string
		CreditID	" "	string								
		CreditName	" "	string								
		DeleteMark	null	int?
		DepartmentId	null	string
		Email	" "	string									
		Enabled	1	int?											
		FirstVisit	null	System.DateTime?
		Gender	"男"	string										
		InnerUser	0	int?										
		LastVisit	null	System.DateTime?
		LogOnCount	null	int?
		Mobile	"185xxxxxxxx"	string						
		ModifyDate	2018/5/15 15:45:36	System.DateTime?	
		ModifyUserId	"System"	string					
		ModifyUserName	"超级管理员"	string				
		OICQ	null	string
		Online	null	int?
		OpenId	null	int?
		Password	null	string
		PreviousVisit	null	System.DateTime?
		RealName	"kay"	string									
		Remark	" "	string									
		Secretkey	null	string
		SortCode	null	int?
		Spell	"请选择"	string									
		Telephone	" "	string								
		UserId	"29F31DE3-002E-44F2-9C6B-F40AC9F99D2F"	string   	
		emailstate	null	string									
		imgurl1	null	string
		imgurl2	null	string
		mAddress	" "	string								
		mamount	null	int?
		mheader	null	string
		sponsor	" "	string									
		weixin	"12345678"	string'									
#通过mssql监控程序获得的代码为带变量的文本
$sql='Update Base_User
Set CompanyId=@CompanyId,
InnerUser=@InnerUser,
Code=@Code,
Account=@Account,
RealName=@RealName,
Spell=@Spell,
Gender=@Gender,
Mobile=@Mobile,
Telephone=@Telephone,
Email=@Email,
Remark=@Remark,
Enabled=@Enabled,
ModifyDate=@ModifyDate,
ModifyUserId=@ModifyUserId,
ModifyUserName=@ModifyUserName,
CardId=@CardId,
weixin=@weixin,
mAddress=@mAddress,
sponsor=@sponsor,
CreditID=@CreditID,
CreditName=@CreditName 
Where UserId=@UserId'

#将第一个文本转换为带name,value,type属性的列表
$itemlist=New-Object System.Collections.ArrayList
foreach($item in $list.Split("`n")){
if($item){
$item=$item.split('',[StringSplitOptions]::RemoveEmptyEntries)
$obj=New-Object psobject|select name,value,type
$obj.name=$item[0] #为属性赋值
$obj.value=$item[1]
$obj.type=$item[2]
[void]$itemlist.add($obj)#加入到列表
}
}

#通过正则表达式替换变量为数值
[regex]::Replace($sql,'@\w+',{param($str)
$find=$str.tostring().TrimStart("@")
$item=$itemlist|?{$_.name -eq $find}
if($item.type -notmatch "int"){
"'"+$item.value.replace('"',$null)+"'" } #如果不是数字类型,去掉双引号,补上单引号,不直接替换是因为日期不带双引号但也需要单引号
else{$item.value}
})

在控制台获得的结果:

Update Base_User
Set CompanyId='090f2a23-d641-4c01-bee9-dc5f80691935',
InnerUser=0,
Code='GT1805140001',
Account='185xxxxxxxx',
RealName='kay',
Spell='请选择',
Gender='男',
Mobile='185xxxxxxxx',
Telephone=' ',
Email=' ',
Remark=' ',
Enabled=1,
ModifyDate='2018/5/15',
ModifyUserId='System',
ModifyUserName='超级管理员',
CardId='230302xxxxxxxx4011',
weixin='12345678',
mAddress=' ',
sponsor=' ',
CreditID=' ',
CreditName=' ' 
Where UserId='29F31DE3-002E-44F2-9C6B-F40AC9F99D2F'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值