Hey,
I want the Output from this powershell-script to be added into a txt file.
No matter what I try it wont work. I tried a simple > c:\dhcp.txt, a | Out-File, Add-Content, Set-Content… Nothing works.
Can somebody help me please before punch my keyboard through the desk?
$dhcpsrvs = get-content c:\dhcpsrvs.txt
foreach ($dhcpsrv in $dhcpsrvs)
{
Write-Host $dhcpsrv
$scopeids = Get-DhcpServerv4Scope -ComputerName $dhcpsrv | select-object scopeid
foreach ($scopeid in $scopeids) {
$scope = $scopeid -split “=”[0]
$scope = $scope[1] -split “}”[0]
$scope = $scope[0]
Get-DhcpServerv4lease -ComputerName $dhcpsrv -ScopeId $scope -ClientId “78542ee9e782” -ErrorAction SilentlyContinue
}
}
This Script searches through all the scopes in a given windows dhcp server and outputs the entry where the mac is equal to the given ClientID
Thx everybody
BGL
September 28, 2017, 3:39pm
2
Just to eliminate the obvious - are you trying to write the out-file to the root of c:?
You are much better to input and output to a folder - that way you can control the permissions, only a local admin has write privilege to the root.
1 Like
BGL
September 28, 2017, 4:36pm
3
I’ve had another think about this (and my PoSH Fu is bad, really bad) but, I think you need to have an object before you pipe to the output e.g.
$dhcpsrvs = get-content c:\test\dhcpsrvs.txt
foreach ($dhcpsrv in $dhcpsrvs)
{
Write-Host $dhcpsrv
$scopeids = Get-DhcpServerv4Scope -ComputerName $dhcpsrv | select-object scopeid
foreach ($scopeid in $scopeids) {
$scope = $scopeid -split “=”[0]
$scope = $scope[1] -split “}”[0]
$scope = $scope[0]
$output = Get-DhcpServerv4lease -ComputerName $dhcpsrv -ScopeId $scope -ClientId “78542ee9e782” -ErrorAction SilentlyContinue
}
}
$output | export-csv -path "c:\test\outfile.txt" -Append
I might try and play with this later but I someone who knows what they are doing will probably helped you by then.
1 Like
Also tried to write the outputfiles to my local desktop. No luck