Ps—导出:sql作业配合ps导出csv文件

本文介绍使用PowerShell脚本实现按月倒数第二天自动导出CSV报表的方法,通过比较当前日期与目标日期来触发报表生成任务。
 1 $dateText=Get-Date #获取当前日期时间
 2 $dateText = $dateText.ToShortDateString()  #转为短日期格式(去掉时间部分)
 3 $checkDate=(Get-Date 0).AddYears((Get-Date).Year - 1).AddMonths((Get-Date).Month).AddDays(-2).ToShortDateString()#当月倒数第2天,转为短日期格式(去掉时间部分)
 4 if ($dateText -eq $checkDate)
 5 {
 6     $file = Get-ChildItem  -name * -include $dateText
 7     if ( $file.count -eq $null )  # 井号是注释符, $null是预定义的变量,代表空
 8     {
 9         new-item -path D:/wwwroot/iot_cn/reportfiles -name $dateText -type directory 
10     }
11     $today='D:\wwwroot\iot_cn\reportfiles\'+$dateText+'\query.csv' # D:\wwwroot\iot_cn\reportfiles
12     Invoke-Sqlcmd -Query "use ManuLife; exec sp_consignment_pro_details " -QueryTimeout 3 | Export-Csv $today -NoTypeInformation -Encoding Default
13 }

注:工作中一些小需求,需要用作业定时导出csv报表(频率不高),写windows服务感觉太麻烦,在网上搜索,偶然间看到了一些有关“PowerShell”的博文,其间收获颇丰,顿时油然而生-》“有此强大的工具,岂有不用之理”。

       还有其他的办法导出么,不要吝啬哦?

转载于:https://www.cnblogs.com/HonkerXin/p/10599245.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值