Advertisement
Guest User

powershell

a guest
Apr 27th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $switch = "";
  2. $pathtowrite = $env:USERPROFILE + "\Verzeichnisse"
  3. $csvfilepath
  4. $csvdata
  5. $counter = 0
  6. $textfortextfile
  7.  
  8. function CreateFolderIfNotExists($path)
  9. {
  10.    
  11.     if((Test-Path $path) -eq 0)
  12.     {
  13.         New-Item -ItemType Directory -Force -Path $path
  14.     }
  15. }
  16.  
  17. function Deletedir()
  18. {
  19.     $antwort = Read-Host "Wollen Sie wirklich " $pathtowrite " Recursive löschen? [J/N]"
  20.     if($antwort -eq "J" -or $antwort -eq "j")
  21.     {
  22.         Remove-Item -Recurse -Force $pathtowrite
  23.         Write-Host "Ordner Wurden gelöscht!"
  24.     }
  25.     else
  26.     {
  27.         Write-Host "Canceling...."
  28.     }
  29. }
  30.  
  31. function ReadCSVData($csvpath)
  32. {
  33.     $csvdata = Import-Csv $csvfilepath -Delimiter "," -Encoding Default
  34.     createDirForUsers($csvdata)
  35. }
  36.  
  37. function createTextFileforUser($path)
  38. {
  39.    
  40.     New-Item -ItemType File -Force -Path ($path + "\Hinweis.txt")
  41.     Set-Content -Value $textfortextfile -Path ($path + "\Hinweis.txt")
  42.  
  43. }
  44.  
  45. function createDirForUsers($csvdata)
  46. {
  47.     $csvdata | ForEach-Object {
  48.         $userpath = $pathtowrite+ "\" + $_.Langname + "_" + $_.Vorname.substring(0,2) + "_" + $_.Klasse
  49.         New-Item -ItemType Directory -Force -Path $userpath
  50.         $counter = $counter +1
  51.         $textfortextfile = "Willkommen " + $_.Langname + " " + $_.Vorname
  52.         #Write-Host $userpath
  53.         createTextFileforUser($userpath)
  54.     }
  55.  
  56.     Write-Host "------------------"
  57.     Write-Host "Es wurden " + $counter + " Ordner erstellet!"
  58. }
  59.  
  60. function getCSVPath()
  61. {
  62.     $csvfilepath = Read-Host "Bitte geben Sie den Pfad oder Dateinamen der CSV datei an!"
  63.     if((Test-Path $csvfilepath) -eq 0)
  64.     {
  65.         Write-Host "Kann die CSV datei nicht finden. Breche ab"
  66.     }
  67.     else
  68.     {
  69.         ReadCSVData($csvfilepath)
  70.     }
  71. }
  72.  
  73.  
  74. if($args.Count -eq 0)
  75. {
  76.     Write-Host "Sie haben keinen Switch angegeben. Es wird der default switch -Create verwendet"
  77.     $switch = "Create"
  78.     CreateFolderIfNotExists($pathtowrite)
  79.     getCSVPath
  80. }
  81. elseif($args.Count -gt 1)
  82. {
  83.     Write-Host "Bitte nur einen Parameter angeben"
  84. }
  85. elseif($args[0] -eq "-Create" -or $args[0] -eq "Create")
  86. {
  87.     Write-Host "-Create switch wurde gewählt...."
  88.     $switch = "Create"
  89.     CreateFolderIfNotExists($pathtowrite)
  90.     getCSVPath
  91. }
  92. elseif($args[0] -eq "-Delete" -or $args[0] -eq "Delete")
  93. {
  94.     Write-Host "-Delete switch wurde gewählt...."
  95.     $switch = "Delete"
  96.     Deletedir
  97. }
  98. else
  99. {
  100.     Write-Host "ERROR. Bitte überprüfen Sie Ihre eingabe"
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement