-nouvelle branche pour nouveau fichier .xlsx (02/06/20)

-essai sur usb
This commit is contained in:
2020-05-30 15:10:42 +02:00
parent 002d01f523
commit fa4d0d5c01
3 changed files with 312 additions and 1 deletions

View File

@@ -0,0 +1,210 @@
# Pour avoir les accents, choisir l'encodage UTF-8-BOM
# Ouvrir ajt_creator en passant le fichier .xlsx en paramètre.
# .\ajt_creator "C:\Users\bruno\Desktop\xls\essai ve 15-05.xlsx"
param([string]$ajt_file)
Write-host "`n ###################"
Write-host " MGPT Ajt Créateur"
Write-host " ###################"
Function Save-FileName($initialDirectory, $name){
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
$SaveFileDialog.initialDirectory = $initialDirectory
$SaveFileDialog.FileName = $name;
$SaveFileDialog.filter = DAT files (*.dat)|*.dat|All files (*.*)|*.*
$SaveFileDialog.ShowDialog() | Out-Null
$SaveFileDialog.filename
}
Function Get-FileName($initialDirectory) {
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "Excel Files|*.xls;*.xlsx"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
# Mettre le fichier .xlsx dans un dossier xls sur le bureau
#$ajt_file = "$env:USERPROFILE\Desktop\xls\essai ve 15-05.xlsx"
<#
# Entrer le nom de fichier au prompt
$inputfile = read-host "Entrer le chemin du fichier .xlsx"
# xls\essai ve 15-05.xlsx
$inputdata = get-content $inputfile
#>
# Choisir le fichier .xlsx par une boite de dialogue (sur le bureau par défaut)
if ([string]::IsNullOrEmpty($ajt_file)) {
Write-host "`nChoisir le fichier .xlsx ..."
$ajt_file = Get-FileName "$env:USERPROFILE\Desktop"
}
# Si aucun fichier .xlsx fourni, on quitte le script.
if ([string]::IsNullOrWhitespace($ajt_file)) {
#[Environment]::Exit(1) # quit Powershell
Write-host "Aucun fichier choisi. On quitte le script ..."
Break
}
Write-host "On importe le .xlsx ..."
$ajt = Get-Item $ajt_file
# Nom long du fichier .xlsx
$ExcelFileName = $ajt.fullname
# Nom court du fichier .csv
$CSVBaseName = $ajt.Basename
# Chemin du .xlsx
$CSVpath = Split-Path -Path $ajt_file
# Nom long du fichier .csv
$CSVFileName = $CSVpath + "\" + $CSVBaseName + ".csv"
<#
Write-host "ExcelFileName: $ExcelFileName"
Write-host "CSVBaseName: $CSVBaseName"
Write-host "CSVpath: $CSVpath"
Write-host "CSVFileName: $CSVFileName"
#>
# Export to .csv (6)
$xlCSV=6
# Conversion .xlsx vers .csv
Write-host "Conversion du fichier .xlsx en .csv ..."
$Excel = New-Object -comobject Excel.Application
$Excel.Visible = $False
$Excel.displayalerts=$False
$Workbook = $Excel.Workbooks.Open($ExcelFileName)
$Workbook.SaveAs($CSVFileName,$xlCSV)
$Excel.Quit()
If(ps excel){kill -name excel}
# On efface / backup tous les fichiers MGPT*.dat existants
Write-host "On efface / backup tous les fichiers MGPT*.dat existants..."
$fileToCheck = "$env:USERPROFILE\Desktop\MGPT*.dat"
$archivePath = "$env:USERPROFILE\Desktop\MGPT_Ajt_Archives"
if (Test-Path $fileToCheck -PathType leaf) { # leaf: file, container: folder, any: tous
#Remove-Item $fileToCheck
if (!(Test-Path $archivePath -PathType container)) {
mkdir $archivePath
}
Move-Item -Path $fileToCheck -Destination $archivePath -Force
}
# On importe le .csv selon $header
Write-host "On importe le .csv ..."
$header = "planche", "d_deb", "jour", "h_deb", "h_fin", "J", "d_fin", "lieu", "VC", "gare", "zep", "encadrant", "rptx", "ncp", "get"
#$A = import-csv -Path "$env:USERPROFILE\Desktop\xls\essai ve 15-05.csv" -Delimiter ";" -Header $header
$A = import-csv -Path $CSVFileName -Delimiter ";" -Header $header
# On sélectionne uniquement les lignes commençant par AK ou BF
#$planches = $A | Where-Object planche -Match "^AK*|^BF*"
$planches = $A | Where-Object VC -eq "V"
Write-host "Création du .dat avec les opérations suivantes:"
# Pour chaque opération
ForEach ($operation in $planches){
# Operation
#$ope = $($operation.planche).Split(" ")[1]
$ope = $($operation.planche)
#$ope
# ZEP/Groupement
#CCTT 8948 "ZEP DJ20948" (702+712+714+720+722+724+726+728)
$zep = $($operation.zep).Split(" ")[1]
#$zep
# Debut
$dd = $($operation.d_deb)
$y = $($dd.Split("/")[2])
$m = $($dd.Split("/")[1])
$d = $($dd.Split("/")[0])
$dd = "$($y)$($m)$($d)"
$hd = $($operation.h_deb).Replace(":", "")
$debut = "$($dd)$($hd)"
#$debut
# Fin
$df = $($operation.d_fin)
$y = $($df.Split("/")[2])
$m = $($df.Split("/")[1])
$d = $($df.Split("/")[0])
$df = "$($y)$($m)$($d)"
$hf = $($operation.h_fin).Replace(":", "")
$fin = "$($df)$($hf)"
#$fin
# Voie
$voie = ""
# Rptx
$rptx = $($operation.rptx).Split(":")[1]
#$rptx
# N° CP
$cp = $($operation.ncp)
#$cp
# Tel
$tel = "0000000000"
#$tel
# Terminaison
$term = "||||||||||||||||||||||||||"
$ligne = "$($ope)|1|$($zep)|$($voie)|$($debut)|$($fin)|$($rptx)|$($cp)|$($tel)$term"
Write-host $ligne
# Chaque opération est écrite dans le fichier MGPT_<date-du-jour>.dat
#echo $ligne >> $env:USERPROFILE/Desktop/MGPT_$dd.dat
$dat_file = "$env:USERPROFILE\Desktop\MGPT_$dd.dat"
Add-Content $dat_file $ligne
}
Write-host "Le fichier ajt $dat_file a été crée."
[string]$Export = Read-Host "Voulez-vous exporter le fichier ajt ? [o/n]"
IF ($Export -eq "o") {
Write-host "Insérer une clé USB..."
$d = Get-Item $dat_file
$dat = $d.Basename
$dest_dat_file = Save-FileName -initialDirectory "%USERDATA%\desktop\" -name $dat
$dest_dat_file
<#
$e = Get-Item $dest_dat_file
$e.Basename
$e.fullname
$destination = Split-Path -Path $e
$destination
Copy-Item -Path $dat_file -Destination $destination
#>
Write-host "Copie du fichier ajt sur la destination..."
Copy-Item -Path $dat_file -Destination $dest_dat_file
}

33
usb.ps1
View File

@@ -1,8 +1,11 @@
# Get-CimClass -ClassName Win32_DiskDrive
#$usb = GET-WMIOBJECT query "SELECT * from win32_diskdrive where InterfaceType = 'USB'"
$usb = GET-WMIOBJECT win32_diskdrive | Where { $_.InterfaceType -eq 'USB' }
$usb
#If ($usb.model -ne "Kingston DataTraveler 3.0 USB Device"){
If ($usb.model -eq "USB Device"){
If ($usb.model -eq "USB Device") {
Write-host "USB Device inserted"
}
@@ -10,6 +13,34 @@ If ($usb.model -eq "USB Device"){
#$newEvent = Wait_Event -SourceIdentifier volumeChange
#$newEvent
###################
Get-WmiObject Win32_DiskDrive | ForEach-Object {
$disk = $_
$partitions = "ASSOCIATORS OF " +
"{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
"WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Get-WmiObject -Query $partitions | ForEach-Object {
$partition = $_
$drives = "ASSOCIATORS OF " +
"{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
"WHERE AssocClass = Win32_LogicalDiskToPartition"
Get-WmiObject -Query $drives | ForEach-Object {
New-Object -Type PSCustomObject -Property @{
Disk = $disk.DeviceID
DiskSize = $disk.Size
DiskModel = $disk.Model
Partition = $partition.Name
RawSize = $partition.Size
DriveLetter = $_.DeviceID
VolumeName = $_.VolumeName
Size = $_.Size
FreeSpace = $_.FreeSpace
}
}
}
}
$drives = Get-PSDrive
$drives

70
usb2.ps1 Normal file
View File

@@ -0,0 +1,70 @@
# https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?redirectedfrom=MSDN&view=powershell-7
Get-CimInstance Win32_Diskdrive -PipelineVariable disk |
Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition -PipelineVariable partition |
Get-CimAssociatedInstance -ResultClassName Win32_LogicalDisk |
Select-Object @{n='Disk';e={$disk.deviceid}},
@{n='DiskSize';e={$disk.size}},
@{n='DiskModel';e={$disk.model}},
@{n='Partition';e={$partition.name}},
@{n='RawSize';e={$partition.size}},
@{n='DriveLetter';e={$_.DeviceID}},
VolumeName,Size,FreeSpace
# Pour voir toutes les entrées:
#Get-CimInstance Win32_DiskDrive | Select-Object *
<#
InterfaceType : USB
Size : 8101900800
Caption : USB Device
Name : \\.\PHYSICALDRIVE1
MediaLoaded : True
MediaType : Removable Media
Model : USB Device
SerialNumber : AA15104200000849
#>
# Where { $_.InterfaceType -eq 'USB' }
Get-CimInstance Win32_DiskDrive -Filter "InterfaceType like 'USB%'" | Select-Object Model,SerialNumber
#Get-CimInstance Win32_LogicalDisk | % VolumeSerialNumber
#Get-CimInstance Win32_LogicalDisk | Select-Object *
<#
Name : C: | D:
Description : Disque monté local | Disque CD-ROM | Disque amovible | Connexion réseau
FreeSpace : 22891048960
Size : 89682935808
FileSystem : NTFS | FAT32 | PrlSF
ProviderName : \\Mac\Dropbox
VolumeName : BOOTCAMP | ESD-ISO | Shared Folders
VolumeSerialNumber : 02469FF7
#>
Get-CimInstance Win32_LogicalDisk | Select-Object DeviceID,Name,Description,Freespace,Size,FileSystem,ProviderName,VolumeName,VolumeSerialNumber
<#
CIM_LogicalDisk {SetPowerState, R... {Caption, Description, InstallDate, Name...}
Win32_MappedLogicalDisk {SetPowerState, R... {Caption, Description, InstallDate, Name...}
CIM_DiskPartition {SetPowerState, R... {Caption, Description, InstallDate, Name...}
Win32_DiskPartition {SetPowerState, R... {Caption, Description, InstallDate, Name...}
CIM_DiskDrive {SetPowerState, R... {Caption, Description, InstallDate, Name...}
Win32_DiskDrive {SetPowerState, R... {Caption, Description, InstallDate, Name...}
CIM_DisketteDrive {SetPowerState, R... {Caption, Description, InstallDate, Name...}
CIM_DiskSpaceCheck {Invoke} {Caption, CheckID, CheckMode, Description...}
#Win32_LogicalDiskRootDirectory {} {GroupComponent, PartComponent}
Win32_LogonSessionMappedDisk {} {Antecedent, Dependent}
CIM_LogicalDiskBasedOnPartition {} {Antecedent, Dependent, EndingAddress, StartingAddress}
Win32_LogicalDiskToPartition {} {Antecedent, Dependent, EndingAddress, StartingAddress}
CIM_LogicalDiskBasedOnVolumeSet {} {Antecedent, Dependent, EndingAddress, StartingAddress}
#Win32_DiskDrivePhysicalMedia {} {Antecedent, Dependent}
#CIM_RealizesDiskPartition {} {Antecedent, Dependent, StartingAddress}
Wi
#>
Get-CimInstance CIM_LogicalDisk | Select-Object *
Get-CimInstance CIM_DiskDrive | Select-Object *
Get-CimInstance Win32_MappedLogicalDisk | Select-Object *
Get-CimInstance Win32_DiskPartition | Select-Object *