• PowerShell+NetApi 批处理执行程序


    基于CMD 编写批处理程序很反人类,此篇文章介绍利用Power Shell脚本(借鉴明经net版主雪山飞狐的脚本改造而成)处理之前的Bat+scr的过程(https://www.cnblogs.com/NanShengBlogs/p/10957489.html


    Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能。与com对象无缝对接,可随时条用

    1# 条用com对象

    COM就像没落的贵族一样,虽然过气,但那个范儿还在。作为遗产,它们还在放光发热。PowerShell没有嫌弃它,而是选择与他们兼容。这也让现在依旧战斗在Native C++的程序员颇为欣慰。PowerShell对COM的访问方式是通过其ProgID来进行。示例如下:

    $ie = New-Object -ComObject Excel.Application
    $ie.Visible = $true

    2# 条用dotnet framework对象

    Add-Type -AssemblyName mscorlib
    $str New-Object System.Text.StringBuilder
    $str.Append("Hello")
    $str.ToString()
    3# 改造之前的bat+scr 合并为Powershell脚本
     1 # 控制台程序路径
     2 $key = Get-Item HKLM:SoftwareAutodeskHardcopy
     3 $arr = @()
     4 foreach($value in $key.Property)
     5 {
     6     $arr += Get-Item HKLM:Software$value
     7 }
     8 
     9 $arr = $arr | sort
    10 for($i=0;$i -lt $arr.Count;$i++)
    11 {
    12     $_values = Get-ItemProperty $arr[$i].PSPath
    13     $s = "{0}.{1}" -f ($i+1),$_values.ProductName
    14     [Console]::WriteLine($s)
    15 }
    16 $n = Read-Host "请选择AutoCad版本"
    17 
    18 
    19 $_values = Get-ItemProperty $arr[[int]$n - 1].PSPath
    20 
    21 
    22 $MyConsole = $_values.Location + "accoreconsole.exe"
    23 
    24 # 设置进程启动信息
    25 $psi= New-Object System.Diagnostics.ProcessStartInfo
    26 
    27 $psi.FileName = "cmd.exe"
    28 
    29 # 设置进程自动重定向输入
    30 $psi.UseShellExecute = $false
    31 $psi.CreateNoWindow=$true
    32 $psi.RedirectStandardInput = $true
    33 $process = New-Object System.Diagnostics.Process
    34 $process.StartInfo = $psi
    35 
    36 #获取当前ps1文件所在的文件夹
    37 
    38 $ScriptPath=Split-Path -Parent $MyInvocation.MyCommand.Definition
    39 
    40 
    41 $dllFileName=$ScriptPath+"ShopDrawing.dll"
    42 
    43 $files=[System.IO.Directory]::GetFiles($ScriptPath,"*.dwg")
    44 
    45 foreach ($item in $files)
    46 {
    47     
    48     [System.IO.FileInfo] $fi=New-Object System.IO.FileInfo($item)
    49     if (!$fi.IsReadOnly){
    50      $process.Start()
    51      $process.StandardInput.WriteLine("""$MyConsole"" "+"/i"+" ""$item""")    
    52      $process.StandardInput.WriteLine("SECURELOAD")
    53      $process.StandardInput.WriteLine("0")
    54      $process.StandardInput.WriteLine("netload")
    55      $process.StandardInput.WriteLine("""$dllFileName""")
    56      $process.StandardInput.WriteLine("FILEDIA")
    57      $process.StandardInput.WriteLine("1")
    58      $process.StandardInput.WriteLine("mySSsetTest")
    59      $process.StandardInput.WriteLine("Qsave")
    60      $process.StandardInput.WriteLine("QUIT")
    61      $process.StandardInput.WriteLine("EXIT")
    62      #$process.Kill()
    63      #$process.Close()
    64      Write-Host ""$item""批处理执行完成!" 
    65     }
    66     else
    67     {
    68         Write-Host ""“$item""被占用或者处于只读状态,批处理执行失败!" 
    69         continue
    70     }
    71     
    72 }
    View Code


     PowerShell脚本下载地址:见第一条评论

  • 相关阅读:
    jQuery.Ajax()执行WCF Service的方法
    呼叫WCF Service的方法出现Method not allowed异常
    ASP.NET MVC呼叫WCF Service的方法
    表格行与列mouse经过时高亮显示
    Git管理项目实例说明-记录和跟踪项目
    Maven私服Nexus3.x环境构建操作记录
    Nginx部署web缓存服务环境
    Mysql连接错误:Lost connection to Mysql server at 'waiting for initial communication packet'
    Linux下修改系统编码的操作记录
    web cache server方案比较:varnish、squid、nginx
  • 原文地址:https://www.cnblogs.com/NanShengBlogs/p/10981687.html
Copyright © 2020-2023  润新知