Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, May 2, 2014 10:35 PM
I am creating a script to collect all the machines with certificates installed on them. After gathering what type of computer I want (based on a character in the name) I run Get-ADComputer to get my computer list.
$Servername=Get-ADComputer-LDAPFilter$ComputerTypeChoice-SearchBase"OU=servers,DC=Seatimes,DC=com"|SelectName
I then need to create a variable string that looks like this \*servername\*My to place into my command to search for certificates
$Scanset=ForEach($1in$servername){"\"+$1+"\My"} - But this is not working
If I do this with a single computer name in the variable that I create myself then it works perfect. - \computername\My
If I do this from my Get-ADComputer output I get this - \@{Name=servername}\My. I get that for each server in the list with servername replaced with the actual server name of course
If I run $servername | Select * I am shown the server list with just the names. This is the last line in the code to fix and I think this will work but I can't get past it. Please help
Oh and I am still a newbie to Powershell
All replies (4)
Friday, May 2, 2014 10:42 PM âś…Answered | 1 vote
Use the -ExpandProperty parameter of the Select-Object cmdlet.
$Servername = Get-ADComputer -LDAPFilter $ComputerTypeChoice -SearchBase "OU=servers,DC=Seatimes,DC=com" | Select -ExpandProperty Name
Edit: Added code example
Friday, May 2, 2014 11:29 PM
Bill,
This line
$Servername = Get-ADComputer -LDAPFilter $ComputerTypeChoice -SearchBase "OU=servers,DC=Seatimes,DC=com" | Select Name
results in custom PowerShell objects with a Name property, rather than a simple list of names.
So instead of reading from the list like this:
$Scanset = ForEach ( $1 in $servername ) { "\" + $1 + "\My" }
try:
$Scanset = ForEach ( $1 in $servername ) { "\" + $1.Name + "\My" }
or if you are using PowerShell 4.0, you can try:
$Scanset = ForEach ( $1 in $servername.Name ) { "\" + $1 + "\My" }
Tim Curwick
MadWithPowerShell.com
Monday, May 5, 2014 3:56 PM
Thanks Tommy. That worked perfectly. And I found that the rest of my code is not working. the entire code is
#get the server type
<$ComputerTypes=@(%22P-Production%22,%22Q-QA%22,%22T-Test%22,%22A-All>")
$ComputerTypes
$ComputerTypeChoose=Read-Host 'Enter the letter for the type of computer you want'
If ($ComputerTypeChoose -eq "P") {$ComputerTypeChoice="(name=p*)"}
If ($ComputerTypeChoose -eq "Q") {$ComputerTypeChoice="(name=q*)"}
If ($ComputerTypeChoose -eq "T") {$ComputerTypeChoice="(name=t*)"}
If ($ComputerTypeChoose -eq "A") {$ComputerTypeChoice="(name=*)"}
$blankline
$Servername=Get-ADComputer -LDAPFilter $ComputerTypeChoice -SearchBase "OU=servers,DC=Domain,DC=com" | Select -ExpandProperty Name
$Scanset=ForEach($1 in $servername){"\" + $1 + "\My"}
ForEach($1 in $Scanset){$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("[\"+$1](file://\) +"My","LocalMachine")}
ForEach($1 in $Scanset){$store.Open("ReadOnly")}
ForEach($1 in $Scanset){$store.Certificates | Select * | Format-Table FriendlyName,NotBefore,NotAfter,DnsNameList -auto -wrap}
I am receiving an error on the line ForEach($1 in $Scanset){$store.Open("ReadOnly")}
the error is 'Exception calling "open" with "1" argument(s). "The parameter is incorrect." should I be running these as seperate ForEach commands or piping one into the other?
Monday, May 5, 2014 3:58 PM
Tim,
I am running version 3 of Powershell.
When I tried your command it replaced the name with another '\. So I ended up with \\My for each name
tommymanard's option worked for me