PowerShell Script mit Anwendung
Guten Morgen zusammen!
Ich benötige mal eure Hilfe.
Ich brauche ein PowerShell Script was folgendes erledigt.
- Sobald ein Word oder Excel Dokument geschlossen wird, soll sich eine Anwendung starten, die ich in dem Script hinterlegen kann. Wichtig ist das die Anwendung mit dem geschlossenen Dokument geöffnet wird (als würde man auf dem Desktop die Word Datei in die Anwendung schieben.)
Bei Fragen meldet euch gerne!
Vielen Dank für eure Unterstützung.
Ich benötige mal eure Hilfe.
Ich brauche ein PowerShell Script was folgendes erledigt.
- Sobald ein Word oder Excel Dokument geschlossen wird, soll sich eine Anwendung starten, die ich in dem Script hinterlegen kann. Wichtig ist das die Anwendung mit dem geschlossenen Dokument geöffnet wird (als würde man auf dem Desktop die Word Datei in die Anwendung schieben.)
Bei Fragen meldet euch gerne!
Vielen Dank für eure Unterstützung.
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 1135371877
Url: https://administrator.de/forum/powershell-script-mit-anwendung-1135371877.html
Ausgedruckt am: 02.04.2025 um 08:04 Uhr
13 Kommentare
Neuester Kommentar

Moin,
Viele Grüße
Bei Fragen meldet euch gerne!
Gerne: was ist Dir das gewünschte Tool denn wert? Klingt so nach Anforderung.Viele Grüße
Moin,
Gruß Thomas
# Quelldatei und Anwendung
$source = "<Dateipfad>"
$app = "<Programmpfad>"
try {
if (!([System.IO.FileInfo]$source).Exists) {throw "Die Quelldatei existiert nicht. "}
do {
try {
$stream = [System.IO.File]::OpenWrite($source)
} catch {
Write-Host -NoNewLine ("`r[{0:HH:mm:ss}] Wird gewartet, bis die Datei geschlossen wurde... " -f ([datetime]::get_Now()))
sleep 10
}
} until ($stream)
$stream.close()
Write-Host -ForegroundColor green "ok."
&$app $source
} catch {
Write-Host -ForegroundColor red $_.Exception.Message
}
Gruß Thomas

Hi.
Ja, das dachte ich mir.
Copy&Pasta vom Feinsten.
Benutze bitte Codetages.
Gruß
Ja, das dachte ich mir.
$applicationPath = "Pfad zur Anwendung"
.. hier sollte der Pfad zu deiner Anwendung stehen ..Copy&Pasta vom Feinsten.
Benutze bitte Codetages.
Gruß

funktioniert aber nicht
Schön und wir sollen nun raten, warum es nicht funktioniert?Bitte Fehlermeldung posten und auch, wie Du das Script verändert hast....
Montagmorgen, fast wie Freitag.
Gruß
Kauf dir ein Powershell Buch und befasse dich mal damit, sonst verstehst Du nicht wirklich was...
https://www.amazon.de/PowerShell-Core-Dummies-Andreas-Dittfurth/dp/35277 ...
https://www.amazon.de/PowerShell-Core-Dummies-Andreas-Dittfurth/dp/35277 ...

du scheinst ja heute wirklich schlechte Laune zu haben.
Nach deinem Post, schon. Ja.Jetzt denk' Dich mal in uns hinein: Wie sollen wir Dir helfen, wenn Du uns nicht mal die Fehlermeldung mitteilst? "funktioniert aber nicht" ist semihilfreich für uns.
Und: wenn Du uns auch mitteilen würdest, WAS genau Du in dem Script Du geändert hast, könnte man auch hier helfen.
Habe dies nur für den Post eingetragen
:facepalm: Ist halt quatsch um vernünftig unterstützen zu können...Wie sagt man so schön?
Wieso sollte ich mein Hirn anstrengend, wenn ich mühelos andere dazu bewegen kann?Kauf dir ein Powershell Buch und befasse dich mal damit, sonst verstehst Du nicht wirklich was...
Achtung: unfreundlicher Kommentar seitens TOs incomming ...Najaa.
Gruß
Servus,
Grüße Uwe
# function to get pathes of currently opened office files
Function Get-OpenedOfficeFiles {
param(
[parameter(mandatory=$false)][ValidateSet('All','Powerpoint','Visio','Excel','Word','Publisher','Acrobat')][string[]]$app = 'All'
)
function Get-ExcelFiles {
# Excel
try{
$excel = [System.Runtime.InteropServices.Marshal]::GetActiveObject('Excel.Application')
$excel.Workbooks | ?{Test-Path $_.FullName} | select -Expand Fullname
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel)
}catch{}
}
function Get-PowerpointFiles {
# Powerpoint
try{
$pp = [System.Runtime.InteropServices.Marshal]::GetActiveObject('Powerpoint.Application')
$pp.Presentations | ?{Test-Path $_.FullName} | select -Expand Fullname
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($pp)
}catch{}
}
function Get-VisioFiles {
# Visio
try{
$visio = [System.Runtime.InteropServices.Marshal]::GetActiveObject('Visio.Application')
$visio.Documents | ?{$_.ObjectType -eq 10 -and (Test-Path $_.Fullname)} | select -Expand Fullname
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($visio)
}catch{}
}
function Get-WordFiles {
# Word
try{
$word = [System.Runtime.InteropServices.Marshal]::GetActiveObject('Word.Application')
$word.Documents | ?{Test-Path $_.FullName} | select -Expand Fullname
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($word)
}catch{}
}
function Get-PublisherFiles {
# Publisher
try{
$pub = [System.Runtime.InteropServices.Marshal]::GetActiveObject('Publisher.Application')
$pub.Documents | ?{Test-Path $_.FullName} | select -Expand Fullname
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($pub)
}catch{}
}
function Get-AcrobatFiles {
# Acrobat
try{
$acro = New-Object -Com "AcroExch.App"
$numdocs = $acro.GetNumAVDocs()
if ($numdocs){
0..($numdocs-1) | %{
$js = $acro.GetAVDoc($_).GetPDDoc().GetJSObject()
[Type]::GetType($js).InvokeMember('Path',[System.Reflection.BindingFlags]::GetProperty,$null,$js,$null) -replace '^/' -replace '/','\' -replace '^(\w+)(?=\\)','$1:'
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($js)
}
}
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($acro)
}catch{}
}
switch ($app){
'All' {
Get-PowerpointFiles
Get-ExcelFiles
Get-WordFiles
Get-PublisherFiles
Get-VisioFiles
Get-AcrobatFiles
}
'Powerpoint' {Get-PowerpointFiles}
'Visio' {Get-VisioFiles}
'Word' {Get-WordFiles}
'Excel' {Get-ExcelFiles}
'Publisher' {Get-PublisherFiles}
'Acrobat' {Get-AcrobatFiles}
}
[gc]::collect()
[gc]::WaitForPendingFinalizers()
}
function foreach-parallel {
[CmdletBinding()]
param(
[parameter(mandatory=$true,ValueFromPipeline=$true)][ValidateNotNullOrEmpty()][object[]]$InputObject,
[parameter(mandatory=$true)][ValidateNotNullOrEmpty()][scriptblock]$process,
[parameter(mandatory=$false)][ValidateNotNullOrEmpty()][int]$ThrottleLimit = [System.Environment]::ProcessorCount,
[parameter(mandatory=$false)][switch]$showprogress,
[parameter(mandatory=$false)][object]$messagedata
)
begin{
$rspool = [runspacefactory]::CreateRunspacePool(1,$ThrottleLimit)
$rspool.ApartmentState = 'STA'
$rspool.Open()
$jobs = New-Object System.Collections.ArrayList
$objects = New-Object System.Collections.ArrayList
}
process{
$InputObject | %{[void]$objects.Add($_)}
}
end{
foreach ($obj in $objects){
$ps = [Powershell]::Create()
$ps.RunspacePool = $rspool
[void]$ps.AddScript($process).AddParameter('_',$obj).AddParameter('MessageData',$messagedata)
$job = $ps.BeginInvoke()
[void]$jobs.Add(([pscustomobject]@{Handle = $job; Powershell = $ps}))
}
write-verbose "Waiting for all jobs to complete."
$completed = 0
while($jobs.Count){
if ($showprogress.IsPresent){
Write-Progress -Activity $PSCmdlet.MyInvocation.InvocationName -Status "Total of $($objects.Count) objects." -PercentComplete (($completed / $objects.Count) * 100) -CurrentOperation "Completed $completed of $($objects.Count)."
}
# process finished jobs and output their result
$finished = @()
foreach($job in $jobs | ?{$_.Handle.IsCompleted}){
$job.Powershell.EndInvoke($job.handle)
$job.Powershell.Dispose()
$completed++
$finished += $job
}
$finished | %{$jobs.Remove($_)}
}
# cleanup
$rspool.Close();$rspool.Dispose()
}
}
# get currently opened word and excel files
$openfiles = Get-OpenedOfficeFiles -app Word,Excel
if ($openfiles){
write-host "Waiting until all files are closed ..." -F Green
# process files in parallel
$openfiles | foreach-parallel -showprogress -process {
param($_)
# Check file locked state function
function Is-FileLocked([string]$filePath){
$fileStream = $null; $result = $true
try{
$fileStream = (New-Object System.IO.FileInfo $filePath).OpenWrite()
# file is not locked by another process
$result = $false
}catch{
}finally{
if($filestream){
$fileStream.Close(); $fileStream.Dispose()
}
}
return $result
}
# wait until file is unlocked
while(Is-FileLocked $_){
sleep 1
}
"File '$_' was closed"
# start application to process closed file
start cmd -ArgumentList "/c echo File '$_' was closed. &pause"
}
}else{
write-warning "No open office files found."
}
Serie: Anwendungen
PowerShell Script mit Anwendung13