Switching powershell to different Credential

81 Views Asked by At

If this has been answered apologies but i cant find a working solution. I have this very simple script belowthat the user has to enter there username and password for it to work.

The first part works fine, it runs on the DHCP server and loops though the scopes as sets the lease time. The issue i have is with the last command

Invoke-DhcpServerv4FailoverReplication -scope $scope.ScopeId -Force

This fails with the error message below, which i partialy understand that the Credentials can't "hop", clearly the cmdlet "Invoke-DhcpServerv4FailoverReplication" also has a part that runs aginst the secondary Failover DHCP sever.

I also tried to start the "New_PSSesion" on the local machine

$Sessionnew = New-PSSession -Credential $Credentials

but i get the same issue with it failing at the permission stage is there any simple way to fix this.

Failed to get superscope information on DHCP server DHCPSecondary.net. + CategoryInfo : PermissionDenied: (DHCPSecondary.net:root/Microsoft/...overReplication) [Invoke-DhcpServerv4FailoverReplication], CimException + FullyQualifiedErrorId : WIN32 5,Invoke-DhcpServerv4FailoverReplication + PSComputerName : DHCPSecondary.net

Script

$destinationServer = "DHCPprimary.net"
$Credentials = Get-Credential -Message "Enter credentials"

$Sessionnew = New-PSSession -Credential $Credentials -ComputerName $destinationServer

$sourcedata = Invoke-Command -Session $Sessionnew -AsJob {
   
$UserLeaseDuration = "2.00:00:00"
$GuestLeaseDuration = "0.02:00:00"
$ServerLeaseDuration = "7.00:00:00"


$scopeList = Get-DhcpServerv4Scope 
foreach ($scope in $scopeList){
    switch -regex ($scope.name) 
    {
    "WIFI-CORP" {Set-DhcpServerv4Scope -ScopeId $scope.ScopeId  -LeaseDuration $UserLeaseDuration}
    "Wired" {Set-DhcpServerv4Scope -ScopeId $scope.ScopeId  -LeaseDuration $UserLeaseDuration}
    "Server" {Set-DhcpServerv4Scope -ScopeId $scope.ScopeId  -LeaseDuration $ServerLeaseDuration}
    Default {write-host ($scope.name)}
    }

    Invoke-DhcpServerv4FailoverReplication -scope $scope.ScopeId  -Force
}
    
} 

Receive-Job -Job $sourcedata -Wait -AutoRemoveJob
0

There are 0 best solutions below