I have a script to check active directory for windows 7 computers, ping them and report back the ip address.
The export-csv function writes the correct fields and data, except the IP address. The IP address shows as "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection"
Copy of Script
Function Get-Win7 {
$Win7list = get-adcomputer -filter {(operatingsystem -like "Windows 7 Professional") -and (enabled -eq "True")} -properties operatingsystem, lastlogondate
foreach ($pc in $Win7list){
$pingtest = test-connection $pc.name -erroraction silentlycontinue -errorvariable pingfail
if (get-variable -name pingfail -erroraction silentlycontinue) {
if ($pingfail.exception -match "failed"){
$IP3 = "No IP"
$pingfall = $null
}
}
$pc.IP = New-Object -TypeName PSCustomObject -Property #{IPAddress = $pingtest.IPV4Address.ToString | select-object -first 1}
$PC |select-object name,operatingsystem, lastlogondate, IP |sort-object lastlogondate | export-csv -path c:\users\{user}\desktop\Win7-GSN.csv -notypeinfo -append
}
}
Any help is appreciated.
You can use a calculated property in Select-Object, which allows you to add a custom property and give it a custom value. Here we can add a custom property IP and give it the IPV4Address.
$PC | Select-Object name,operatingsystem, lastlogondate, #{n='IP';e={$pingtest[0].IPV4Address}}
As a result, you can remove the $pc.IP = ... line entirely.
Related
I am trying to get write a script where I can get all of the machine within my domains. here is what I found so far however I need to add additional information and still unable to get the correct information to get pull out. If someone can help me this will be great.
Get-ADComputer -Filter 'operatingsystem -like "*Windows server*" -and enabled -eq "true"' -Properties Name,Operatingsystem, OperatingSystemVersion, OperatingSystemServicePack,IPv4Address | Sort-Object -Property Operatingsystem | Select-Object -Property Name,Operatingsystem, OperatingSystemVersion, OperatingSystemServicePack, IPv4Address| ft -Wrap –Auto
I still need to be able to grab the MAC Address from all machines as well domains the machine belong to. and to make it worst I need to figure out how to export all of the data to CSV.
You will need to loop over the computers and get the MAC address individually inside the loop:
# Get-ADComputer returns these properties by default:
# DistinguishedName, GroupCategory, GroupScope, Name, ObjectClass, ObjectGUID, SamAccountName, SID
$props = 'OperatingSystem', 'OperatingSystemVersion', 'OperatingSystemServicePack', 'IPv4Address'
$result = Get-ADComputer -Filter "operatingsystem -like '*Windows server*' -and enabled -eq 'true'" -Properties $props |
Sort-Object OperatingSystem | ForEach-Object {
$mac = if ((Test-Connection -ComputerName $_.Name -Count 1 -Quiet)) {
# these alternative methods could return an array of MAC addresses
# get the MAC address using the IPv4Address of the computer
(Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'" -ComputerName $_.IPv4Address).MACAddress
# or use
# Invoke-Command -ComputerName $_.IPv4Address -ScriptBlock { (Get-NetAdapter | Where-Object {$_.Status -eq 'Up'}).MacAddress }
# or
# (& getmac.exe /s $_.IPv4Address /FO csv | ConvertFrom-Csv | Where-Object { $_.'Transport Name' -notmatch 'disconnected' }).'Physical Address'
}
else { $mac = 'Off-Line' }
# return the properties you want as object
$_ | Select-Object Name, OperatingSystem, OperatingSystemVersion, OperatingSystemServicePack, IPv4Address,
#{Name = 'MacAddress'; Expression = {#($mac)[0]}}
}
# output on screen
$result | Format-Table -AutoSize -Wrap
# or output to CSV file
$result | Export-Csv -Path 'X:\Wherever\ADComputers.csv' -NoTypeInformation -UseCulture
Active directory computer object doesn't contain the MAC address attribute , so you will not be able to get the info needed using active directory object only; but instead you can use the "IPv4Address" attribute of the AD computer object and query the DHCP server to find the machines MAC address and place the output data as "custompsobject" then export the result as C.V sheet.
Also if you have System center configuration manager "SCCM" you can query its database to generate a report with all needed data (Name,Operatingsystem, OperatingSystemVersion, OperatingSystemServicePack,IPv4Address and MAC address)
i got a client who wants to find all of the companys installed programs i wrote a script but i dont want the script show me the same same programs for each comuter every time,i want to see overall installations
$computers = get-adcomputers -filter *
foreach($computer in $computers){
Get-ItemProperty
HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-
Table –AutoSize}
I did not test this, but you can try
$computers = (Get-ADComputer -Filter *).DNSHostName # or use .Name or .CN
$software = Invoke-Command -ComputerName $computers {
Get-ItemProperty -Path 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
}
$software | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate -Unique |
Format-Table -AutoSize
P.S.1 You need to have admin permissions on all computers to do this
P.S.2 Don't forget there is also HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
Apparently you are running into problems where computers are off-line.
To overcome that, you need to add a loop so you can test if a machine is reachable or not.
$computers = (Get-ADComputer -Filter *).Name # or use .CN
# loop through the collection and (if reachable) get the software list
$result = foreach ($computer in $computers) {
# test if the computer is online
if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
# output the properties you need to get collected in variable $result
Invoke-Command -ComputerName $computer {
Get-ItemProperty -Path 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
} | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
}
else {
Write-Warning "Computer $computer is off-line"
}
}
$software = $result | Select-Object * -Unique
# output to console
$software | Format-Table -AutoSize
# output to CSV file
$software | Export-Csv -Path 'D:\Software.csv' -NoTypeInformation
I'm currently connecting to a domain controller that is on a different domain. I'm trying to automate a report that tells us if a user is enabled and the last time they logged in. I'm looking to either add the domain name to the excel output or even rename the tabs inside the workbook. Currently I just name the excel sheet by the IP of the domain controller. I have a total of 8 domains Thank you for any help.
$domainserver = "ipaddress1" , "ipaddress2" , "ipaddress3"
foreach ($s in $domainserver){
Get-ADUser -Credential $Credential -Server $s -Filter {Enabled -eq $TRUE} -Properties Name,SamAccountName,LastLogonDate |
Where {($_.LastLogonDate -lt (Get-Date).AddDays(-30)) -and ($_.LastLogonDate -ne $NULL)} |
Select Name,SamAccountName,LastLogonDate | Sort-Object LastLogonDate |
Export-Csv C:\temp\$s.csv -NoTypeInformation
}
Run Get-AdDomain against the same server, to find domain details:
$domainserver = "ipaddress1" , "ipaddress2" , "ipaddress3"
foreach ($s in $domainserver)
{
$domain = Get-AdDomain -Server $s -Credential $Credential
Get-ADUser -Credential $Credential -Server $s -Filter {Enabled -eq $TRUE} -Properties Name,SamAccountName,LastLogonDate |
Where {($_.LastLogonDate -lt (Get-Date).AddDays(-30)) -and ($_.LastLogonDate -ne $NULL)} |
Select-Object -Property Name,SamAccountName,LastLogonDate,#{Label='Domain'; Expression={$domain.DnsRoot}} |
Sort-Object LastLogonDate |
Export-Csv C:\temp\$s.csv -NoTypeInformation
}
$ErrorActionPreference = 'SilentlyContinue'
$ComputerName =Get-ADComputer -Filter {(Name -like "*")} -SearchBase "OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM" | Select-Object -ExpandProperty Name
$results = #{}
ForEach ($computer in $ComputerName) {
$Results += Get-NetAdapter -CimSession $ComputerName | Select-Object PsComputerName, InterfaceAlias, Status, MacAddress
}
$results | Export-csv -path C\users\bret.hooker\desktop\macaddress.csv -Append
Please note the base and filter are just examples and not the actual code due to work place confidentiality. Code currently will pull from AD all computer name, then will run the ForEach command to get the NetAdapter Information. I am unable to get it to output to the CSV file however. Any advice would be great.
My recommendations are 1) don't continuously append objects to an array, 2) avoid the -Append parameter of Export-Csv, and 3) take advantage of the pipeline. Example:
$computerNames = Get-ADComputer -Filter * -SearchBase "OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM" | Select-Object -ExpandProperty Name
$computerNames | ForEach-Object {
Get-NetAdapter -CimSession $_ | Select-Object PSComputerName,InterfaceAlias,Status,MACAddress
} | Export-Csv "C\users\bret.hooker\desktop\macaddress.csv" -NoTypeInformation
List all active machineaccounts in the current domain
$ComputerScan = #(Get-QADComputer -sizelimit $sizelimit -IncludedProperties LastLogonTimeStamp -WarningAction SilentlyContinue -Inactive:$false -OSName $computerFilter | where {$_.AccountIsDisabled -eq $false} )
# Create list of computers
ForEach ($Computer in $ComputerScan){
$compObj = New-Object PsObject -Property #{
Computer = $computer
Credentials = $credentials
Domain = $domain
}
$computers += $compObj
}
I am doing a foreach on $computers after this but I would like to have a exclusionlist..
Preferably formatted like this
computer1
server4
computet4
But, how?
Greetings from Norway!
A few improvements to the computer query:
LastLogonTimeStamp is returned by default, no need to include it
-Inactive is $false by default, no need to specify it.
Instaed of using where-object, use ldap filters to get enabled computers
$computerScan = Get-QADComputer
-LdapFilter '(!(userAccountControl:1.2.840.113556.1.4.803:=2))'
-Sizelimit $sizelimit -WarningAction SilentlyContinue
-OSName $computerFilter |
Select-Object -ExpandProperty Name
$ComputerScan = #('blah', 'bluh', 'blih', 'bloh')
$ExclusionList = #('blih', 'blah')
$ComputerScan | where { $ExclusionList -notcontains $_ } | Write-Host