修改Powershell脚本以针对多个不同的计算机目标执行

修改Powershell脚本以针对多个不同的计算机目标执行,powershell,active-directory,Powershell,Active Directory,我需要修改下面的脚本,这样就可以对所有DFS服务器成员或多个服务器执行该脚本,而不是手动将RDP绑定到每个服务器,然后执行该脚本 当$ComputerName=$env:ComputerName时,现有脚本工作,但当目标服务器是多个服务器时,现有脚本不工作 以下脚本用途:检查并报告DFS文件服务器复制的积压工作。如果导出的结果也是.CSV文件,这样就可以根据服务器名称对其进行排序,那就太好了 Param ( [String[]]$ReplicationGroupList = ("

我需要修改下面的脚本,这样就可以对所有DFS服务器成员或多个服务器执行该脚本,而不是手动将RDP绑定到每个服务器,然后执行该脚本

$ComputerName=$env:ComputerName
时,现有脚本工作,但当目标服务器是多个服务器时,现有脚本不工作

以下脚本用途:检查并报告DFS文件服务器复制的积压工作。如果导出的结果也是.CSV文件,这样就可以根据服务器名称对其进行排序,那就太好了

Param (
    [String[]]$ReplicationGroupList = ("")
)

$RGroups = Get-WmiObject  -Namespace "root\MicrosoftDFS" -Query "SELECT * FROM DfsrReplicationGroupConfig"
#If replication groups specified, use only those.
if ($ReplicationGroupList) {
    $SelectedRGroups = @()
    foreach ($ReplicationGroup IN $ReplicationGroupList) {
        $SelectedRGroups += $rgroups | Where-Object { $_.ReplicationGroupName -eq $ReplicationGroup }
    }
    if ($SelectedRGroups.count -eq 0) {
        Write-Error "None of the group names specified were found, exiting"
        exit
    }
    else {
        $RGroups = $SelectedRGroups
    }
}

$ComputerName = Get-DfsrMember | Select-Object -ExpandProperty ComputerName -Unique | Sort-Object
$Succ = 0
$Warn = 0
$Err = 0
Start-Transcript -path "$([Environment]::GetFolderPath("Desktop"))\dfsr1.txt"
foreach ($Group in $RGroups) {
    $RGFoldersWMIQ = "SELECT * FROM DfsrReplicatedFolderConfig WHERE ReplicationGroupGUID='" + $Group.ReplicationGroupGUID + "'"
    $RGFolders = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query  $RGFoldersWMIQ
    $RGConnectionsWMIQ = "SELECT * FROM DfsrConnectionConfig WHERE ReplicationGroupGUID='" + $Group.ReplicationGroupGUID + "'"
    $RGConnections = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query  $RGConnectionsWMIQ
    foreach ($Connection in $RGConnections) {
        $ConnectionName = $Connection.PartnerName#.Trim()
        if ($Connection.Enabled -eq $True) {
            if (((New-Object System.Net.NetworkInformation.ping).send("$ConnectionName")).Status -eq "Success") {
                foreach ($Folder in $RGFolders) {
                    $RGName = $Group.ReplicationGroupName
                    $RFName = $Folder.ReplicatedFolderName

                    if ($Connection.Inbound -eq $True) {
                        $SendingMember = $ConnectionName
                        $ReceivingMember = $ComputerName
                        $Direction = "inbound"
                    }
                    else {
                        $SendingMember = $ComputerName
                        $ReceivingMember = $ConnectionName
                        $Direction = "outbound"
                    }

                    $BLCommand = "dfsrdiag Backlog /RGName:'" + $RGName + "' /RFName:'" + $RFName + "' /SendingMember:" + $SendingMember + " /ReceivingMember:" + $ReceivingMember
                    $Backlog = Invoke-Expression -Command $BLCommand

                    $BackLogFilecount = 0
                    foreach ($item in $Backlog) {
                        if ($item -ilike "*Backlog File count*") {
                            $BacklogFileCount = [int]$Item.Split(":")[1].Trim()
                        }
                    }

                    if ($BacklogFileCount -eq 0) {
                        $Color = "white"
                        $Succ = $Succ + 1
                    }
                    elseif ($BacklogFilecount -lt 10) {
                        $Color = "yellow"
                        $Warn = $Warn + 1
                    }
                    else {
                        $Color = "red"
                        $Err = $Err + 1
                    }
                    Write-Output "$BacklogFileCount files in backlog $SendingMember->$ReceivingMember for $RGName"

                } # Closing iterate through all folders
            } # Closing  If replies to ping
        } # Closing  If Connection enabled
    } # Closing iteration through all connections
} # Closing iteration through all groups
Write-Output "$Succ successful, $Warn warnings and $Err errors from $($Succ+$Warn+$Err) replications."
Stop-Transcript

$file = "$([Environment]::GetFolderPath("Desktop"))\dfsr1.txt"

get-content $file |
Select-Object -Skip 18 |
set-content "$file-temp"
Move-Item "$file-temp" $file -Force

$emailrecipients = "boss@it.com";
$emailbody = Get-Content -Path "$([Environment]::GetFolderPath("Desktop"))\dfsr1.txt" -Raw

Send-MailMessage -to $emailrecipients -smtpserver smtp.domain.COM -from "$env:COMPUTERNAME@$env:userdnsdomain" -subject "DFSR Report for $(get-date -format dd/MM/yyyy) from $env:COMPUTERNAME" -body $emailbody;

Remove-Item "$([Environment]::GetFolderPath("Desktop"))\dfsr1.txt"

假设WinRM已在环境中配置,则无需修改脚本-只需使用
Invoke命令在远程目标上按原样执行即可:

$RemoteDFSServers = "dfs01","dfs2","dfs3"

Invoke-Command -FilePath C:\path\to\existing\script.ps1 -ComputerName $RemoteDFSServers

假设WinRM已在环境中配置,则无需修改脚本-只需使用
Invoke命令在远程目标上按原样执行即可:

$RemoteDFSServers = "dfs01","dfs2","dfs3"

Invoke-Command -FilePath C:\path\to\existing\script.ps1 -ComputerName $RemoteDFSServers

嗨,Mathias,C:drive可以在网络驱动器上更改为UNC路径吗?@SeniorSystemsEngineer
-FilePath
是一个本地路径,因此只要可以从源会话访问它,是的,这很好,谢谢Mat。嗨,Mathias,C:驱动器是否可以在网络驱动器上更改为UNC路径?@SeniorSystemsEngineer
-FilePath
是一个本地路径,因此只要可以从源会话访问它,是的,这很好,谢谢。