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.