Is it possible to embed and execute VBScript within a batch file without using a temporary file?

后端 未结 6 893
慢半拍i
慢半拍i 2020-11-22 04:39

People have been embedding and executing VBScript within batch files for a long time. But all the published solutions that I have seen (at the time this question was ori

6条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 05:18

    I don't how much exactly the VB.NET counts for VBScript but it is definitely VisualBasic code with some C# flavours.

    With the version of msbuild there's a thing called an inline tasks that allows you to load a .net code into memory and execute it. And this can be embedded into a batch file with the a similar manner that used with wsf:

     
    
    
    
      
        
      
       
        
         
         
          
          
            
          
        
      
    
    

    it verbose indeed though xml syntax allows almost everything to be put in one line if somebody wants (but I thing for educational reasons is better this example to be left like that). The batch code cannot contain -- so for robustness this part can be put in a CDATA also.

    Other way to embed VB.NET into batch without temp files is with the help of powershell and its type definition cmdlet:

    <# : batch portion
    @echo off & setlocal
    
    set "CMD_ARGS=%~1"
    
    powershell -noprofile "iex (${%~f0} | out-string)"
    goto :EOF
    
    : end batch / begin powershell #>
    
    param($psArg1 = $env:psArg1)
    
    
    $VB = @" 
    Imports System
    namespace VB 
        Public Class VB
            Public Shared Sub Print()
                Console.WriteLine("PRINTED BY VB")
            End Sub
        End Class
    End Namespace
    "@
    
    Add-Type -TypeDefinition $VB -Language VisualBasic
    
    
    [VB.VB]::Print()
    

提交回复
热议问题