Fill Variable with Name information from previous command - powershell

Having an bit of an issue with my Powershell script. I'm currently running this below -
Get-ADComputer -Filter * -Properties * | FT Name,OperatingSystem, LastLogonDate -AutoSize
Which lists Computers and operating systems in my Active Directory.
I want to fill this variable $Computers with the Computer names discovered in my command above.
Anyone got any idea how I do this?

$computers = (Get-ADComputer -Filter *).Name
should do the trick. At least it seems to work with a list of file names as in:
$filenames = (ls).Name

Related

Powershell simple script

I have a simple script that is made to search the members of a group introduced as a parameter, and it works properly:
$param1=$args[0]
Get-ADGroupMember "$param1" | ft name,objectclass,samaccountname
But when I try to run this other script (very similar funtionality), it doesn't show anything,, just blank:
$param1=$args[0]
Get-ADUser -Filter 'Name -like "*$param1*"' -Properties LastLogonDate,PasswordLastSet | ft Name,SamAccountName,LastLogonDate,PasswordLastSet
Someone could help me, what am I doing wrong?
PD: When I run the second command manually, replacing $param1 with a letter, it works as intended.
---NEWS---
I have tried this variation of the second script and idk why but it works:
Get-ADUser -Identity "$args" -Properties LastLogonDate,PasswordLastSet | ft Name,SamAccountName,LastLogonDate,PasswordLastSet
May it be, that the option "-Filter 'Name -like "$args"'" it's what is causing all the trouble?
It's very strange because, the second script doesn't show an error, it just doesn't show nothing and goes to the next prompt line. (I have already tried to replace the $param1 with $args like in the last example and it's the same output, nothing)
Thanks in advice :))
Alex, Try this instead. Get-ADUser -Filter "Name -like '*$($param1)*'" -Properties LastLogonDate,PasswordLastSet | ft Name,SamAccountName,LastLogonDate,PasswordLastSet

Powershell: Get number of computers connected to Active Directory by OS

I have a task to get the number of computers connected to a certain Active Directory, grouped by OS.
I figured out how to find out the name of the OS installed on a certain computer:
Get-ADComputer -Filter * -Properties * | Select-Object -ExpandProperty OperatingSystem
I am having a hard time understanding, how should I group and then count the different kind of operating system in powershell. Also in the testing enviroment I got set up, I only have one computer connected to the AD, so I really don't have room to test out my ideas. I have requested some additional virtual machines to be connected to the AD, but I would like to figure the how until I get those.
As suggested in the comments by #Scepticalist - Group-Object is the tool designed for this specific purpose.
Get-ADComputer -Filter * -Properties OperatingSystem | group-object OperatingSystem | select name,count
All Windows Servers
Get-ADComputer -Filter {operatingsystem -like 'server'} -Properties Name,Operatingsystem,OperatingSystemVersion,IPv4Address,lastlogondate | Export-Csv c:\temp\WinServers6.csv
All Windows clients
Get-ADComputer -Filter {operatingsystem -notlike 'server'} -Properties Name,Operatingsystem,OperatingSystemVersion,IPv4Address,lastlogondate | Export-Csv c:\temp\WinClients.csv
All Computers in AD
Get-ADComputer -Filter * -Properties Name,Operatingsystem,OperatingSystemVersion,IPv4Address

cmdlet to variable not being accepted

Should be a very simple script but having issues getting the output from the get-aduser to be recognized as a variable, among other things. I've tried every format of quotes and brackets I can think of but can't get a proper output. The script is just querying a specific user and exporting the AD groups to a folder named for their department, then into a text file using the name and title.
$usertocheck = Read-Host -Prompt 'Input user to check'
$depttoadd = Get-AdUser -Filter {samAccountName -eq "$usertocheck"} -Properties Department |
Select-Object -expand Department
New-Item -ItemType Directory -Force -Path "C:\Users\Public\Desktop\UserRecords\$depttoadd\"
Get-ADPrincipalGroupMembership $usertocheck | select name |
Out-File -FilePath "C:\Users\Public\Desktop\UserRecords\$($usertocheck)_$($titlelookup).txt"
Any hints would be appreciated.
It works for me, when I remove the quotes around $usertocheck in the below line ($usertocheck is a string already, so no need for quotes)
$depttoadd = Get-AdUser -Filter {samAccountName -eq $usertocheck} -Properties Department |
As a side note, you could also access the department property of the object returned by Get-AdUser like so
$depttoadd = $(Get-AdUser -Filter {samAccountName -eq $usertocheck} -Properties Department).Department
Acessing the properties of an object is from my experience the more reliable and cleaner way of getting the output you want, rather than using 'Select-Object'.
Hope this helps.

Powershell ActiveDirectory module Variable with wildcard not working

Why doesn't the below get-adcomputer commandline return any results? It's really irritating when cmdlets don't accomodate powershell syntax. At least that's what it looks like is happening here. If I do a write-output, it displays exactly what I want to be inserted in the commandline. However, when I go to use it with the get-adcomputer cmdlet, no results are returned.
PS: C:\> $Variable = "88FF"
PS: C:\> write-output "*$($Variable)*"
*88FF*
PS: C:\> Get-ADComputer -Filter {Name -like "*$($Variable)*"} -Property *
PS: C:\>
PS: C:\> Get-ADComputer -Filter {Name -like "*88FF*"} -Property *
computer1
computer2
computer3
I've tried a bunch of different variants... including even adding literal quotes to the variable by escaping them. I've been pulling my hair out trying to figure out something that should take less than 10 seconds to do.
PS: C:\> $Variable = "`"*888FF*`""
PS: C:\> $Variable
"*88FF*"
PS: C:\> PS: C:\> Get-ADComputer -Filter {Name -like $Variable} -Property *
PS: C:\>
Edit: I've also tried below variant with same exact result:
PS: C:\> Get-ADComputer -Filter {Name -like '*$Variable*'} -Property *
PS: C:\>
Give this a try:
Get-ADComputer -Filter "Name -like '*$Variable*'" -Property *
Pretty lame, it looks like this is one of the many limitations of the Powershell Active-Directory module that comes with Windows. I wasn't doing anything wrong in my original attempts. I ended up having pipeline the output to where{ } to filter it items.
Get-ADComputer -properties Name, OperatingSystem -Filter *| ?{$_.name -like "*$($Variable)*"} |ft Name, OperatingSystem -Wrap -Auto
How about this:
$myvar="*888FF*"
get-adcomputer -filter {name -like $myvar} -property *
It's really annoying, you would expect this to work but -filter just has some weird parsing rules internally I guess
$myvar="888FF"
get-adcomputer -filter {name -like "*$myvar*"} -property *
Shay's solution works beautifully (at least on v4)
I also found out that LDAPFilters will work too!
get-adcomputer -LDAPFilter "(&(name=$name*)(operatingsystem=server))"

Get computers list from certain OU in active directory?

I am using the powershell command below to get a list of computers that havent been logged into in the past 60 days. This is returning all OU computers. Is it possible to change the line below to return from a certain OU?
Get-ADComputer -Property Name,lastLogonDate -Filter {lastLogonDate -lt $then} | FT Name,lastLogonDate
From the online help page try using -SearchBase filter
C:\PS>Get-ADComputer -LDAPFilter "(name=*laptop*)" -SearchBase "CN=Computers,DC=Fabrikam,DC=com"