converting a bat script to an ps1 script

38 Views Asked by At

I have the following batch file.

@echo off
@echo Kopiere Autodesk-Profil ins Homeverzeichnis (H:\)...
@rmdir /s /q "H:\Downloads\Temp_Autodesk\Autodesk AutoCAD Map 3D 2021\"
@xcopy /s /y "%APPDATA%\Autodesk\Autodesk AutoCAD Map 3D 2021\*.*" "H:\Downloads\Temp_Autodesk\Autodesk AutoCAD Map 3D 2021\*.*"
cscript dialogfenster.vbs "AutoCAD-Einstellungen gesichert"
exit

it copies data from a path that depends on the user (user can change and must therefore be a variable) into the directory "H:\Downloads\Temp_Autodesk\Autodesk AutoCAD Map 3D 2021". now i have to convert this code into a .ps1 file. I have now tried this with the partial help of AI and have come to the following result.

> # Löscht den Ordner und alle seine Inhalte, ohne eine Bestätigung zu fordern
Remove-Item -Path "H:\Downloads\Temp_Autodesk\Autodesk AutoCAD Map 3D 2021\" -Recurse -Force -ErrorAction SilentlyContinue

# Kopiert alle Dateien und Unterordner aus dem Quellverzeichnis in das Zielverzeichnis
Copy-Item -Path "$env:APPDATA\Autodesk\Autodesk AutoCAD Map 3D 2021\*" -Destination "H:\Downloads\Temp_Autodesk\Autodesk AutoCAD Map 3D 2021\" -Recurse -Force

# Führt ein VBScript aus und zeigt möglicherweise ein Dialogfenster an
& cscript //NoLogo "dialogfenster.vbs" "AutoCAD-Einstellungen gesichert"

# Skriptende
exit

Unfortunately, this does not work properly, which is probably due to the fact that Copy-Item -Path "$env:APPDATA\Autodesk\Autodesk AutoCAD Map 3D 2021*" this part does not access the user

1

There are 1 best solutions below

0
sirtao On

Most likely:

You are using Copy-Item to move stuff in the directory H:\Downloads\Temp_Autodesk\Autodesk AutoCAD Map 3D 2021\.
But you did DELETE that directory with your first command: thus the copy fails as the cmdlet doesn't find the destination as it doesn't create it by itself.

just use Remove-Item -Path "H:\Downloads\Temp_Autodesk\Autodesk AutoCAD Map 3D 2021\*" -Recurse -Force -ErrorAction SilentlyContinue so it only deletes the CONTENTS of the directory, not the directory itself.

as alternative: copy the directory itself, not just the contents:

Remove-Item -Path "H:\Downloads\Temp_Autodesk\Autodesk AutoCAD Map 3D 2021\" -Recurse -Force -ErrorAction SilentlyContinue

Copy-Item -Path "$env:APPDATA\Autodesk\Autodesk AutoCAD Map 3D 2021" -Destination "H:\Downloads\Temp_Autodesk\" -Recurse -Force