Hi All Scripting Master,
I hope so this is good forum. I want write two script with arguments
- Create one folder (without permission to delete main folder, file and subfolders for users - only administrators can delete) in all domain Servers
Second Script: - Delete .xls files from all created new_folders in all servers and export log to csv
- Send mail to user with attachment ( .csv)
But I have a few problems: - How to set up ACL for folder without permission to delete
- I have problem with Invoke-Command if I want use it I must enable WIN-RM on all Servers but I cann’t - Can I replace this command with another one? - If yes - on which?
- I don’t know why Export-CSV don’t add logs which files are deleted
- Can I write all in one script?
Script 1)
#import module
Import-Module ActiveDirectory
#define variables
$path = "D:\exist_folder\new_folder"
$servers = Get-ADComputer -Filter {SamAccountName -like "SRV*"}
# create folder or no if exist
Foreach ($srv in $servers){
If(!(test-path $path))
Invoke-Command -ComputerName $srv.Name -ScriptBlock { New-Item -ItemType Directory -Force -Path $path }
Script 2
# Remove all .xls files
$path = "D:\exist_folder\new_folder"
get-childitem $path -include *.xls -recurse | foreach ($_) {remove-item $_.fullname} | Export-Csv -Path c:\path\del.csv
#Send mail with attachment to user which files were deleted
Send-MailMessage -To "User1 " -From "User2 <user2@user.com" -Subject "Delete .xls" -SmtpServer "mail.user.com" -Attachments "C:\path\del.csv"
}