## Script Version 0.2 Param( [string]$PcfilePath, [parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()][string]$DeploymentPS1File ) if ($DeploymentPS1File.length -eq "0" ) { write-host "No deployment ps1 Defined " exit-script(999) } else { $ToInstall = $DeploymentPS1File write-host "Using the following ps1 file : "+ $ToInstall } $InstallFile = get-content $ToInstall foreach ($info in $InstallFile) { if ($info.StartsWith("`$appVendor")) { $appVendor = $info.Split(" = ") $appVendor = $appVendor.Item(3).replace('"','') #write-host $appVendor } elseif ($info.StartsWith("`$appName")) { $appName = $info.Split(" = ") $appName = $appName.Item(3).replace('"','') #write-host $appName } elseif ($info.StartsWith("`$appVersion")) { $appVersion = $info.Split(" = ") $appVersion = $appVersion.Item(3).replace('"','') #write-host $appVersion } elseif ($info.StartsWith("`$appRevision")) { $appRevision = $info.Split(" = ") $appRevision = $appRevision.Item(3).replace('"','') #write-host $appRevision } elseif ($info.StartsWith("`$appScriptVersion")) { $appScriptVersion = $info.Split(" = ").replace('"','') $appScriptVersion = $appScriptVersion.Item(3).replace('"','') #write-host $appScriptVersion } } $ToInstall = $ToInstall.replace('ps1','exe') $ApptoInstall = $appVendor + "_" + $appName + "_" + $appVersion $AppDeployVersion = "_Rev_" + $appRevision + "_" + $appScriptVersion $Sleepsecuntes = "3600" write-host "JobName : " $ApptoInstall write-host "JobVersion : " $AppDeployVersion write-host "Sleep between Rerun off jobs : " $Sleepsecuntes if ($Pcfilepath.length -eq "0" ) { $defaltHostlist = "c:\pmsds\pc.txt" Write-Host "Default input file with computernames :" $defaltHostlist } else { if (test-path $PcfilePath ) { $defaltHostlist = $PcfilePath Write-Host "custom input file with computernames : " $defaltHostlist } } #$credential = Get-Credential -Credential it-nerd\lwh0 #$ApptoInstall = "AdobeReader11.0.09" #$AppDeployVersion = "v1" #$ToInstall = "\\pmsds\PMSDS\Software\ToInstall\AdobeReader11.0.09\Toolkit\Deploy-Application.exe" $pcs = Get-Content $defaltHostlist "`nLoaded total pc from file $($pcs.count)`n" $datestamp = Get-DAte -format "dd-mm-yyyy-HH-mm" $pmsdsRoot = "c:\pmsds" $pmsdsJobs = "\jobs" $pmsdslogs = "\logs" $slash = "\" $Filety = ".bat" $filelog = ".log" $filererun = ".rerun" $filebad = ".BAD" $Jcount = 0 $ServiceUI = "\\pmsds\PMSDS\Tools\ServiceUIx64.exe " $cmd = " C:\Windows\System32\cmd.exe /C " $PSExec = "C:\SysinternalsSuite\psexec.exe \\" $PSExecSystem = " -s " $SUI = "_serviceUi" $loganalyseLine = "C:\Windows\System32\cmd.exe exited on " $joblog = $pmsdsRoot + $pmsdsJobs + $slash + $jobname $jobname = $ApptoInstall + $AppDeployVersion $jobdir = $pmsdsRoot + $pmsdsJobs +$slash +$ApptoInstall + $AppDeployVersion +$slash + $datestamp $joblogdir = $pmsdsRoot + $pmsdslogs $joblog = $pmsdsRoot + $pmsdslogs + $slash + $jobname + $filelog $jobRerun = $jobdir + $slash + $slash + $jobname + $datestamp + $filererun $jobBAD = $jobdir + $slash + $slash + $jobname +$datestamp + $filebad if(!(Test-Path -Path $jobdir)){ New-Item -ItemType directory -Path $jobdir } if(!(Test-Path -Path $joblogdir)){ New-Item -ItemType directory -Path $joblogdir } foreach($pc in $pcs){ $softwarelogs = '\\'+ $pc + '\logs' write-host "testing connection to : " $softwarelogs if (Test-path $softwarelogs ) { $scriptToRunWithUser = $PSExec + $pc + $PSExecSystem + $cmd + $ServiceUI + $ToInstall $scriptToRunNouser = $PSExec + $pc + $PSExecSystem + $cmd + $ToInstall $runFileuser = $jobdir + $slash + $pc+ $SUI + $Filety $runFile = $jobdir + $slash + $pc + $Filety #write-host "to write:" $runFile #write-host "to run: " $scriptToRun # test om der er en bruger logget ind. $SB = { Get-ChildItem Registry::\HKEY_USERS | Where-Object { Test-Path "$($_.pspath)\Volatile Environment" } | ForEach-Object { (Get-ItemProperty "$($_.pspath)\Volatile Environment").USERNAME }} $user = Invoke-Command -ComputerName $pc -ScriptBlock $SB write-host "User Loggedon: " $user if ($user.length -eq "0") { $scriptToRunNouser | Out-File $runFile -Encoding ascii -Force } else { $scriptToRunWithUser | Out-File $runFileuser -Encoding ascii -Force } } else { $pc.tostring() | Out-File $jobRerun -Append -Encoding ascii } } get-job -Name $jobname -ErrorAction SilentlyContinue | remove-job Foreach ($bat in get-childitem $jobdir){ if ($bat.Extension -eq ".bat") { write-host "Running at background job : " $bat $scriptblock = $cmd + $bat.FullName $scriptBlock = [Scriptblock]::Create($scriptblock) start-job -Name $jobname -ScriptBlock $scriptblock } } #write-host "All jobs are started sleeping 20 sec" #Start-Sleep -Seconds 20 get-job -Name $jobname | Wait-Job get-job -Name $jobname | Receive-Job -Keep 2>&1 > $joblog $loganalyse = Get-Content $joblog foreach ($line in $loganalyse ) { if ($line.ToLower().StartsWith( $loganalyseLine.ToLower())) { $analyse = $line.Split(" ") $hostname = $analyse.Item(3) $psexit = $analyse.Item(7) if( $psexit -eq "0.") { Write-Host "job done" Write-Host "hostname: " $analyse.Item(3) Write-Host "Exitcode: " $analyse.Item(7) } elseif ($psexit -eq "5000.") { write-host "job defered, need to rerun" Write-Host "hostname: " $analyse.Item(3) Write-Host "Exitcode: " $analyse.Item(7) $analyse.Item(3).tostring() | Out-File $jobRerun -Append -Encoding ascii } else { write-host "job gone bad, look into it" Write-Host "hostname: " $analyse.Item(3) Write-Host "Exitcode: " $analyse.Item(7) $line.tostring() | Out-File $jobBad -Append -Encoding ascii } } } if (Test-Path ($jobRerun)) { Write-Host "waiting this ammount of secondes before rerun: " $Sleepsecuntes Start-Sleep -Seconds $Sleepsecuntes Invoke-Expression -Command ($PSCommandPath + ' -PcfilePath ' + $jobRerun + ' -DeploymentPS1File ' + $DeploymentPS1File) }