Powershell理解Copy Item和foreach多个服务器



我的第一篇文章我希望我做的一切都对:(

我想做的事:

  1. 从特定AD组获取服务器
  2. 检查windows错误文件夹中是否存在名为xyz的ApplicationCrash
  3. 对于表$DestinationServer和$Folderpath中定义的所有结果,我希望将它们复制到\NetworkShare\test
  4. 在复制源到目标部分中,我想将复制源到目的地的Servername添加到文件夹名称中

我该怎么做?所以我得到了一个输出,我想让他们从服务器上复制所有内容,结果是复制到特定的网络共享?

请参阅下面的我的脚本

$Computers = Get-ADGroupMember "ADGROUP" 
$result = Invoke-Command -ComputerName $Computers -ScriptBlock { Get-ChildItem C:ProgramDataMicrosoftWindowsWERReportArchive} | Select FullName,Parent,Name,PSComputerName,LastWriteTime | where {$_.Name -like '*Applikationname*'}
$fromPath = $result.Fullname
$foldername = $result.name
$Destinastionserver = $result.PSComputerName
foreach HOWTO ?
{
$Session = New-PSSession -ComputerName "$connectoserver"
Copy-Item "$frompath" -Destination "\Networksharetest$foldername" -ToSession $Session -Recurse
}

感谢的帮助

现在我已经将代码更改为:

Get-ADGroupMember "ADGROUPSERVER" | Where-Object { $_.ObjectClass -eq 'Computer'} |
ForEach-Object {
Invoke-Command -ComputerName $PSItem.Name -ScriptBlock {
Get-ChildItem -Path 'C:ProgramDataMicrosoftWindowsWERReportArchive'} |
Select-Object -Property FullName,
Parent,
Name,
PSComputerName,
LastWriteTime |
where {$PSItem.Name -like '*App*'} |
Select-Object -Property Fullname,
name,
PSComputerName
} |
ForEach {
$Session = New-PSSession -ComputerName $PSItem.PsComputername
Copy-Item "$PSItem.FullName" -Destination "\serversharefolder$($PSitem.PSComputerName)" -ToSession $Session -Recurse -WhatIf
}

我收到这个错误,为什么Servername后面有一个:字符串

Copy-Item : The path '\serversharefolderSERVERNAME' is not valid. Only absolute paths are supported on remote copy operations.
At line:19 char:5
Copy-Item "$PSItem.FullName" -Destination "\servershare ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo          : InvalidArgument: (\servershareDataSERVERNAME:String) [Copy-Item], ArgumentException
FullyQualifiedErrorId : RemotePathIsNotAbsolute,Microsoft.PowerShell.Commands.CopyItemCommand 

如果我在关注你发布的内容,也许,只是也许,这个重构(未经测试,在我的脑海中(应该有助于为你指明正确的方向。

请记住,在远程会话中,变量具有作用域。因此,如果它们没有被看到,那么您需要处理变量范围,比如使用$Using修饰符。

这种格式是不需要的,这只是我让老眼睛更容易阅读的方式;-}当然,所有人都有自己的偏好。

Get-ADGroupMember 'ADGROUP"' | 
ForEach-Object {
Invoke-Command -ComputerName $PSItem.Name -ScriptBlock { 
Get-ChildItem -Path 'C:ProgramDataMicrosoftWindowsWERReportArchive'} | 
Select-Objeft -Property FullName, 
Parent, 
Name, 
PSComputerName, 
LastWriteTime | 
where {$PSItem.Name -like '*Applikationname*'} | 
Select-Object -Property Fullname,
name,
PSComputerName
} | 
ForEach {
$Session = New-PSSession -ComputerName $connectoserver
Copy-Item -Path $PSItem.FullName -Destination "\Networksharetest$($PSitem.PSComputerName)" -ToSession $Session -WhatIf
}

根据您的意见更新

根据复印件文档

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-7.1

# Example 5: Copy a file to a remote computer
$Session = New-PSSession -ComputerName "Server01" -Credential "ContosoUser01"
Copy-Item "D:Folder001test.log" -Destination "C:Folder001_Copy" -ToSession $Session

# Example 6: Copy a folder to a remote computer
$Session = New-PSSession -ComputerName "Server02" -Credential "ContosoUser01"
Copy-Item "D:Folder002" -Destination "C:Folder002_Copy" -ToSession $Session

# Example 7: Recursively copy the entire contents of a folder to a remote computer
$Session = New-PSSession -ComputerName "Server04" -Credential "ContosoUser01"
Copy-Item "D:Folder003" -Destination "C:Folder003_Copy" -ToSession $Session -Recurse

然而,目的地肯定已经存在。您必须提前创建,或者使用-Force开关/参数在副本上强制创建。

New-Item -Path "\Networksharetest$($PSitem.PSComputerName)"
Copy-Item -Path $PSItem.FullName -Destination "\Networksharetest$($PSitem.PSComputerName)" -ToSession $Session -WhatIf
# Or 
Copy-Item -Path $PSItem.FullName -Destination "\Networksharetest$($PSitem.PSComputerName)" -ToSession $Session -Force -WhatIf
Copy-Item
[-Path] <String[]>
[[-Destination] <String>]
[-Container]
[-Force]
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Recurse]
[-PassThru]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[-FromSession <PSSession>]
[-ToSession <PSSession>]
[<CommonParameters>]

相关内容

最新更新