Powershell split items into text file into multiple variables - powershell

I am trying to run a software script against my servers, to see exactly what software is installed and what version. I have the list of the servers stored in a text file "SystemList.txt" that is formatted like you see below.
SERVER001
SERVER002
SERVER003
SERVER004
I need to pull the server names out of the file and use them in my csv that I need to look like this:
Here is the code:
$computers = Get-Content -path
D:\Users\stephen.lyons.sa\Documents\SystemList\SystemList.txt
Foreach($computer in $computers)
{
$computer
$env:COMPUTERNAME
Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*
|Select-Object Displayname, DisplayVersion, Publisher, InstallDate | Out-
file \\txusocts002\d$\APSO\Steve\Test\$env:COMPUTERNAME.csv
}

Perhaps I am missing something, but would this meet your CSV file need?
Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object -Property DisplayName,DisplayVersion,Publisher,InstallDate |
Export-Csv -Path my.csv

Related

How do I get a list of the servers as well as respective application and version of that very same app to be exported properly into excel?

With this script, I am able to find a specific application on a list of multiple remote devices and determine the version number of the application on their corresponding host system. This is outputted beautifully in the PS window. However, I am having trouble exporting the results properly into excel, that is, I want each property (Name, Version, PSComputerName) to be in a separate column vs all in one column. So far, I've tried the following
$list = Get-Content -Path C:\Users\bob\AppList.txt
$Servers = Get-Content -Path C:\Users\bob\ServerList.txt
foreach ($Serv in $Servers) {
Get-WmiObject -Namespace ROOT\CIMV2 -Class Win32_Product -ComputerName $Serv |
Select-Object -Property Name, Version, PSComputerName |
Where-Object -FilterScript { $_.Name -like "*$list*" } |
Export-Csv -Path C:\Users\bob\ServerListResults.csv
}
This resulted in simply just one device's information being extracted while every other cell was empty
I added -append at the very end! Thank you anyways!

Powershell not storing foreach loop info in Variable

I am trying to collect a list of the viewers installed on a set of servers. I am trying to loop through that list and run a wmi query and store the results and export a table with with the wmi result and server name next to it.
I am running this on server 2012
$computers = Get-Content C:\computers.txt
$WMIQuery = foreach ($computer in $computers){Get-WmiObject -Class
Win32_Product | where-object {$_.name -match "Microsoft Viewer*"}}
$WMIQuery
$WMIQuery | Out-File c:\Viewers.txt
Desired Results
Server Name Object1 Object2
Server1 Microsoft Excel Viewer Microsoft Visio Viewer
I output the file and get a blank txt file.
foreach ($computer in (Get-Content -Path "C:\computers.txt")) {
Get-WMIObject -ComputerName $computer -Class Win32_Product |
Where-Object {$_.name -match "Microsoft Viewer" } |
Out-File -Append -Path "C:\viewers.txt"
}
Your original code wasn't identifying the computer to perform Get-WMIObject against, so it was looking at only the computer that you were running the script on.
If there are many products on the remote computer, you may want to consider filtering on the remote computer instead of locally, so as to avoid transferring large amounts of data over what may be a slower-than-ideal network:
foreach ($computer in (Get-Content -Path "C:\computers.txt")) {
Get-WMIObject -ComputerName $computer -Class Win32_Product -Filter "Name LIKE '*Microsoft Viewer*'"|
Out-File -Append -Path "C:\viewers.txt"
}
(I think I have the filter syntax correct; I seem to have to hack at it every time I write a new filter...)
I don't have enough rep to add a comment, but Jeff is correct. However, there are still issues with the original poster's query. The following piece of code will yield no results based on the examples provided by the poster:
{$_.name -match "Microsoft Viewer*"}
That needs to either be changed to
{$_.name -like "*Microsoft*Viewer*"}
or
{$_.name -match "Microsoft.*?Viewer"}

Outputing contents to CSV with Powershell

I use this site alot while learning Powershell. Its been a great help so far!
Here is my issue:
I am trying to write a script that will take workstation names, pull the workstation description from WMI and output both the workstation name and description found into a new .csv file.
Here is what I have so far:
Get-Content -Path "workstation_names.csv" | Select-Object Workstations |
Foreach-Object { Get-WmiObject -Class Win32_OperatingSystem -ComputerName $workstations | Select Workstations, Description | Export-CSV -Path "results.csv" -NoTypeInformation }
The workstation_names.csv has a column named "Workstations" and under it each cell has a single workstation name in it. Currently my script will create an output file that will have two columns in it. One called "Workstations" and the other called "Description". The Workstation column is empty, and the Description column only has the results for the first workstation description in it (even though I have 10 workstation names listed in the workstation_names.csv file).
I am sitting here scratching my head with my n00bish knowledge of Powershell. I know I PROBABLY need to so something with the array that is created from the first .csv file but I am not sure how to code what I need. Any help??
Probably something like this should do the trick:
$inFile = "workstation_names.csv"
$results = #()
Import-CSV -path $inFile -header Workstations |
% {$results += Get-WmiObject -Class Win32_OperatingSystem `
-ComputerName $_.Workstations | Select PSComputerName, Description}
$results | Export-Csv -Path "results.csv" -NoTypeInformation
So to elaborate on this, you aren't really importing the CSV, you are importing the raw text file, which is confusing, since I don't really understand how anything works for you in that case. Here the script will create an array to store the results, import file as CSV, loop through the valuables and output results to the CSV.
4c74356b41's script will fail for several reasons
It's -header not -headers
there is a typo %_ instead of $_
select Workstations won't work as that's no property of gwmi
This only slightly reworked script should do.
$inFile = "workstation_names.csv"
$results = #()
Import-CSV -path $inFile -header Workstations |
% {$results += Get-WmiObject -Class Win32_OperatingSystem `
-ComputerName $_.Workstations | Select PSComputerName, Description}
$results | Export-Csv -Path "results.csv" -NoTypeInformation
To see which properties are available see the following output
GWmi -Class Win32_OperatingSystem -ComputerName localhost|gm|out-gridview

how do i out put from AD or txt

I am trying to run this programme against a a list of remote pc/servers either by AD out TXT and display them in either csv or html if any one can offer some help or advise I would be greatly appreciative.
My only limitation is all my machines run powershell v2 only
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Format-Table –AutoSize
You want to take the output of that command and put it in a file? PowerShell has a lot of tools to do this. However, you need to remove the Format-Table command first.
See, Format-Table is all about making your command output look really good in a PowerShell window, so it's got a lot of hard returns and columns and things defined in it which make sense to the console, but look like garbage when you export it.
For data like this, I think Comma Separated Value is probably the way to go.
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Export-CSV -NoTypeInfo -Path \\server\share\$($env:ComputerName)_Programs.csv
This example will export a CSV, omitting the import-helper info PowerShell normally adds, using the -NoTypeInformation switch. I figured it'd be useful to know the name of the computer which made the file, so that's just what it will do. Edit -Path to point to a server with a share and away you go. You'll end up with files like this:
ComputerA_Programs.Csv
ComputerB_Programs.Csv
ComputerC_Programs.Csv
If you want to pull from all Ad computers
ForEach ($COMPUTER in (Get-ADComputer -Filter * | Select-Object -ExpandProperty Name))
{if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "cannot reach $computer" -f red}
else{Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Export-CSV -NoTypeInfo -Path "\\server\share$\$Computer_Programs.csv" -NoTypeInformation}}
for if you have list of computers in text
Foreach ($computer in ($computers= Get-Content "c:\Computers.txt" ))
{if(!(Test-Connection -cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "cannot reach $computer" -f red}
else{
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Export-CSV -NoTypeInfo -Path "\\server\share$\$Computer_Programs.csv" -NoTypeInformation
}}

Trying to determine managedby attribute for specific distributionlist

I've a text file with a list of distribution groups that I'm trying to get managedby attribute. I tried running different commands but seems to be a syntax issue ( fairly new to PowerShell) because I'm able to retrieve the attribute managedby for single distribution group. When I'm formatting and exporting the result to csv file all I get is a bunch of numbers. I'm on powershell exchange server 2008.
Starting with a flat text file named groups.txt:
Employees#company.com
Executives#company.com
Suggested Solution:
$grouplist = Get-Content groups.txt | foreach {Get-DistributionGroup -Identity $_ | Select-Object PrimarySMTPaddress, ManagedBy}
$grouplist | Export-Csv -Path results.csv -NoTypeInformation
Gives results like:
"PrimarySmtpAddress","ManagedBy"
"Employees#company.com","admin"
"Executives#company.com","admin"
Reproducing the "bunch of numbers" issue:
$grouplist = Get-Content groups.txt | foreach {Get-DistributionGroup -Identity $_ | Select-Object PrimarySMTPaddress, ManagedBy}
Export-Csv -InputObject $grouplist -Path results2.csv -NoTypeInformation
Resulted in:
"Count","Length","LongLength","Rank","SyncRoot","IsReadOnly","IsFixedSize","IsSynchronized"
"2","2","2","1","System.Object[]","False","True","False"
Environment: Exchange 2013, Powershell 5.0, Windows 10 Tech Preview 3