Invoke-Command Credentials not passed

Francesco S. Tremamunno 41 Reputation points
2021-02-18T22:37:29.14+00:00

On 50+ DHCP server to manage, I'm trying to export in CSV all the lease for each scope.
I've defined my script that is like this:
$mag=$env:computername.substring($env:computername.Length -3)
New-PSDrive -Name X -PSProvider FileSystem -Root \ServerX\Reservations -Credential $cred -Description Temp_On_DC | Out-Null
$DHCP_Scopes = (Get-DhcpServerv4Scope | select Scopeid)
$Scope_Count = $DHCP_Scopes.count
for ($i=0; $i -lt $Scope_Count; $i++) {
$Scope = $DHCP_Scopes[$i] -replace '[a-zA-Z{}\@\=]'
Get-DhcpServerv4Lease -ScopeId $Scope | select IPAddress,@{N='ClientId';E={$.ClientId -replace '-'}},HostName,AddressState | Where-Object {$.hostname -like 'WSA*' -or $.hostname -like 'WSI*' -or $.hostname -like 'hpbox*' -or $.hostname -like 'HP*' -or $.hostname -like 'NPI*'} | sort hostname | Export-Csv X:\$mag'Scope'$scope.txt -NoTypeInformation
}
Remove-PSDrive X

I'm calling this script from another script:
$Mags = Get-Content 'Stores.txt'
foreach ($mag in $mags) {
$DHCP_Server = 'Server'+$mag+'.domain'
Write-Host $Mag
Invoke-Command -FilePath Get_Lease_Scope.ps1 -ArgumentList $cred -ComputerName $DHCP_Server -Credential $cred
}

Credential are stored with the usual command:
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList ($User, $pass)

Infact, this credential work, because i'm able to connect to the remote host.
Without -Credential $cred, i will get access denied.

When I try to map a psdrive, i'm prompted for password, like the -argumentlist is not passing the variable.
According to me, it's not going through because i made a step in the ps1 where i explicitly ask to write the $cred even is the password is encrypted.

Everything works fine after prompting for the password.
But inserting 50+ username and password can be tiresome and can lead to mistakes (as i did of course).

Where am i wrong?
i don't see typos and tried to use Powershell ISE to double check spelling and commands.

Any help will be appreciated.

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-02-19T09:08:56.503+00:00

    Hi,

    You can add the param statement param([Parameter(Mandatory=$true)]$cred) to the beginning of the script Get_Lease_Scope.ps1

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-02-19T01:18:25.903+00:00

    like the -argumentlist is not passing the variable.

    -Filepath only passes positional parameters. You have to add a param statement.

    param ($cred)
    $Mags = Get-Content 'Stores.txt'
    foreach ($mag in $mags) {
        $DHCP_Server = 'Server'+$mag+'.domain'
        Write-Host $Mag
        Invoke-Command -FilePath Get_Lease_Scope.ps1 -ArgumentList $cred -ComputerName $DHCP_Server -Credential $cred
    }
    

  2. Francesco S. Tremamunno 41 Reputation points
    2021-02-19T12:55:11.58+00:00

    It works now! Thanks @Anonymous
    Still to understand why i was prompted for credentials at the very beginning of the script.

    My credential request script is this:
    $User = (read-host -Prompt 'Input Login User: ')
    $User = 'Domain\'+$User
    $pass = (read-host -Prompt 'Input Password: ' -assecurestring) | ConvertTo-SecureString -AsPlainText -Force
    $cred = New-Object System.Management.Automation.PSCredential -ArgumentList $User, $pass

    It's funny that entering command by command works.
    In a scripts, i get access denied when login into the remote server.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.