2017-08-14 2 views
-1

Je peux poster le code si nécessaire (c'est plutôt long) mais le court est que j'ai un script Ps1 qui obtient des informations sur un partage (nom de fichier, date de création, etc) et crée un fichier CSV. fichier dans un emplacement. Ensuite, il change tout le csv en xlsx et supprime le csv. Lorsqu'il est exécuté manuellement, cela fonctionne parfaitement, lorsqu'il est programmé pour exécuter le planificateur de tâches avec les autorisations les plus élevées, il crée le csv, puis supprime tout, semblant ignorer la conversion en xlsx. Qu'est-ce qui ferait que le même script se comporte différemment lorsqu'il est exécuté manuellement par le biais d'un planificateur de tâches?Powershell s'exécute différemment lors de l'exécution du planificateur de tâches

CODE CI-DESSOUS:

###Make sure I can Access the Share 
net use \\Share\Share /user:USER PASS /persisten:no 

###Move the ones that exist to Back-Up Delete Backup 
Remove-Item C:\Users\USER\Desktop\OutputBk\* -recurse 
Get-ChildItem -Path "C:\Users\USER\Desktop\Output" -Recurse -File | Move-Item -Destination "C:\Users\USER\Desktop\OutputBk" 

### Output1 

Get-ChildItem -Path \\SHARE\Output1 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output1.csv -NoTypeInformation 

### Output2 

Get-ChildItem -Path \\SHARE\Output2 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output2.csv -NoTypeInformation 

### Output3 

Get-ChildItem -Path \\SHARE\ Output3-Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output3.csv -NoTypeInformation 

### Output4 

Get-ChildItem -Path \\SHARE\ Output4 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output4.csv -NoTypeInformation 

### Output5 

Get-ChildItem -Path \\SHARE\ Output5 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output5.csv -NoTypeInformation 

### Output6 

Get-ChildItem -Path \\SHARE\ Output6 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output6.csv -NoTypeInformation 

### Output7 

Get-ChildItem -Path \\SHARE\ Output7 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output7.csv -NoTypeInformation 

### Output8 

Get-ChildItem -Path \\SHARE\ Output8 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output8.csv -NoTypeInformation 

### Output9 

Get-ChildItem -Path \\SHARE\ Output9 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output9.csv -NoTypeInformation 

### Output10 

Get-ChildItem -Path \\SHARE\ Output10 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output10.csv -NoTypeInformation 

### Output11 

Get-ChildItem -Path "\\SHARE\Recycle Bin - Output11" -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output11.csv -NoTypeInformation 

### Output12 

Get-ChildItem -Path "\\SHARE\Recycle Bin - Output12" -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output12.csv –NoTypeInformation 

### Convert the CSV to XLSX 

$workingdir = "C:\Users\USER\Desktop\Output\*.csv" 
$csv = dir -path $workingdir 
foreach($inputCSV in $csv){ 
$outputXLSX = $inputCSV.DirectoryName + "\" + $inputCSV.Basename + ".xlsx" 

#### Create a new Excel Workbook with one empty sheet 
$excel = New-Object -ComObject excel.application 
$excel.DisplayAlerts = $False 
$workbook = $excel.Workbooks.Add(1) 
$worksheet = $workbook.worksheets.Item(1) 

### Build the QueryTables.Add command 
### QueryTables does the same as when clicking "Data » From Text" in Excel 
$TxtConnector = ("TEXT;" + $inputCSV) 
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1")) 
$query = $worksheet.QueryTables.item($Connector.name) 

### Set the delimiter (, or ;) according to your regional settings 
$query.TextFileOtherDelimiter = $Excel.Application.International(5) 

### Set the format to delimited and text for every column 
### A trick to create an array of 2s is used with the preceding comma 
$query.TextFileParseType = 1 
$query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count 
$query.AdjustColumnWidth = 1 

### Execute & delete the import query 
$query.Refresh() 
$query.Delete() 

### Save & close the Workbook as XLSX. Change the output extension for Excel 2003 
$Workbook.SaveAs($outputXLSX,51) 
$excel.Quit() 
} 
### To exclude an item, use the '-exclude' parameter (wildcards if needed) 
remove-item -path $workingdir -exclude *Crab4dq.csv 

### Rename Output1 

$xlspath = "C:\Users\USER\Desktop\Output\Output1.xlsx" 
$xldoc = new-object -comobject Excel.application 
$xldoc.Visible = $false 
$xldoc.DisplayAlerts = $false 
$workbook = $xldoc.Workbooks.Open($xlspath) 
$worksheet = $workbook.worksheets.item(1) 
$worksheet.name = "1" 
$workbook.Save() 
$workbook.Close() 
$xldoc.Quit() 

### Rename Output2 

$xlspath = "C:\Users\USER\Desktop\Output\Output2.xlsx" 
$xldoc = new-object -comobject Excel.application 
$xldoc.Visible = $false 
$xldoc.DisplayAlerts = $false 
$workbook = $xldoc.Workbooks.Open($xlspath) 
$worksheet = $workbook.worksheets.item(1) 
$worksheet.name = "2" 
$workbook.Save() 
$workbook.Close() 
$xldoc.Quit() 

### Rename Output3 

$xlspath = "C:\Users\USER\Desktop\Output\Output3.xlsx" 
$xldoc = new-object -comobject Excel.application 
$xldoc.Visible = $false 
$xldoc.DisplayAlerts = $false 
$workbook = $xldoc.Workbooks.Open($xlspath) 
$worksheet = $workbook.worksheets.item(1) 
$worksheet.name = "3" 
$workbook.Save() 
$workbook.Close() 
$xldoc.Quit() 

### Rename Output4 

$xlspath = "C:\Users\USER\Desktop\Output\Output4.xlsx" 
$xldoc = new-object -comobject Excel.application 
$xldoc.Visible = $false 
$xldoc.DisplayAlerts = $false 
$workbook = $xldoc.Workbooks.Open($xlspath) 
$worksheet = $workbook.worksheets.item(1) 
$worksheet.name = "4" 
$workbook.Save() 
$workbook.Close() 
$xldoc.Quit() 

### Rename Output5 

$xlspath = "C:\Users\USER\Desktop\Output\Output5.xlsx" 
$xldoc = new-object -comobject Excel.application 
$xldoc.Visible = $false 
$xldoc.DisplayAlerts = $false 
$workbook = $xldoc.Workbooks.Open($xlspath) 
$worksheet = $workbook.worksheets.item(1) 
$worksheet.name = “5" 
$workbook.Save() 
$workbook.Close() 
$xldoc.Quit() 

### Rename Output6 

$xlspath = "C:\Users\USER\Desktop\Output\Output6.xlsx" 
$xldoc = new-object -comobject Excel.application 
$xldoc.Visible = $false 
$xldoc.DisplayAlerts = $false 
$workbook = $xldoc.Workbooks.Open($xlspath) 
$worksheet = $workbook.worksheets.item(1) 
$worksheet.name = "6" 
$workbook.Save() 
$workbook.Close() 
$xldoc.Quit() 

### Rename Output7 

$xlspath = "C:\Users\USER\Desktop\Output\Output7.xlsx" 
$xldoc = new-object -comobject Excel.application 
$xldoc.Visible = $false 
$xldoc.DisplayAlerts = $false 
$workbook = $xldoc.Workbooks.Open($xlspath) 
$worksheet = $workbook.worksheets.item(1) 
$worksheet.name = "7" 
$workbook.Save() 
$workbook.Close() 
$xldoc.Quit() 

### Rename Output8 

$xlspath = "C:\Users\USER\Desktop\Output\Output8.xlsx" 
$xldoc = new-object -comobject Excel.application 
$xldoc.Visible = $false 
$xldoc.DisplayAlerts = $false 
$workbook = $xldoc.Workbooks.Open($xlspath) 
$worksheet = $workbook.worksheets.item(1) 
$worksheet.name = "8" 
$workbook.Save() 
$workbook.Close() 
$xldoc.Quit() 

### Rename Output9 

$xlspath = "C:\Users\USER\Desktop\Output\Output9.xlsx" 
$xldoc = new-object -comobject Excel.application 
$xldoc.Visible = $false 
$xldoc.DisplayAlerts = $false 
$workbook = $xldoc.Workbooks.Open($xlspath) 
$worksheet = $workbook.worksheets.item(1) 
$worksheet.name = "9" 
$workbook.Save() 
$workbook.Close() 
$xldoc.Quit() 

### Rename Output10 

$xlspath = "C:\Users\USER\Desktop\Output\Output10.xlsx" 
$xldoc = new-object -comobject Excel.application 
$xldoc.Visible = $false 
$xldoc.DisplayAlerts = $false 
$workbook = $xldoc.Workbooks.Open($xlspath) 
$worksheet = $workbook.worksheets.item(1) 
$worksheet.name = "10" 
$workbook.Save() 
$workbook.Close() 
$xldoc.Quit() 

### Rename Output11 

$xlspath = "C:\Users\USER\Desktop\Output\Output11.xlsx" 
$xldoc = new-object -comobject Excel.application 
$xldoc.Visible = $false 
$xldoc.DisplayAlerts = $false 
$workbook = $xldoc.Workbooks.Open($xlspath) 
$worksheet = $workbook.worksheets.item(1) 
$worksheet.name = "11" 
$workbook.Save() 
$workbook.Close() 
$xldoc.Quit() 

### Rename Output12 

$xlspath = "C:\Users\USER\Desktop\Output\Output12.xlsx" 
$xldoc = new-object -comobject Excel.application 
$xldoc.Visible = $false 
$xldoc.DisplayAlerts = $false 
$workbook = $xldoc.Workbooks.Open($xlspath) 
$worksheet = $workbook.worksheets.item(1) 
$worksheet.name = "12" 
$workbook.Save() 
$workbook.Close() 
$xldoc.Quit() 

Merci,

+0

Probablement en raison de la partie Excel, pas PowerShell/le script. Voir [ce poste StackOverflow] (https://stackoverflow.com/questions/35819825/powershell-scheduled-task-to-run-script-with-excel-com-object) et le lien, et [ce poste SuperUSer] (https : //superuser.com/questions/579900/why-cant-excel-open-a-file-when-run-from-task-scheduler). Ils détaillent l'étape BS de la création d'un dossier vide, et l'étape moins BS des autorisations DCOM. Si vous avez des macros, il y a un changement de reg nécessaire en fonction des macros. – gms0ulman

+0

[connexes] (https://stackoverflow.com/a/41635982/1630171). –

+0

Le dossier est ouvert au niveau d'autorisation "tout le monde", et il n'y a pas de macros - j'ai ajouté le code. –

Répondre

0

ty gms0ulman - Grâce à vos liens, j'ai trouvé la réponse.

C'est un problème d'autorisations DCOM. La seule façon que j'ai trouvée autour de tels problèmes est de configurer Excel pour qu'il s'exécute comme un utilisateur spécifique via les autorisations DCOM.

ouverts Services de composants (Démarrer -> Exécuter, tapez dcomcnfg) Drill vers le bas pour les services de composants -> Informatique -> Poste de travail et cliquez sur Configuration DCOM Faites un clic droit sur Microsoft Excel application et choisissez Propriétés dans l'identité Sélectionnez cet utilisateur et entrez l'ID et le mot de passe d'un compte utilisateur interactif (domaine ou local), puis cliquez sur OK. Le conserver en tant qu'utilisateur interactif ou le lancement ne fonctionne malheureusement pas avec le planificateur de tâches, même en réglant la tâche sur courir sous un compte qui a un accès administrateur à la machine

Merci,

0

Task Properties - Mail Trigger

espère que vous avez donné un compte qui ont l'autorisation de modifier le fichier, certains compte ne peut avoir la permission d'écrire ne pas modifier le fichier dans le dossier de partage. Après avoir donné le compte avec les privilèges requis, si le problème persiste, essayez d'ajouter "Start-Sleep -s 60" avant les commandlets qui supprime le fichier CSV. Une tâche de suppression de temps peut dépasser la tâche précédente, ce qui suppose de changer l'extension du fichier. S'il vous plaît poster le script si rien de ce qui précède ne fonctionne.

+0

Code ajouté, tout est en cours d'exécution avec la permission de l'utilisateur le plus élevé et marqué pour exécuter si l'utilisateur est connecté ou non –