【问题标题】:how to load an assembly in a new powershell window如何在新的 powershell 窗口中加载程序集
【发布时间】:2014-10-06 06:37:50
【问题描述】:

我正在开发一个 powershell 脚本,该脚本压缩一个文件夹,然后在多个目标上并行解压缩,一次有 10 个,而不是一个。我想打开一个新的 powershell 窗口以将文件提取到多个目的地,并仅在成功解压缩操作时关闭窗口。我认为我在逻辑方面做得很好,但我一直在传递参数,似乎我能够加载程序集,但我的变量在新窗口中没有被正确识别。我如何让它工作?

$command1= 'Add-Type -Assembly "System.IO.Compression.FileSystem"'
           $command2= '[System.IO.Compression.ZipFile]::ExtractToDirectory("$zipfilename", "\\$Server\$DestinationLocation")'
           $stringcommand= "-noexit -command $command1 |-noexit -command $command2";           
           $ServerCopyWindows += Start-Process Powershell -ArgumentList $stringcommand -WindowStyle Normal

这是我正在处理的完整代码:

    function ZIPFileCopy
    {  
     $sourcedir= Read-Host ("Please Enter the Folder that needs to be Zipped")
     $zipfilename=Read-Host ("Please Enter the ZIP File location and ZIp File name with .Zip extension, Eg.C:\folder\Name.Zip")
  Add-Type -Assembly System.IO.Compression.FileSystem
  $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
  [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir, $zipfilename, $compressionLevel, $false)
  $Servernames= Get-Content "C:\ServerNames.txt"
  $DestinationLocation= Read-Host("Please enter the destination location, eg.c$\deployment\")  
  $TotalServers = $Servernames.count  
    for($i=0; $i -le $TotalServers/10; $i++)
    {
        $startLoopCount = ($i) * 10
        if($TotalServers - $startLoopCount -gt 10)
        {
            $endLoopCount = 10
            for( $k=0 ; $K -lt $endLoopCount; $K++)
            {
               $Server = $Servernames[$startLoopCount]
               $Server = $Server.Trim()
               $Server
               $command1= 'Add-Type -Assembly "System.IO.Compression.FileSystem"'
               $command2= '[System.IO.Compression.ZipFile]::ExtractToDirectory("$zipfilename", "\\$Server\$DestinationLocation")'
               $stringcommand= "-noexit -command $command1 |-noexit -command $command2";           
               $ServerCopyWindows += Start-Process Powershell -ArgumentList $stringcommand -WindowStyle Normal
               $startLoopCount++
            }
        }
        else
        {
            $endLoopCount = $TotalServers - $startLoopCount
            for( $k =0 ; $k -lt $endLoopCount; $k++)
            {
               $Server = $Servernames[$startLoopCount]
               $Server = $Server.Trim()
               $Server           
               $ServerCopyWindows += Start-Process Powershell
               $startLoopCount++
            }
        }

        [bool]$stopFlag = $true

        while($stopFlag)
        {
            $stopFlag = $false
            foreach($ServerWindow in $ServerCopyWindows)
            {
                if($ServerWindow -ne $null)
                {
                    if($ServerWindow.hasExited -ne $true)
                    {
                        $stopFlag = $true
                        break
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: c# .net powershell automation


    【解决方案1】:

    我认为管道传递给 PowerShell.exe 的命令在这里可能是一个问题。在Technet Social Site 上阅读 DMeier_Nera 的答案。

    基本上你可以像这样在一个变量中定义你的“脚本”:

    $yourscript = {
         Add-Type -Assembly "System.IO.Compression.FileSystem"
         [System.IO.Compression.ZipFile]::ExtractToDirectory("$zipfilename", "\\$Server\$DestinationLocation")
    }
    

    然后将您的脚本转换为字符串

    $command = $yourscript.ToString()
    

    将字符串转换为编码命令

    $bytes = [System.Text.Encoding]::Unicode.GetBytes( $command )
    $encodedCommand = [Convert]::ToBase64String( $bytes )
    

    然后执行

    powershell.exe -ExecutionPolicy ByPass -WindowStyle Minimized -EncodedCommand $encodedCommand
    

    我自己还没有测试过,但它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多