【发布时间】: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