问题
I'm trying to compile the script to manually connect to each server and then perform the Powershell command remotely, and if the Powershell module is not found, then failback to the native command.
The script is like the following:
Get-DfsrMember | Select-Object -ExpandProperty ComputerName -Unique | Sort-Object | ForEach-Object {
$session = New-PSSession -ComputerName $_
Invoke-Command -Session $session -ScriptBlock {
Try {
# User Powershell Module DFSR to force Replicate all DFS server content to all its peers
Try {
Write-Host "Processing Server ... $($_) using DFSR Powershell Module" -ForegroundColor Yellow
Import-Module -Name 'DFSR' -ErrorAction Stop
Update-DfsrConfigurationFromAD
Sync-DfsReplicationGroup
}
Catch {
Write-Warning -Message "PowerShell Module not found"
Write-Warning -Message $Error[0].Exception.Message
}
Finally {
Write-Host "All DFS Servers has been updated succesfully using DFSR Powershell Module" -ForegroundColor Green
Exit
}
# User the builtin DFSR command to force Replicate all DFS server content to all its peers
Try {
Write-Host "Processing Server ... $($_) using DFSR native command" -ForegroundColor Yellow
Dfsrdiag PollAD
Dfsrdiag SyncNow
}
Catch {
Write-Warning -Message "Unable to execute command"
Write-Warning -Message $Error[0].Exception.Message
}
Finally {
Write-Host "All DFS Servers has been updated succesfully using DFSR builtin command" -ForegroundColor Green
Exit
}
}
Catch {
Write-Warning -Message "[PROCESS] Something wrong happened, nothing is updated"
Write-Warning -Message $Error[0].Exception.Message
}
Finally {
Clear-Variable * -Scope Global
[System.GC]::Collect()
}
}
Remove-PSSession $session
}
The script has been executed locally on my laptop as DOMAIN\Enterprise.Administrator
However, the error is like the below, three errors for each of the servers?
Cannot overwrite variable false because it is read-only or constant.
+ CategoryInfo : WriteError: (false:String) [Clear-Variable], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotWritable,Microsoft.PowerShell.Commands.ClearVariableCommand
+ PSComputerName : DFS-ServerA
Cannot overwrite variable true because it is read-only or constant.
+ CategoryInfo : WriteError: (true:String) [Clear-Variable], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotWritable,Microsoft.PowerShell.Commands.ClearVariableCommand
+ PSComputerName : DFS-ServerA
The variable cannot be validated because the value 0 is not a valid value for the MaximumErrorCount variable.
+ CategoryInfo : MetadataError: (:) [Clear-Variable], ValidationMetadataException
+ FullyQualifiedErrorId : ValidateSetFailure,Microsoft.PowerShell.Commands.ClearVariableCommand
+ PSComputerName : DFS-ServerA
Cannot overwrite variable false because it is read-only or constant.
+ CategoryInfo : WriteError: (false:String) [Clear-Variable], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotWritable,Microsoft.PowerShell.Commands.ClearVariableCommand
+ PSComputerName : DFS-ServerB
Cannot overwrite variable true because it is read-only or constant.
+ CategoryInfo : WriteError: (true:String) [Clear-Variable], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotWritable,Microsoft.PowerShell.Commands.ClearVariableCommand
+ PSComputerName : DFS-ServerB
The variable cannot be validated because the value 0 is not a valid value for the MaximumErrorCount variable.
+ CategoryInfo : MetadataError: (:) [Clear-Variable], ValidationMetadataException
+ FullyQualifiedErrorId : ValidateSetFailure,Microsoft.PowerShell.Commands.ClearVariableCommand
+ PSComputerName : DFS-ServerB
回答1:
When running Invoke-Command against remote sessions, local variables from the calling session are not defined in the remote sessions. You either need to pass in those variables as arguments to a script block or use the using:
scope modifier. The syntax is $using:variable
.
In your case, the local variable is $_
. So you would need to use $using:_
.
Write-Host "Processing Server ... $($using:_) using DFSR Powershell Module" -ForegroundColor Yellow
Keep in mind that the using:
scope modifier only reads variables and does not allow writes.
来源:https://stackoverflow.com/questions/63719121/using-try-catch-command-to-execute-powershell-command-remotely-and-then-fallback