简体   繁体   English

如何在新的Powershell窗口中加载部件

[英]how to load an assembly in a new powershell window

I am working on a powershell script that zips a folder and then unzip on multiple destinations in parallel with a batch of 10 rather one at a time. 我正在研究一个Powershell脚本,该脚本压缩一个文件夹,然后一次在多个目标上同时解压缩多个目的地,而不是一次10个。 I want to open a new powershell window to extract the files on to multiple destinations and close the windows only for the successful unzip operation. 我想打开一个新的Powershell窗口以将文件提取到多个目标位置,并仅为成功进行解压缩操作而关闭窗口。 I think I am doing good in regards to logic but I am stuck passing the parameters, seems like I am able to load assembly but my variables are not properly recognized on the new window. 我认为我在逻辑方面做得很好,但是我一直无法传递参数,似乎可以加载程序集,但是在新窗口中无法正确识别变量。 How do I get this working? 我该如何工作?

$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

Here is the full code that I am working on: 这是我正在处理的完整代码:

    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
                    }
                }
            }
        }
    }
}

I think pipelining the commands passed to PowerShell.exe might be a problem here. 我认为在管道中传递传递给PowerShell.exe的命令可能是一个问题。 Give DMeier_Nera's answer a read on the Technet Social Site . Technet社交网站上阅读DMeier_Nera的答案。

Basically you can define your "script" in a variable like so: 基本上,您可以像这样定义变量中的“脚本”:

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

Then convert your script to a string 然后将您的脚本转换为字符串

$command = $yourscript.ToString()

Convert to string to an encoded command 转换为字符串以编码的命令

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

And execute it 并执行它

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

Haven't tested tthis myself, but it should work. 我自己还没有测试过,但是应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM