Question:
I have written a power-shell script that copy the files from one location to another locally on server. I am trying to assemble the files at one server location and while doing this getting permission error. Script below:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
$date = (Get-Date).ToString("_MMddyyyy") #Storing servers $Servers = Get-Content C:\server.txt #Scanning Servers and executing $Servers | ForEach-Object { invoke-command -ComputerName $_ -Scriptblock { $CompName = (Get-WmiObject -Class Win32_ComputerSystem).Name #Defining Source and Destination path $DestPath = "\\$CompName\d$\Temp\IIS_Logs_"+"$CompName" $SourcePath = "\\$CompName\d$\Logs\W3SVC85\u_ex15122116.log" #Creating new folder for storing backup New-Item -Path $DestPath -ItemType directory #Copying folder copy -Recurse -Path $SourcePath -destination $DestPath }} |
Answer:
I removed the invoke command and after changing the logic script work as expected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
$date = (Get-Date).ToString("_MMddyyyy") #Storing servers $Servers = Get-Content C:\server.txt #Scanning Servers and executing $Servers | ForEach-Object { #Defining Source and Destination path $DestPath = "d:\Temp\IIS_Logs_"+"$_" $SourcePath = "\\$_\d$\u_ex15122717.log" #Creating new folder for storing backup New-Item -Path $DestPath -ItemType directory #Copying folder Copy-Item -Recurse -Path $SourcePath -destination $DestPath } |