Switching powershell to different Credential

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

  • You need to be an Admin to run script, and Admin account has to be the same on local and remote machine. Try starting PS by right click PS shortcut and select Run As Admin.

    – 

  • I want to avoid running powershell as admin, some users are not able to run as admin on there machine due to local policies out side of the teams control. the “new PSSession” works for most situations its jsut this one specifice command at the moment that is causing issues

    – 

  • You need to run As Admin if you are changing a different account. If users are only changing there own account than code should work.

    – 

Leave a Comment