2017-12-14 4 views
0

Ich erstelle ein Bereitstellungsskript, um Software mithilfe einer ppkg-Datei auf einem neuen Gerät zu installieren.Powershell-Skript zum Bereitstellen mehrerer Software mithilfe von Variablen

Das Skript untersucht, welches Laufwerk das USB-Laufwerk ist und kopiert die Software in den lokalen temporären Ordner und führt sie gemäß einer Reihe von Variablen aus, wie unten gezeigt.

Was ich zu tun habe, ist das Skript zu vereinfachen, also wiederhole ich Code 7 mal nicht auf der Seite, ich möchte nur eine Schleife sieben Mal ausführen, um die benötigte Software einzubinden. Ich habe versucht, ein Array, aber ich denke, ich verstehe es nicht ganz.

Das ist mein Skript ist so weit mit dem sich wiederholenden Code:

#SOE application Variables 

#applcation1 CM_client 
$app1name = "Config Manager Client 1706" 
$app1skip = "no" 
$app1path = "$env:SystemDrive\temp\soe\application_installs\app1\CM_client_inst_1706\" 
$app1runcommand = "clientx64.bat" 
$app1arguments = "" 
#applcation2 
$app2name = "Office 2016 Pro Plus" 
$app2skip = "no" 
$app2path = "$env:SystemDrive\temp\soe\application_installs\app2\O2016\" 
$app2runcommand = "setup.exe" 
$app2arguments = "/configure configuration.xml" 
#log Folder 
$datetime = Get-Date -format "yyyy.MM.dd-HH.mm.ss" 
$logpath = "$env:ALLUSERSPROFILE\SOEInst_ppkg\$datetime" 
New-Item -Path $logpath -ItemType Directory -ErrorAction SilentlyContinue 

#Transcript Start 
Start-Transcript -Path $logpath\SOE-app-installer-ppkg-$datetime.log 

#Timer Function 
$pkgremovetime = Get-Date -format "HH:mm:ss" 
write-host "Script Start Time - $pkgremovetime" 

#Find USB Drive 
Write-host Discovering USB Drive 

$drives = (GET-WMIOBJECT –query “SELECT * from win32_logicaldisk").DeviceID 

foreach ($drive in $drives) { 

      $usbdrive = (dir $drive USBIMG.FILE | Select-Object -Unique "USBIMG.FILE") 
      if ($usbdrive -match "USBIMG.FILE*") { 
        $datadrive = $drive 
        } 
      } 

Write-host Found $datadrive is the USB drive 

#Copy Applications to Local Drive 
Write-Host Creating Installer Folder 
New-Item -Path $env:SystemDrive\temp\SOE -ItemType Directory 
Copy-Item $datadrive\application_installs $env:SystemDrive\temp\soe -Recurse -Verbose 

#Install Applications 
#Application 1 
    if ($app1skip -eq "no") { 
    if ($app1arguments) { #Arguments Variable Populated 
     Write-Host Installing Applcation 1 `($app1name`) 
     $app1 = Start-Process -Wait -FilePath $app1path$app1runcommand -ErrorAction Continue -ArgumentList $app1arguments -WindowStyle Normal 
      if ($app1.ExitCode -eq "0") { 
       Write-Host $app1name Installed ok 
       } Else { 
        Write-host $app1name install exited with code $app1.ExitCode 
        } 
       } 
      }Else { #Argurments Variable Empty 
       Write-Host Installing Applcation 1 `($app1name`) 
       $app1 = Start-Process -Wait -FilePath $app1path$app1runcommand -ErrorAction Continue -WindowStyle Normal 
       if ($app1.ExitCode -eq "0") { 
        Write-Host $app1name Installed ok 
        } Else { 
         Write-host $app1name install exited with code $app1.ExitCode 
       } 
      } 


#Application 2 
if ($app2skip -eq "no") { 
    if ($app2arguments) { #Arguments Variable Populated 
     Write-Host Installing Applcation 2 `($app2name`) 
     $app2 = Start-Process -Wait -FilePath $app2path$app2runcommand -ErrorAction Continue -ArgumentList $app2arguments -WindowStyle Normal 
      if ($app2.ExitCode -eq "0") { 
       Write-Host $app2name Installed ok 
       } Else { 
        Write-host $app2name install exited with code $app2.ExitCode 
        } 
       } 
      }Else { #Argurments Variable Empty 
       Write-Host Installing Applcation 2 `($app2name`) 
       $app2 = Start-Process -Wait -FilePath $app2path$app2runcommand -ErrorAction Continue -WindowStyle Normal 
       if ($app2.ExitCode -eq "0") { 
        Write-Host $app2name Installed ok 
        } Else { 
         Write-host $app2name install exited with code $app2.ExitCode 
       } 
      } 
#cleanup 

Remove-Item $env:SystemDrive\temp\soe -Recurse -Force -Verbose 

#get end time 
$pkgremovetime_end = Get-Date -format "HH:mm:ss" 

#calculate time difference 
$timetaken = New-TimeSpan $pkgremovetime $pkgremovetime_end 
    if ($timetaken.Seconds -lt 0) { 
$Hrs = ($timetaken.Hours) + 23 
$Mins = ($timetaken.Minutes) + 59 
$Secs = ($timetaken.Seconds) + 59 } 
    else { 
$Hrs = $timetaken.Hours 
$Mins = $timetaken.Minutes 
$Secs = $timetaken.Seconds } 
$Difference = '{0:00}:{1:00}:{2:00}' -f $Hrs,$Mins,$Secs 

#log time difference 
write-host "Script End Time - $pkgremovetime_end" 
Write-Host "Total time taken $difference" 

#Transcript End 
Stop-Transcript 
+1

Sie nicht, dies zu einem kleinen repräsentativen Stichprobe nach unten destillieren könnte anstelle eines 100+ Linie Code-Dump? –

+0

Schneiden Sie es für Sie herunter, als am besten darzustellen, was es tut, hoffe, dass Sie glücklich macht ..... –

+0

Danke für die Bearbeitung. Bitte hör auf, die JavaScript/HTML-Code-Formatierung nach dem Ausziehen wieder herzustellen. Dies ist kein Javascript oder HTML. Verwenden Sie für regulären Code die Schaltfläche ** {} ** in der Symbolleiste. Vielen Dank. –

Antwort

1

Ich schlage vor, eine Funktion machen, die in den Variablen erfolgt. Ich habe einen schnellen Vergleich der Installation Codes und so etwas wie dies sollte

function installApplication{ 
    Param($skip, $arguments, $name, $path, $runcommand) 
    if ($skip -eq "no"){ 
     if ($arguments){ 
      write-host "Installing Application $appname" 
      $app = Start-Process -Wait -FilePath $path$runcommand -ErrorAction.... 
      if($app.ExitCode -eq "0"){ 
      .... 
      .... 
} 

und so weiter arbeiten, können Sie dann die Funktion aufrufen

installApplication $app1skip $app1arguments $app1name $app1path $app1runcommand 
installApplication $app2skip $app2arguments $app2name $app2path $app1runcommand 

Ihre Eingabe Argumenten wird die Funktionsparameter in der ersetzen Reihenfolge, in der Sie sie übergeben, oder Sie können -skip $ app1skip verwenden, um die Parameter zuzuweisen.

Wenn Sie den gleichen Code zu oft wiederholen, schlage ich vor, es in etwas wie diffchecker zu werfen, setzen Sie den Code in eine Funktion und ersetzen Sie alle Unterschiede durch Variablen.

Sie können den Code hier https://www.diffchecker.com/FxAIdD6g (1 Tag nur) sehen

+0

Ich werde das versuchen, sieht aus wie eine sehr schöne Lösung, danke. Ich habe einige andere Probleme in meinem Code gefunden und aufgeräumt habe ich in die Funktion schauen. –

Verwandte Themen