将文件夹和内容复制到远程位置的 Powershell 脚本

Abh*_*hek 5 powershell

我编写了一个 power-shell 脚本,将文件从一个位置复制到服务器本地的另一个位置。我试图在一个服务器位置组装文件,但在执行此操作时出现权限错误。脚本如下:-

$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 


}}
Run Code Online (Sandbox Code Playgroud)

Abh*_*hek 2

我删除了调用命令,并在更改逻辑脚本后按预期工作。

$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 

}
Run Code Online (Sandbox Code Playgroud)