Author Posts

November 21, 2017 at 1:18 pm

Hi All Scripting Master,

I hope so this is good forum. I want write two script with arguments
1) Create one folder (without permission to delete main folder, file and subfolders for users – only administrators can delete) in all domain Servers
Second Script:
1) Delete .xls files from all created new_folders in all servers and export log to csv
2) Send mail to user with attachment ( .csv)
But I have a few problems:
1) How to set up ACL for folder without permission to delete
2) 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?
3) I don't know why Export-CSV don't add logs which files are deleted
4) 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 

November 21, 2017 at 1:29 pm

Let's start with the most obvious I could see: In your first script in your foreach loop you're testing the path on this computer where you run this script – not on the remote computer. Therefore you would have to include the Test-Path part inside your script block.